...

View Full Version : foreach unde while?



MHaris
08-24-2007, 04:51 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_ASSOC)){
echo "<tr>";
echo "<td>".$row['question']."</td>";
echo "<td>".$a[$i]."</td>";
foreach($closest as $key => $value){
echo "<td>".$row['answer']."</td>";
}
$i++;
echo "</tr>";
}
?>
</tr>
</table>

The $row['answer'] is taking the last row from the query that is in foreach. Though, I want all the answers of all the candidates to be published in the table.

Any help how would I go about doing it? If I do a while in the second foreach, 10 tds will be created due to repetition.


foreach($closest as $key => $value){
echo "<td>".$row['answer']."</td>";
}



How should I do it?

Fumigator
08-24-2007, 05:42 AM
The obvious problem is you have your mysql_query() function call inside your foreach loop but you do not fetch the data out of the query until after the foreach loop is over, which means the only $closest array element that is fetched out of the query is the last one.

I would suggest you run one query and build an array containing just the relevant info you need based on that one query. You can then manipulate the array for purposes of building your html code. As you have it, you're using the query loop (the "while"s) to build the markup, so you're forced into multiple queries, and (apparantly) multiple foreach loops on the $closest array.

Which brings me to my question: What does the $closest array contain and what does it do for you? It seems the data from your query is related directly to the stuff in the $closest array (connected via $key), so it makes sense to me to add the query data to the $closest array and go from there.

Example:


foreach($closest as $key => $value){

$result = mysql_query("
SELECT c.*, q.*, a.*
FROM answers as a
LEFT JOIN candidates as c
ON a.candidate_id = c.id
LEFT JOIN questions as q
ON a.question_id = q.id
WHERE c.id = '$key'") or die(mysql_error());

for ($i = 0; $i < mysql_num_rows($result); $i++)
$closest[$key][$i] = mysql_fetch_assoc($result);
}
}

//You can now use the $closest array to build your html without having to do any other queries
foreach ($closest as $key => $value) {
foreach ($value as $answers) {
echo "<th>".$answers['name']."</th>";
}
}

MHaris
08-24-2007, 06:57 AM
Warning: Cannot use a scalar value as an array in C:\wamp\www\quiz\templates\compare_table.php on line 18


$closest[$key][$i] = mysql_fetch_assoc($result);

$closest is


foreach($binned as $key => $value){
$results[$key] = count(array_diff_assoc($user_answer[0], $value));
}

asort($results);
$closest = array_slice($results, 0, 3, 1);

And $binned is:


$result = mysql_query("SELECT * FROM answers") or die(mysql_error());
while ($row = mysql_fetch_assoc($result)){
$cID = $row['candidate_id'];
$binned_key = $cID;
$binned[$binned_key][] = $row["answer"];
}

And also Warning:


Invalid argument supplied for foreach() in C:\wamp\www\quiz\templates\compare_table.php on line 24

Line # 24:

foreach ($value as $answers) {

MHaris
08-25-2007, 10:25 PM
Help?

Inigoesdr
08-26-2007, 04:32 AM
$value is not an array, post the code that deals with it before line 24.

MHaris
08-26-2007, 12:31 PM
$value is not an array, post the code that deals with it before line 24.


//You can now use the $closest array to build your html without having to do any other queries
foreach ($closest as $key => $value) {
foreach ($value as $answers) {
echo "<td>".$answers['name']."</td>";
}
}
?>

Inigoesdr
08-26-2007, 04:38 PM
Put this before the foreach and post what it outputs:

echo '<pre>' . print_r($closest, 1);

rafiki
08-26-2007, 04:46 PM
question if i may, what does the 1 do in print_r() is it the same as true? if so why would you want to return the output instead of it being output?

Inigoesdr
08-26-2007, 04:48 PM
Yes, it's the same. It returns the output to echo() because print_r() will print the array before anything is output so the <pre> tag would be after the array. So, if you return it to echo() the <pre> tag is first and will format the array so it's more readable.

This is only when it's on the same line. You can echo('<pre>'); before using print_r() and get the same result. I just like it all on one line.

MHaris
08-26-2007, 09:16 PM
Put this before the foreach and post what it outputs:

echo '<pre>' . print_r($closest, 1);


Array
(
[2] => Array
(
[4] => Array
(
[id] => 34
[name] => Clinton
[question] => Are you for or against same-sex marriage
[answer] => 2
[question_id] => 7
[candidate_id] => 2
)

)

)

Inigoesdr
08-26-2007, 11:05 PM
Maybe you should post the full page, because I just ran that code and it works fine for me.

edwardoit
01-11-2008, 03:20 AM
I think I have the same prob as here...

I want to display html table like this.
A call log each hour. (24 hours)



<tr><td>Phone#</td>
<td>0</td>
<td>1</td>
<td>2</td> .....
<td>23</td>
</tr>

# loop until last row and position each call to spicific time(hour)
<tr> <td>123-43-54</td>
<td>00:15<br>
00:35</td>
<td>01:15<br>
01:25<br>
01:55</td>
</tr>
<tr> <td>123-52-78</td>
<td>02:15<br>
02:35</td>
<td>23:15<br>
23:25<br>
23:55</td>
</tr>



I only have one mysql table for this.

caller
time_call

I need help regarding this. Any Help? :confused:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum