Page 1 of 2

Loop through Arrays?

Posted: Thu Dec 09, 2004 2:50 pm
by amdorsey
How would you loop through the array, in yo,ur update code.

Posted: Thu Dec 09, 2004 3:16 pm
by scorphus
With [php_man]foreach[/php_man] and [php_man]implode[/php_man] maybe... it depends on lots of things. What is the scenario?

Posted: Thu Dec 09, 2004 3:48 pm
by amdorsey
I am trying to Update multiple records in a database using checkboxes that are in a repeated region. So I have a table with one row repeating, and I need to update the records with the corresponding checkboxes. make sense?

Posted: Thu Dec 09, 2004 3:57 pm
by protokol
Not really. Can you post some HTML to clarify?

Posted: Thu Dec 09, 2004 4:07 pm
by amdorsey
Here is the code for the table...

Code: Select all

<td align="center" valign="middle" class="style16"><input name="isSelected" type="checkbox" id="isSelected" value="checkbox"></td>
      <td class="style16"><?php echo $row_Log_Display&#1111;'date_created']; ?></td>
      <td class="style16"><?php echo $row_Log_Display&#1111;'event']; ?></td>
      <td class="style16"><?php echo $row_Log_Display&#1111;'region']; ?></td>
      <td class="style16"><?php echo $row_Log_Display&#1111;'quanity_sets']; ?> Sets of <?php echo $row_Log_Display&#1111;'quanity_each']; ?> Each </td>
      <td><span class="style16"><?php echo $row_Log_Display&#1111;'size']; ?> <?php echo $row_Log_Display&#1111;'sort']; ?></span></td>
      <td><span class="style16"><?php echo $row_Log_Display&#1111;'description']; ?></span></td>
      <td class="style16"><span class="style16 style19"><span class="style21"><?php echo $row_Log_Display&#1111;'status']; ?></span></span></td>
I want to be able to select the

Code: Select all

<input name="isSelected" type="checkbox" id="isSelected" value="checkbox">
checkbox, and based on the rows selected it will update those records when I run the UPDATE script.

Thanks : :)

Posted: Thu Dec 09, 2004 8:41 pm
by nigma
Moved to PHP - Code forum.

Posted: Thu Dec 09, 2004 8:45 pm
by Benjamin
I had the same question a few days ago.

See this post...

viewtopic.php?t=28310

Posted: Thu Dec 09, 2004 10:47 pm
by scorphus
agtlewis wrote:(...) See this post...

viewtopic.php?t=28310
There are good bits and pieces of information on this post. Behold the DevNetwork Forums!
amdorsey wrote:(...) I want to be able to select the

Code: Select all

&lt;input name="isSelected" type="checkbox" id="isSelected" value="checkbox"&gt;
checkbox, and based on the rows selected it will update those records when I run the UPDATE script. (...)
Well, you better change the name and value of the checkboxes to something like:

Code: Select all

<input name="isSelected[]" type="checkbox" id="isSelected" value="<?php echo $row_identifier ;?>">
This way you get all checked boxes in an array inside $_POST indexed by 'isSelected', which is $_POST['isSelected']. You'll also need to identify each of them because you'll have to know which rows have to be updated later in the update script.

Couple questions. What kind of update will be going on? You are going to update rows with new information, where will it come from?

-- Scorphus

Posted: Thu Dec 09, 2004 11:20 pm
by amdorsey
This is the whole page...Form and All.

Code: Select all

<form action="<?php echo $editFormAction; ?>" method="POST" name="Status_Update" id="Status_Update">
  <table width="100%" height="30"  border="0" cellpadding="0" cellspacing="0" bgcolor="#CCCCCC">
    <tr bgcolor="#FFFFFF">
      <td align="left" valign="middle" class="style1"><?php echo ("$titleName"); ?> Signs Manager <span class="style23">- <?php echo ("$displayDate"); ?></span></td>
    </tr>
  </table>
  <br>
  <table width="100%"  border="0" cellpadding="10" cellspacing="1" bgcolor="#CCCCCC">
    <tr>
      <td width="50%" align="left" valign="middle" bgcolor="#F2F2F2" class="style16 style19">Change Status to:
          <select name="status" class="style16" id="status">
            <option selected>Status...</option>
            <option value="Just Added">Just Added</option>
            <option value="Pending Approval">Pending Approval</option>
            <option value="Approved and Printing">Approved and Printing</option>
            <option value="Completed">Completed</option>
        </select>
          <input name="Submit" type="submit" class="style16" value="Update Sign Status">
</td>
      <td width="50%" align="left" valign="middle" bgcolor="#F2F2F2" class="style16">You have selected a total of <span class="style18 style25"><?php echo $totalRows_Log_Display ?></span> Signs.</td>
    </tr>
  </table>
  <br>
  <table width="100%"  border="0" cellpadding="5" cellspacing="1" bgcolor="#CCCCCC">
    <tr align="left" valign="middle" bgcolor="#E6E6E6">
      <td><span class="style18">Select</span></td>
      <td><span class="style18">Date /Time Added </span></td>
      <td><span class="style18">Event</span></td>
      <td bgcolor="#E6E6E6"><span class="style18">Region</span></td>
      <td><span class="style18">Quanity</span></td>
      <td><span class="style18"> Sign </span></td>
      <td><span class="style18">Description</span></td>
      <td><span class="style18">Status</span></td>
    </tr>
	<?php $count=0; ?>
    <?php do &#123; ?>
	<?php
$count++;
if(($count % 2) == 1)&#123;
$background = "#ffffff";
&#125; else &#123;
$background = "#F4F4F4";
&#125;
?>
    <tr align="left" valign="middle" bgcolor="<?php echo $background; ?>">
      <td align="center" valign="middle" class="style16">        <input name="id" type="hidden" id="id" value="<?php echo $row_Log_Display&#1111;'id']; ?>">      </td><td class="style16"><?php echo $row_Log_Display&#1111;'date_created']; ?></td>
      <td class="style16"><?php echo $row_Log_Display&#1111;'event']; ?></td>
      <td class="style16"><?php echo $row_Log_Display&#1111;'region']; ?></td>
      <td class="style16"><?php echo $row_Log_Display&#1111;'quanity_sets']; ?> Sets of <?php echo $row_Log_Display&#1111;'quanity_each']; ?> Each </td>
      <td><span class="style16"><?php echo $row_Log_Display&#1111;'size']; ?> <?php echo $row_Log_Display&#1111;'sort']; ?></span></td>
      <td><span class="style16"><?php echo $row_Log_Display&#1111;'description']; ?></span></td>
      <td class="style16"><span class="style16 style19"><span class="style21"><?php echo $row_Log_Display&#1111;'status']; ?></span></span></td>
    </tr>
    <?php &#125; while ($row_Log_Display = mysql_fetch_assoc($Log_Display)); ?>
  </table>
  <input type="hidden" name="MM_update" value="Status_Update">
</form>
I want to update the status of multipler checked rows in my query display. So the page before this one the user sorts out info from the database. I want them to be able to update 1 column in the table (status) So I have one pulldown menu with different "status" values, and I wan't that value to be substituted into all of the rows that have been checked.

I hope this explains my scenario a little better.

Posted: Thu Dec 09, 2004 11:58 pm
by scorphus
Nice! As I said above, change the name and value of the checkbox:

Code: Select all

<input name="isSelected[]" type="checkbox" id="isSelected" value="<?php echo $row_Log_Display['id']; ?>">
and write it in place of that hidden filed. This way you'll get an item in $_POST['isSelected'] holding a row ID for each marked checkbox. Then, in the form's action= page, do something like this:

Code: Select all

<?php
$newStatus = $_POST['status'];
foreach ($_POST['isSelected'] as $id) {
    $sql = "UPDATE Log_Display SET status = '$status' WHERE id = $id";
    mysql_query($sql) or die('Query error! ' . mysql_error());
}
?>
References:
[php_man]foreach[/php_man]
[php_man]mysql_affected_rows[/php_man]

Regards,
Scorphus.

Posted: Fri Dec 10, 2004 1:14 am
by scorphus
Or better yet:

Code: Select all

<?php
$newStatus = $_POST['status'];
$values = implode(', ', $_POST['isSelected']);
$sql = "UPDATE Log_Display SET status = '$status' WHERE id IN ($values)";
mysql_query($sql) or die('Query error! ' . mysql_error());
?>
-- Scorphus

Posted: Fri Dec 10, 2004 1:28 am
by ol4pr0
CROSS post ??

URI=>HERE

Posted: Fri Dec 10, 2004 12:44 pm
by scorphus
Sure it is a cross post. Thanks, ol4pr0. Could some moderator please lock the other thread?

-- Scorphus

Posted: Fri Dec 10, 2004 12:57 pm
by amdorsey
Sorry, I didn't even realize I made a cross-post, my bad.

Posted: Fri Dec 10, 2004 1:08 pm
by scorphus
So, amdorsey, what about your problem with the update script? Yet it is the main subject here! :) Let us know if you've got it working or not...

-- Scorphus