CodeIgniter Forums
Problem with creating a form dynamically - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Problem with creating a form dynamically (/thread-13320.html)



Problem with creating a form dynamically - El Forum - 11-18-2008

[eluser]browntown[/eluser]
Okay, I have a problem that's driving me nuts. Here's the situation:

I have a site where a user can submit an request for an estimate...the admin can then add line items and submit a proposal. The admin can also go back at a later time and add additional items by clicking a "add item" button. I'm using javascript to add additional rows.

So my the number of line items will change based on how many items the admin has added.

When I come back to the form I'm getting a number of "illegal offset" type of errors. My code is trying to use an array as a counter and I can't figure out why.

My form code looks like this:
Code:
<?
    $form = array('class'=>'form', 'id'=>'frm_createInvoice');
    $description = "description";
    $amount = "amount";
    $discount = array('name'=>'discount', 'id'=>'discount', 'value'=>$this->validation->discount, 'maxlength'=>'', 'length'=>'50', 'size'=>'50', 'style'=>'');    
    $percentage = array('name'=>'percentage', 'id'=>'percentage', 'value'=>$this->validation->percentage, 'maxlength'=>'3', 'length'=>'5', 'size'=>'4', 'style'=>'');
    if($items){
        $code .= $items;
    }
    else{
        for($i=0; $i<=count($lineItems); $i++){
            $$description{$i} = array('name'=>'description'.$i, 'id'=>'description'.$i, 'value'=>$lineItems[$i]['item'], 'maxlength'=>'', 'length'=>'50', 'size'=>'50', 'style'=>'');    
            $$amount{$i} = array('name'=>'amount'.$i, 'id'=>'amount'.$i, 'value'=>$lineItems[$i]['amt'], 'maxlength'=>'', 'length'=>'5', 'size'=>'4', 'style'=>'');

            $totalAmount += $lineItems[$i]['amt'];
            $code .="
                <tr>
                    <td class='description' colspan='2'>".form_input($$description{$i})."</td>
                    <td class='dollar'>$</td>
                    <td class='amount'>".form_input($$amount{$i})."</td>
                    <td class='cents'>.00</td>
                </tr>
            ";
        }
    }
    
    
    
    $code .="
        <tr id='lastRow'>
            <td class='discount' colspan='2' style='text-align:right;'>
                <div style='position:relative; float:left; width:49%; text-align:left;'><a href='#' id='btn_addItem'>Add item</a></div>
                <div style='position:relative; float:right; width:49%;'>Discount:</div>
                    <div class='clearer'></div>
            </td>
            <td class='dollar'>&nbsp;</td>
            <td class='amount'> ".form_input($percentage)."</td>
            <td class='cents'>%</td>
        </tr>
        <tr>
            <td class='total' colspan='2' style='text-align:right; font-size:160%;'>Total Due:</td>
            <td class='dollar' style='font-size:160%;'>$</td>
            <td class='amount' style='text-align:right; font-size:160%;'><div id='totalDue'>".$totalAmount."</div></td>
            <td class='cents' style='font-size:160%;'>.00</td>
        </tr>
    ";

    $jobestimate = array('name'=>'jobestimate', 'id'=>'jobestimate', 'value'=>$this->validation->jobestimate, 'maxlength'=>'5', 'length'=>'5', 'size'=>'4', 'style'=>'');
?&gt;

The last instance of $i is an array? A var_dump of the last $i produces the following:

Code:
array(7) { ["name"]=>  string(12) "description5" ["id"]=>  string(12) "description5" ["value"]=>  string(5) "mark7" ["maxlength"]=>  string(0) "" ["length"]=>  string(2) "50" ["size"]=>  string(2) "50" ["style"]=>  string(0) "" }

Can someone explain why this is happening?

I apologize if this is unclear...please let me know if I can clarify anything.

Thanks!