Loop through Arrays?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

amdorsey
Forum Newbie
Posts: 19
Joined: Mon Dec 06, 2004 2:46 pm

Loop through Arrays?

Post by amdorsey »

How would you loop through the array, in yo,ur update code.
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post 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?
amdorsey
Forum Newbie
Posts: 19
Joined: Mon Dec 06, 2004 2:46 pm

Post 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?
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

Not really. Can you post some HTML to clarify?
amdorsey
Forum Newbie
Posts: 19
Joined: Mon Dec 06, 2004 2:46 pm

Post 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 : :)
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Moved to PHP - Code forum.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

I had the same question a few days ago.

See this post...

viewtopic.php?t=28310
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post 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
amdorsey
Forum Newbie
Posts: 19
Joined: Mon Dec 06, 2004 2:46 pm

Post 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.
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post 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.
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post 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
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

CROSS post ??

URI=>HERE
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

Sure it is a cross post. Thanks, ol4pr0. Could some moderator please lock the other thread?

-- Scorphus
amdorsey
Forum Newbie
Posts: 19
Joined: Mon Dec 06, 2004 2:46 pm

Post by amdorsey »

Sorry, I didn't even realize I made a cross-post, my bad.
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post 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
Post Reply