tags:

views:

51

answers:

2

Hi I'm looking for suggestions on how best to handle the submission of a database table.

So far, I've generated the repeating rows data like so:

<table border=1>
  <tr>
  <th>Phone Number</th>
  <th>Prefix CallerID with</th>
  <th>Seconds to ring before<br/>reaching voicemail</th>
  <th>Voice mailbox<br/>extension number</th>
  <th>Notes</th>

  </tr>
  <?php
      $id = 1;
      foreach ( $line_detail as $line ){
          echo '<tr>';
          echo '<td>'.form::input($id.'_did', $line->did).'</td>';
          echo '<td>'.form::input($id.'_cid_prefix', $line->cid_prefix).'</td>';
          echo '<td>'.form::input(array($id.'_dial_timeput', 'size'=>15, 'maxlength'=>3), $line->dial_timeout).'</td>';
          echo '<td>'.form::dropdown($id.'_extension', $phones, $line->voicemail).'</td>';
          echo '<td>'.form::input($id.'_notes', $line->notes).'</td>';
          echo "</tr>";
          $id++;
      }
  ?>
</table>

And when I submit this I have repeating data like this:

(array) Array
(
    [1_did] => 64123456789
    [1_cid_prefix] => DDI-
    [1_extension] => 800
    [1_notes] => 
    [2_did] => 645678903
    [2_cid_prefix] => TEST-
    [2_extension] => 820
    [2_notes] => 
    [submit] => Save
)

Ok, so now I can group them by id and submit an update to the database. But, I'm wondering if there is a better way of doing this.

Any suggestions?

+3  A: 

PHP's array notation for input names is the way to go.

<input name="data[<?php echo $id;?>]['did'] />
<input name="data[<?php echo $id;?>]['cid_prefix'] />
<input name="data[<?php echo $id;?>]['extension']"/>
...

You'll end up, once the data are posted, with a nice two-dimensional associative array.

timdev
Thanks, this is exactly what I needed. Makes it far simpler to work with.
Matt H
A: 

you can build form like this:

foreach ( $line_detail as $line ){
      echo '<tr>';
      echo '<td>'.form::input('did['.$id.']', $line->did).'</td>';
      echo '<td>'.form::input('cid_prefix['.$id.']', $line->cid_prefix).'</td>';
      echo '<td>'.form::input(array('dial_timeput['.$id.']', 'size'=>15, 'maxlength'=>3), $line->dial_timeout).'</td>';
      echo '<td>'.form::dropdown('extension['.$id.']', $phones, $line->voicemail).'</td>';
      echo '<td>'.form::input('notes['.$id.']', $line->notes).'</td>';
      echo "</tr>";
      $id++;
  }

it will give you the following array:

(array) Array
(
    [did] => array(
        [1] => 64123456789,
        [2] => 645678903,
    )
    [cid_prefix] => array(
        [1] => 'DDI-',
        [2] => 'TEST-',
    )
    [extension] => array(
        [1] => 800,
        [2] => 820,
    )
    [notes] => array(
        [1] => '',
        [2] => '',
    )
    [submit] => Save
)

it might be a little more convinient

kgb
Thanks, it's very close to what I wanted. However, the result of timdev's answer is a little easier to work with.
Matt H