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?
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