Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've got this code designed to echo a list of people from a specific location. You can then group all these people by checking boxes next to their names and submitting the form which will update the database with the new group. It all sounds pretty simple. But unfortunately the database is set up using a lot of tables that are associated with one another. So it gets a bit complicated. Here's what I have so far:

<?php

//Query the database for the Location ID of the currently logged in user

$query1 = mysql_query("SELECT * FROM `Location` WHERE Name = '".$_SESSION['Location']."'");
$array1 = mysql_fetch_array($query1);

//Query the database for the all the participants in this location, make an array of the results and then implode the array

$query2 = mysql_query("SELECT idParticipant FROM `Participant/Location` WHERE idLocation = ".$array1['idLocation'].";");
$column1 = array();
while($row = mysql_fetch_array($query2)){
$column1[] = $row['idParticipant'];
}
$values = implode(' OR ', $column1);

//Query database for all first names where the particpant ID's equal those of the above $values

$query3 = mysql_query("SELECT firstName FROM `Participant` WHERE idParticipant = $values;");
$columnFirstName = array();
while($row = mysql_fetch_array($query3)){

$columnFirstName[] = $row['firstName'];

}

foreach($columnFirstName as $value){

echo "<input type='checkbox' name='check[]' id='' value='' />";
echo $value."<br><br>";

}


?>
<input type="submit" name="submit" value="Save New Group"> 

</form>

So the above code echoes out all the first names in that location and adds a check box beside it. The problem is I need the second names to appear beside the first names too. AND I need to get the ID's of each person to put in the value for the checkbox.

I'm not sure how I can do this... I think I need to make a second query for the second names then tweak my foreach loop...and I'll probably need a whole new foreach loop for the id's, won't I?

share|improve this question
2  
Please, don't use mysql_* functions for new code. They are no longer maintained and the community has begun the deprecation process. See the red box? Instead you should learn about prepared statements and use either PDO or MySQLi. If you can't decide, this article will help to choose. If you care to learn, here is good PDO tutorial. –  Madara Uchiha Jun 14 '12 at 13:45
 
The code you pasted here doesn't show the query for lastname. –  Chibuzo Jun 14 '12 at 13:57
add comment

1 Answer

up vote 0 down vote accepted

This is what I needed to do:

$query3 = mysql_query("SELECT * FROM `Participant` WHERE idParticipant = $values;");
$columnFirstName = array();
$columnSecondName = array();
$columnID = array();
while($row = mysql_fetch_array($query3)){

$columnFirstName[] = $row['firstName'];
$columnSecondName[] = $row['secondName'];
$columnID[] = $row['idParticipant'];

}

for($i=0;$i<count($columnFirstName);$i++) {

 $firstName  = $columnFirstName[$i];
 $secondName = $columnSecondName[$i];
 $id = $columnID[$i];

 echo "<input type='checkbox' name='check[]' id='$id' value='$id' />";
 echo $firstName." ";
 echo $secondName."<br><br>";

}

?>

<input type="submit" name="submit" value="Save New Group"> 

</form>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.