...

View Full Version : Building my table.



MHaris
08-27-2007, 11:00 PM
<table border="1" cellspacing="1" cellpadding="5">
<tr>
<th>Questions</th>
<th>Your Answer</th>
<?php
foreach($closest as $key => $value){
$sql_name = mysql_query("SELECT * FROM candidates where id ='$key'");
$name[] = mysql_fetch_assoc($sql_name);
}

foreach($name as $key => $value){
echo "<th>".$value['name']."</th>";
}
?>
</tr>
<?php
$sql_question = mysql_query("SELECT * FROM questions");
while($row = mysql_fetch_assoc($sql_question)){
echo "<tr>";
echo "<td>".$row['question']."<td>";
echo "</tr>";
}

foreach($user_answer as $key => $value){
foreach($value as $answer){
echo "<tr>";
echo "<td>".$answer."</td>";
echo "</tr>";
}
}

?>
</table>

How can I build my table on the following patten.


<table border="1" cellspacing="" cellpadding="5">
<tr>
<th>Questions</th>
<th>Your Answer</th>
<th>Obama</th>
<th>Clinton</th>
<th>Cox</th>
</tr>
<tr>
<td>Are you for or against drilling for energy and oil</td>
<td>For</td>
<td>Against</td>
<td>For</td>
<td>For</td>
</tr>
<tr>
<td>Are you for or against Embyronic Stem Cells</td>
<td>For</td>
<td>For</td>
<td>For</td>
<td>For</td>
</tr>
<tr>
<td>Are you for or against death penatly</td>
<td>Against</td>
<td>Against</td>
<td>For</td>
<td>Against</td>
</tr>
<tr>
<td>Are you for or against education for all childrens</td>
<td>Against</td>
<td>Against</td>
<td>Against</td>
<td>Against</td>
</tr>
<tr>
<td>Are you for or against same-sex marriage</td>
<td>For</td>
<td>Against</td>
<td>Against</td>
<td>For</td>
</tr>
</table>

$closest =

Array
(
[4] => 2
[3] => 2
[2] => 2
)

$name =

Array
(
[0] => Array
(
[id] => 4
[name] => Brownback
)

[1] => Array
(
[id] => 3
[name] => Biden
)

[2] => Array
(
[id] => 2
[name] => Clinton
)

)

$user_answer =

Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 2
[3] => 1
[4] => 2
)

)

I want to base the table on the HTML above but I cannot create it. How would I go about doing it?

MHaris
08-28-2007, 02:33 AM
Any ideas anyone?

Fumigator
08-28-2007, 05:12 AM
I have no idea how the pattern you posted relates to the $closest and $name arrays you posted.

MHaris
08-28-2007, 11:14 AM
I have no idea how the pattern you posted relates to the $closest and $name arrays you posted.

These name comes from the name array that is build up through the closest array. The closest array contains the ids of these names.


<th>Obama</th>
<th>Clinton</th>
<th>Cox</th>
</tr>

pemcconnell
08-28-2007, 04:16 PM
I would do it like so...


<?
//sql connection
// mysql_query connection(s)
?>
<table border="1" cellspacing="1" cellpadding="5">
<?
while($row = mysql_fetch_array($questions)){
// can have a database column assigned to $question or, a switch statement
echo '<tr><th>';
echo $question;
echo '</th></tr>';
// insert switch statement $query1 to select different sql statements(answers) for different questions
while($row = mysql_fetch_array($query1)){
// echo all results for question one out
echo '<tr><td>';
echo $row['Answer'];
echo '</td></tr>';
}
echo '<tr><td>&nbsp;</td></tr>';
}
?>
</table>

MHaris
08-29-2007, 08:10 PM
I would do it like so...


<?
//sql connection
// mysql_query connection(s)
?>
<table border="1" cellspacing="1" cellpadding="5">
<?
while($row = mysql_fetch_array($questions)){
// can have a database column assigned to $question or, a switch statement
echo '<tr><th>';
echo $question;
echo '</th></tr>';
// insert switch statement $query1 to select different sql statements(answers) for different questions
while($row = mysql_fetch_array($query1)){
// echo all results for question one out
echo '<tr><td>';
echo $row['Answer'];
echo '</td></tr>';
}
echo '<tr><td>&nbsp;</td></tr>';
}
?>
</table>

Can you give a more live example?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum