views:

132

answers:

2

Hello Everyone,

I am using this fantastic example of a jQuery editable invoice as a template for creating dynamic invoices for my users.

It's working quite well and I am successfully generating the items but I now need to save the values entered into the various text fields and enter them into the MySQL database.

I am confident in doing the MySQL entering with PHP but what makes this trickier is that the amount of 'invoice items' is completely dynamic and I am unsure how I can get PHP to 'check' through the pages text fields and find new ones, group them and then add them to my DB.

Here is an example of my code that I am using to generate the items:

<?php if($invoice_items->result_array()) { ?>
<?php foreach($invoice_items->result_array() as $invoice_Row): ?>
    <tr class="item-row">
            <td class="item-name">
            <div class="delete-wpr">
                <textarea><?php echo $invoice_Row['item_name']; ?> Facility Booking</textarea>
                <a class="delete" href="javascript:;" title="Remove row">X</a>
            </div>
        </td>
            <td class="description">
                <textarea><?php echo $invoice_Row['description']; ?></textarea>
            </td>
            <td><textarea class="cost">$<?php echo $invoice_Row['hourly_cost']; ?>.00</textarea></td>
            <td><textarea class="qty"><?php echo $total_time_hours; ?></textarea></td>
            <td><span class="price">$<?php $unit_total = $invoice_Row['hourly_cost']* $total_time_hours; echo $unit_total;?>.00</span></td>
    </tr>
<?php endforeach; ?>
<?php } ?>  

I am thinking that I need to perhaps generate unique ID's for each invoice items text field, ie item-1-desc, item-1-cost etc, but that involves writing javascript which I know almost nothing about. Also I would still have to get PHP to loop through the ID's somehow until it reached the end...

If anyone has attempted something similar before or you can see a solution to my problem I would greatly appreciate your help.

Thanks,

Tim

A: 

Use the php form array syntax name="item-desc[<?php echo $id?>]"

You can then iterate them on the backend with foreach to store the data. You have the id's as keys to the arrays so it should be fairly trivial to update the db.

Byron Whitlock
Hey Byron, thanks for posting. Please correct me if I am wrong but what happens when a user adds a row by themselves through the jquery interface. This new data won't have originated from a PHP foreach loop and therefore I won't be able to insert the data into the database because PHP doesn't know it exists. Is there a way around this?
Tim
You should set an `item-id[]=$id` for the rows that already exist, and use `item-desc[]` then you iterate `item-desc`, checking for id's in `item-id`. If it doesn't exist in item id, then its a new row. Or you could add an ajax callback to update the backend and set the id from the result.
Byron Whitlock
A: 

did u find a solution ?

Anice
yes, see the answer above. Also for future reference you should add comments or questions in the comments field and leave the answers for answers. Cheers.
Tim