Welcome Guest, Not a member yet? Register   Sign In
Insert array into database
#4

[eluser]bohara[/eluser]
Thanks for replying. Let me see if I can clarify.

The html looks like this
Code:
<fieldset>
    <legend><span>Customer Information</span></legend>
        <ol>
            <li><label>Company Name</label>
            &lt;? echo form_input('company_name'); ?&gt;</li>
            <li><label>Notes</label>
            &lt;textarea name="notes" value="" rows="3" cols="50"&gt;&lt;/textarea></li>
        </ol>
</fieldset>

<fieldset id="line_tems">
    <legend><span>Traps</span></legend>
    <ol>
        <li>
        <table cellspacing="0">
        <thead>
            <tr>
                <th>Qty</th>
                <th>Trap</th>
                <th>Shatterproof ?</th>
            </tr>
        </thead>
        <tbody id="container">
            <tr id="row0">
                <td>    
                    &lt;input type="text" name="quantity[0][0]" value="" maxlength="2" size="2"  /&gt;
                </td>
                <td>    
                    &lt;? echo form_dropdown('bulb_type_id[0][1]', $bulb_types); ?&gt;
                </td>
                <td>    
                    &lt;? echo form_checkbox('shatterproof[0][2]', 1, false); ?&gt;
                </td>
            </tr>
        </tbody>
    </table>
    </li>
    </ol>
    
    <p><a class="addRow" href="#">Add another trap</a></p>
</fieldset>

<fieldset>
    <legend><span>Reminder Information</span></legend>
        <ol>
            <li><label>Email to</label>
            &lt;? echo form_input('email_to'); ?&gt;</field><br /></li>
        
            <li><label>Remind me</label>
            &lt;? echo form_dropdown('reminder_date', $reminder_dates); ?&gt;</field> From &lt;?=$created_at?&gt;</li>
        </ol>
</fieldset>

When you click the 'addRow' link shown above another row is inserted to the form via AJAX that looks like this. with the form values incremented by 1 (below)

Code:
<tr id="row0">
                <td>    
                    &lt;input type="text" name="quantity[1][0]" value="" maxlength="2" size="2"  /&gt;
                </td>
                <td>    
                    &lt;? echo form_dropdown('bulb_type_id[1][1]', $bulb_types); ?&gt;
                </td>
                <td>    
                    &lt;? echo form_checkbox('shatterproof[1][2]', 1, false); ?&gt;
                </td>
            </tr>


The function that is inserting this data in my model is as follows:
Code:
function add_reminder()
    {
        $account_id = $this->session->userdata('account_id');
        
        $this->account_id = $account_id;
              $this->company_name = $_POST['company_name'];
        $this->notes = $_POST['notes'];
        $this->email_to = $_POST['email_to'];
        $this->reminder_date = $_POST['reminder_date'];
        $this->created_at = $_POST['created_at'];
        
        $this->db->insert('reminders', $this);
        
        // Insert line_items
        $query = $this->db->query('SELECT id FROM reminders ORDER BY id DESC LIMIT 1');
        $row = $query->row_array();
        
        $reminder_id = $row['id'];
        
        for ($i = 0; $i <= count($_POST['quantity'])-1; $i++) {
            
                $sql = array(
                   $this->reminder_id = $reminder_id,
                   $this->quantity = $_POST['quantity'][$i][1],
                   $this->bulb_type_id = $_POST['bulb_type_id'][$i][2],
                   //$this->shatterproof = $_POST['shatterproof'][$i][3]
                    );
                    
                $this->db->insert('line_items', $sql);
        
        }

    }

In regards to the variable name, I agree it is lame Smile I am just trying to get it working at this point.

Everything up to the for loop executes as expected. I hope that clarifies things a bit. I don't think I am to far off as this was working at one point. Although I may have just been lucky.

Thanks again.


Messages In This Thread
Insert array into database - by El Forum - 07-23-2008, 12:38 PM
Insert array into database - by El Forum - 07-24-2008, 08:30 AM
Insert array into database - by El Forum - 07-24-2008, 10:08 AM
Insert array into database - by El Forum - 07-24-2008, 10:25 AM
Insert array into database - by El Forum - 07-24-2008, 10:34 AM
Insert array into database - by El Forum - 07-24-2008, 10:38 AM
Insert array into database - by El Forum - 07-24-2008, 10:41 AM
Insert array into database - by El Forum - 07-24-2008, 10:43 AM
Insert array into database - by El Forum - 07-24-2008, 10:48 AM
Insert array into database - by El Forum - 07-24-2008, 11:00 AM
Insert array into database - by El Forum - 07-24-2008, 11:49 AM
Insert array into database - by El Forum - 07-24-2008, 12:04 PM
Insert array into database - by El Forum - 07-24-2008, 12:09 PM
Insert array into database - by El Forum - 07-24-2008, 01:44 PM
Insert array into database - by El Forum - 08-26-2008, 12:04 AM



Theme © iAndrew 2016 - Forum software by © MyBB