Forum Moderators: coopster

Message Too Old, No Replies

PHP query results in a table

the table heading keep repeating. please help...

         

lord_trini

10:14 am on Apr 9, 2010 (gmt 0)

10+ Year Member



I am trying to display the results of a query in the form of a table. this it the PHP that i am using below. The problem is that the headings of the table repeats. I really don't want that...

I am also trying to get a border around the cells of the table, but i get an error when i add table properties

Can someone help me please.

<?php

if ( isset( $_GET['id'] ) )
{

$query = "SELECT * FROM schedule WHERE course_id = '". $_GET['id'] ."' ORDER BY indexx ASC";

$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);


if ($numrows == 0)
{
echo "This course does not have a set schedule. ";

}

$q .= " limit $s,$limit";
$result = mysql_query($query) or die("Couldn't execute query");

while ($row= mysql_fetch_array($result))
{
$indexx = $row["indexx"];
$semester = $row["semester"];
$day = $row["day"];
$time = $row["time"];
$instructor = $row["instructor"];
$location = $row["location"];


echo "<table>";
echo "<tr>";
echo " <td>Index</td>";
echo "<td>Semester</td>";
echo "<td>Day</td>";
echo "<td>Time</td>";
echo "<td>Instructor</td>";
echo "<td>Location</td>";
echo "</tr>";
echo "<tr>";
echo "<td>$indexx</td>";
echo "<td> $semester</td>";
echo "<td>$day</td>";
echo "<td>$time</td>";
echo "<td>$instructor</td>";
echo "<td>$location</td>";
echo "</tr>";
echo"</table>";




$count++ ;

} // end WHILE

echo "<br/>End test link<br/>";
} // end IF


?>

Matthew1980

10:35 am on Apr 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there lord_trini,

Welcome to the forum :)

$query = "SELECT * FROM `schedule` WHERE `course_id` = '".mysql_realescape_string( $_GET['id'])."' ORDER BY indexx ASC";

Make the data safe, as you are using a $_GET directly in the sql query.

echo "<td>".$indexx."</td>\n\r";
echo "<td>".$semester."</td>\n\r";
echo "<td>".$day."</td>\n\r";
echo "<td>".$time."</td>\n\r";
echo "<td>".$instructor."</td>\n\r";
echo "<td>".$location."</td>\n\r";

You need to concatonate the vars into the echo correctly ;)

echo "<table border=\"1\">"; // you need to escape the " quotes as you are echoing the table.

Put an error handler on the query too:-

$numresults=mysql_query($query) or die(mysql_error());

Only use when developing, not for production.

Hope this helps a little,

Cheers,
MRb

Matthew1980

12:29 pm on Apr 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there lord_trini,

I missed from my first post that, yes you will get repeats, you are looping the whole table, so for arguments sake your query returns 3 results, you will get three seperate tables as this is how you are asking php to format the returned data.

Build the table outside the loop, then loop the rows that you want repeated inside the while, then the other side of the while, close the table. Simple :)

Hoefully that makes sense.

Cheers,
MRb

Readie

12:31 pm on Apr 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



while ($row= mysql_fetch_array($result))
{
$indexx = $row["indexx"];
$semester = $row["semester"];
$day = $row["day"];
$time = $row["time"];
$instructor = $row["instructor"];
$location = $row["location"];

echo "<table>";
echo "<tr>";
echo " <td>Index</td>";
echo "<td>Semester</td>";
echo "<td>Day</td>";
echo "<td>Time</td>";
echo "<td>Instructor</td>";
echo "<td>Location</td>";

echo "</tr>";
echo "<tr>";
echo "<td>$indexx</td>";
echo "<td> $semester</td>";
echo "<td>$day</td>";
echo "<td>$time</td>";
echo "<td>$instructor</td>";
echo "<td>$location</td>";
echo "</tr>";
echo"</table>";

$count++ ;

} // end WHILE

Your column headers are inside a while loop. Echo them before the loop and it should solve your problem.

[edit] Slow typing :(

lord_trini

10:13 pm on Apr 9, 2010 (gmt 0)

10+ Year Member



i am new to this so thanks for the help... i am working on it...

lord_trini

4:57 am on Apr 10, 2010 (gmt 0)

10+ Year Member



I am not trying to be spoon feed but can can someone actually post the modifications suggested. I cant seem to get it to work...

rocknbil

7:26 pm on Apr 11, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Eliminate your numrows query and the "if" following it. You don't need it, there are two reasons why, which should be evident with the code below.

After doing that, you will see the problem you have here, $query and $q are two unrelated variables.

$query = "SELECT * FROM schedule WHERE course_id = '". $_GET['id'] ."' ORDER BY indexx ASC";
$q .= " limit $s,$limit";


var $class_rows=NULL;
while ($row= mysql_fetch_array($result)) {
$indexx = $row["indexx"];
$semester = $row["semester"];
$day = $row["day"];
$time = $row["time"];
$instructor = $row["instructor"];
$location = $row["location"];
$class_rows .= "
<tr>
<td>$indexx</td>
<td> $semester</td>
<td>$day</td>
<td>$time</td>
<td>$instructor</td>
<td>$location</td>
</tr>
";

$count++; // No idea why you need this.
// but it could easily replace any value
// you'd get from num_rows
} // end WHILE
//
if ($class_rows) {
echo "
<table>
<tr>
<td>Index</td>
<td>Semester</td>
<td>Day</td>
<td>Time</td>
<td>Instructor</td>
<td>Location</td>
</tr>
$class_rows
</table>
";
}
else { echo "<p>This course does not have a set schedule.</p>"; }