Jump to content

Looped results, Turn it into a multiple update record?


liamloveslearning

Recommended Posts

Hi all, Im really struggling to build an update multiple records form, Ive built my table which calls all data i need from the db in a looped region which works fine I just cant understand how to then input these back into the table in the correct rows, if this makes sense? Ive been reading this tutorial but Its not very informative its more of a copy and paste http://www.phpeasystep.com/mysql/10.html

Sorry, my code is

 <form method action details etc>
  <tr>
    <td align="center" bgcolor="#CCCCCC"><strong>Id</strong></td>
    <td align="center" bgcolor="#CCCCCC"><strong>Picture Name</strong></td>
    <td align="center" bgcolor="#CCCCCC"><strong>Upload Date</strong></td>
    <td align="center" bgcolor="#CCCCCC"><strong>No Downloads</strong></td>
    <td align="center" bgcolor="#CCCCCC"><strong> Approved? </strong></td>
    <td align="center" bgcolor="#CCCCCC"><strong> Hunnies Gallery </strong></td>
    <td align="center" bgcolor="#CCCCCC"><strong> Hunks Gallery </strong></td>
    <td align="center" bgcolor="#CCCCCC"><strong> Default Pic </strong></td>
    <td align="center" bgcolor="#CCCCCC"><strong> Theme Pic </strong></td>
    <td align="center" bgcolor="#CCCCCC"><strong> Homepage Pic </strong></td>
  </tr>

    <tr>
      <?php
$rows_massadmin = 2;
$cols_massadmin = ceil($totalRows_massadmin/ 2);
for ($i=0; $i<$rows_massadmin; $i++) {
    for ($j=0; $j<$cols_massadmin; $j++) {
        $currentIndex_massadmin = $i + $rows_massadmin * $j;
        if (@mysql_data_seek($massadmin, $currentIndex_massadmin)) {
            $row_massadmin = mysql_fetch_assoc($massadmin);
?>
  
    </tr>
    <tr>
      <td align="center"><?php echo $row_massadmin['id']; ?><input type="hidden" name="id[]" value="<?php echo $row_massadmin['id']; ?>" /></td>
      <td align="center"><?php echo $row_massadmin['user_picture']; ?><input type="hidden" name="user_picture[]" value="<?php echo $row_massadmin['user_picture']; ?>" /></td>
      <td align="center"><?php echo $row_massadmin['user_picture_date']; ?><input type="hidden" name="user_picture_date[]" value="<?php echo $row_massadmin['user_picture_date']; ?>" /></td>
      <td align="center"><?php echo $row_massadmin['picture_downloads']; ?><input type="hidden" name="downloads[]" value="<?php echo $row_massadmin['picture_downloads']; ?>" /></td>
      
      <td align="center"><input <?php if (!(strcmp($row_massadmin['user_pic_approval'],1))) {echo "checked=\"checked\"";} ?> name="user_pic_approved[]" type="checkbox" value="" /></td>
      <td align="center"><input <?php if (!(strcmp($row_massadmin['hotties_gallery'],1))) {echo "checked=\"checked\"";} ?> name="hunnies_gallery[]" type="checkbox" value="" /></td>
      <td align="center"><input name="hunks_gallery[]" type="checkbox" value="" /></td>
      <td align="center"><input <?php if (!(strcmp($row_massadmin['default_pic'],1))) {echo "checked=\"checked\"";} ?> name="default_pic[]" type="checkbox" value="" /></td>
      <td align="center"><input <?php if (!(strcmp($row_massadmin['theme_gallery'],1))) {echo "checked=\"checked\"";} ?> name="theme_pic[]" type="checkbox" value="" /></td>
      <td align="center"><input <?php if (!(strcmp($row_massadmin['homepage_pic'],1))) {echo "checked=\"checked\"";} ?> name="homepage_pic[]" type="checkbox" value="" /></td>
    </tr>
    <?php
        } else {
            echo '<td> </td>';
        } // end if;
    } //end for 2
    if ($i != $rows_massadmin-1) {
        echo "</tr><tr>";
    }
} // end for 1
?>

<tr>
    <td colspan="5" align="center"><input type="submit" name="Submit" value="Submit" /></td>
  </tr>
</table>

i typically add an identifier to each field name instead of using the bracket notation for multiple fields with the same name. Then on post, I parse off the identifier for each field name. so my code would be like this for the user_picture field:

 

<input type="hidden" name="user_picture<?php echo $row_massadmin['id']; ?>" value="<?php echo $row_massadmin['user_picture']; ?>" />

 

Then, on the post, I loop over posted values, substr()'ing the id off the end of user_picture, something like:

 

foreach ($_POST AS $key=>$val) {
   $field = 'user_picture';
   if (stristr($key, $field)) {
       // strip the id off the end, so we know which record we are updating
       $recid = substr($val, strlen($field));

       // etc....
   }

   // same for other fields...
}

 

hm, hope that's clear as mud... :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.