Jump to content

[SOLVED] printing a few selected results from multiple checkboxes in a larger list


upperbid

Recommended Posts

In the code below, I receive a list of users waiting to be approved with check boxes for either deleting or approving.  Everything works fine on the query for display and the deleting, but when I am trying to get the data for an INSERT statement, it is not working correctly.  To make this easier to resolve, I have shown the relevant code and placed

print "$del_id, $email, $fname, $lname";

in the area where I need that info.  When I select the approve checkbox for designated users (say I might select 2 out of 20 displayed) and click submit, it shows the right amount of 2 columns with the correct user IDs, but the email, fname and lname are all the same for the users I've selected (showing the data from the last row in the initial displayed results of users).

 

e.g.

 

testtest3589, [email protected], ed smith

testtest, [email protected], robert john (this is the last row in the displayed results)

 

becomes

testtest3589, [email protected], robert john

testtest, [email protected], robert john

 

My relevant part of the code is below:

 

while($r=mysql_fetch_array($result))
{	

   	$user = $r["username"];
$email = $r["email"];
$fname = $r["firstname"];
$lname = $r["lastname"];
   
   //display the row
   echo "<tr><td><input type=\"checkbox\" name=\"usera[]\" value=\"$user\" /><input type=\"checkbox\" name=\"usera[]\" value=\"$user\" /><B>$user</B>, <input type=\"hidden\" name=\"email[]\" value=$email>$email, <input type=\"hidden\" name=\"fname[]\" value=$fname>$fname <input type=\"hidden\" name=\"lname[]\" value=$lname>$lname</BR></td></tr>";
}

$count=mysql_num_rows($result);
$checka = $_POST['usera'];

if (count($checka) > 0)
{ 
   for ($i=0;$i<count($checka);$i++)
   {
   $del_id = $checka[$i];
   print "$del_id, $email, $fname, $lname";

}
}

 

Any help would be appreciated.  Thanks.

That part prints out fine, that's the first two lines in my e.g., but that is not what I need to print out the selected checkboxes.

 

When the script displays the checkbox lists, look at the values of the checkboxes and see what they are. check if they are what they are suposed to be. (i.e. fisrtname, lastname..etc)

$del_id = $checka[$i];
      print "$del_id, $email, $fname, $lname";

 

 

that print is suposed to be inside the WHILE which gets the data from the database.. if you select 2 users, you will get 2nd one only, the variables inside the WHILE loop wont transition over inside another loop.

try

 

if (count($checka) > 0)
{
   for ($i=0;$i<count($checka);$i++)
   {
      $del_id = $checka[$i];
      $sql = mysql_query("SELECT * FROM table_name WHERE user_id = '$del_id'") or die(mysql_error());
  $row = mysql_fetch_array($sql);
  
   print $row['user_id'];
   print $row['firstname'];
   print $row['lastname'];
   print $row['email'];

}
}

 

change this

 

 $sql = mysql_query("SELECT * FROM table_name WHERE user_id = '$del_id'") or die(mysql_error());

 

to whatever is your table name, and user id column name.

 

Also change

 

 print $row['user_id'];
   print $row['firstname'];
   print $row['lastname'];
   print $row['email'];

 

to the right column names...

 

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.