looping through selected fields in table

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

Post Reply
User avatar
reiqwan
Forum Commoner
Posts: 25
Joined: Fri Jan 07, 2005 2:27 am

looping through selected fields in table

Post by reiqwan »

hi all;

I have the following issue:

I need to select specific people in a table which is created through a recordset loop and then email those people details

this is the working of the code:

step 1: Query recordset
step 2: create table with recordset and place an array checkbox in one of the tables columns

I have managed to get here so far, the following steps is what II need assistance with
---------------------------------------

step 3: once the user hits submit the code needs to recall the boxes that have been selected
step 4: mail the people that have been selected via the checkboxes

Any help would be greatly appreciated, as the previous methods I used seemed to be inadequate.

thanks
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

i usually store all the rows in a session then...

Code: Select all

session_start();
...
$result = mysql_query(...);
$rows = array();
while ($row = mysql_fetch_assoc($result))
{
  $rows[] = $row;
}
...
$_SESSION['rows'] = $rows;
Now i have an array with indices from 0 to n-1 rows...

So i generate a table/list with selectboxes with values from 0 to n-1

Code: Select all

$i = 0;
foreach($rows as $row)
{
  echo "<tr>";
  echo "<td class='select'><input type='checkbox' name='select[]' value='$i'/></td>";
  // output other columns of the row
  echo "</tr>";
}


And when this page is submitted it's a matter of looking up the selected rows

Code: Select all

session_start();
...
// loop through all selected rows
foreach($_POST['select'] as $select)
{
   $row = $_SESSION['rows'][$select];
}

This mechanism gives you a simple 1 to 1 mapping between rows and select boxes (i do this to avoid problems with primary keys that exist out of more than 1 column)
User avatar
reiqwan
Forum Commoner
Posts: 25
Joined: Fri Jan 07, 2005 2:27 am

Post by reiqwan »

Great thanks for that, that seems to be themost efficient and least time consuming way.
Post Reply