2012-06-14 40 views
0

我有了這個代碼,旨在從一個特定的位置呼應的人的名單。然後,您可以通過選中名稱旁邊的複選框並提交將用新組更新數據庫的表單來將所有這些人分組。這聽起來很簡單。但不幸的是,數據庫是使用很多相互關聯的表來建立的。所以它變得有點複雜。這是我到目前爲止有:PHP呼應的MySQL數據庫中的字段和複選框

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

所以上面的代碼呼應了所有的名字在該位置,並增加了旁邊的複選框。問題是我需要第二個名字出現在名字旁邊。我需要獲取每個人的ID以輸入複選框的值。

我不知道我怎麼能做到這一點...我想我需要爲第二名稱,然後捏捏我的foreach循環第二查詢......我可能會需要一個全新的foreach循環編號的,不是嗎?

+2

請不要使用新代碼'mysql_ *'功能。他們不再被維護,社區已經開始[棄用流程](http://goo.gl/KJveJ)。請參閱[**紅框**](http://goo.gl/GPmFd)?相反,您應該瞭解[準備好的語句](http://goo.gl/vn8zQ)並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli的)。如果你不能決定,[本文](http://goo.gl/3gqF9)將有助於選擇。如果你關心學習,[這裏是很好的PDO教程](http://goo.gl/vFWnC)。 –

+0

您在此粘貼的代碼不顯示姓氏查詢。 – Chibuzo

回答

0

這是我需要做的:

$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> 
相關問題