...

View Full Version : Table iteration



MHaris
08-23-2007, 01:05 AM
<table border="1" cellspacing="0" cellpadding="5">
<tr>
<th>Questions</th>
<th>Your Answer</th>
<?php
foreach($closest as $key => $value){
$result = mysql_query("SELECT * FROM candidates WHERE id='$key'") or die(mysql_error());
while ($row = mysql_fetch_array($result)){
echo "<th>".$row['name']."</th>";
}
}
?>
</tr>
<?php
$result = mysql_query("SELECT * FROM questions");
foreach($user_answer[0] as $key => $value){
$a = $value;
}
while ($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td>".$row['question']."</td>";
echo "<td>".$a."</td>";
echo "</tr>";
}
?>
</tr>
</table>

How do I echo out all the $value($a) in the while loop? $a is just the last value of the array right now.

Mwnciau
08-23-2007, 01:34 AM
<table border="1" cellspacing="0" cellpadding="5">
<tr>
<th>Questions</th>
<th>Your Answer</th>
<?php
foreach($closest as $key => $value){
$result = mysql_query("SELECT * FROM candidates WHERE id='$key'") or die(mysql_error());
while ($row = mysql_fetch_array($result)){
echo "<th>".$row['name']."</th>";
}
}
?>
</tr>
<?php
$result = mysql_query("SELECT * FROM questions");
$a = $user_answer[0];
$i = 0;
while ($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td>".$row['question']."</td>";
echo "<td>".$a[$i]."</td>";
echo "</tr>";
$i++;
}
?>
</tr>
</table>

Is that what you wanted?

MHaris
08-23-2007, 10:11 AM
<table border="1" cellspacing="0" cellpadding="5">
<tr>
<th>Questions</th>
<th>Your Answer</th>
<?php
foreach($closest as $key => $value){
$result = mysql_query("SELECT * FROM candidates WHERE id='$key'") or die(mysql_error());
while ($row = mysql_fetch_array($result)){
echo "<th>".$row['name']."</th>";
}
}
?>
</tr>
<?php
$a = $user_answer[0];
$i = 0;
foreach($closest as $key => $value){
$result = mysql_query("SELECT c.*, q.*, a.* FROM (( answers a LEFT JOIN candidates c ON a.candidate_id = c.id ) LEFT JOIN questions q ON a.question_id = q.id ) WHERE c.id = '$key'");
}
while ($row = mysql_fetch_array($result, MYSQL_BOTH)){
echo "<tr>";
echo "<td>".$row['question']."</td>";
echo "<td>".$a[$i]."</td>";
echo "<td>".$row['answer']."</td>";
$i++;
echo "</tr>";
}
?>
</tr>
</table>
$row['answer'] is suppose to echo answers of all the three keys in closest but echoing only the last key.

What should I do? And is there a better way to code these type of things?

MHaris
08-23-2007, 04:46 PM
Help?

Mwnciau
08-24-2007, 01:54 PM
foreach($closest as $key => $value){
$result = mysql_query("SELECT c.*, q.*, a.* FROM (( answers a LEFT JOIN candidates c ON a.candidate_id = c.id ) LEFT JOIN questions q ON a.question_id = q.id ) WHERE c.id = '$key'");
}

Everytime that loops round $result is being written over, I'm not sure what you want to do, can you explain it again?

MHaris
08-24-2007, 09:40 PM
foreach($closest as $key => $value){
$result = mysql_query("SELECT c.*, q.*, a.* FROM (( answers a LEFT JOIN candidates c ON a.candidate_id = c.id ) LEFT JOIN questions q ON a.question_id = q.id ) WHERE c.id = '$key'");
}

Everytime that loops round $result is being written over, I'm not sure what you want to do, can you explain it again?

http://codingforums.com/showthread.php?t=122156



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum