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. i you can edit the code i posed please please do... i am new at this...

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


?>

Recommended Answers

All 10 Replies

You placed the complete table inside the while loop. That means there will be a new table every time the while loop executes. The way to do it is to take the <table> declaration and your table header out of the while loop.
Don't know where you use count for? To display the results you don't need to store your result in new variables first. See here:

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

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

while ($row= mysql_fetch_array($result)) {
	echo "<tr><td>".$row['indexx']."</td>
		<td>".$row['semester']."</td>
		<td>".$row['day']."</td>
		<td>".$row['time']."</td>
		<td>".$row['instructor']."</td>
		<td>".$row['location']."</td></tr>";
		$count++ ;
} // end WHILE

echo "</table><br/>End test link<br/>";
Member Avatar for diafol

Like colweb. I'd go a bit further with the table (but just my personal preference with \n and \t - just parses 'pretty' html):

echo "<table>\n\t<thead>\n\t\t<tr>\n\t\t\t<td>Index\n\t\t\t</td>\n\t\t\t<td>Semester\n\t\t\t</td>\n\t\t\t<td>Day\n\t\t\t</td>\n\t\t\t<td>Time\n\t\t\t</td>\n\t\t\t<td>Instructor\n\t\t\t</td>\n\t\t\t<td>Location\n\t\t\t</td>\n\t\t</tr>\n\t</thead>\n\t<tbody>";

while ($row= mysql_fetch_array($result)) {
	echo "\n\t\t<tr>\n\t\t\t<td>{$row['indexx']}\n\t\t\t</td>\n\t\t\t<td>{$row['semester']}\n\t\t\t</td>\n\t\t\t<td>{$row['day']}\n\t\t\t</td>\n\t\t\t<td>{$row['time']}\n\t\t\t</td>\n\t\t\t<td>{$row['instructor']}\n\t\t\t</td>\n\t\t\t<td>{$row['location']}\n\t\t\t</td>\n\t\t</tr>";
}
echo "\n\t</tbody>\n</table>";

Ardav, I must agree it makes good readable html code. But the php code becomes unreadable. And it is the php code we are working with....

thanks guys... i am trying it now... and i will let u know how it goes

Member Avatar for diafol

Ardav, I must agree it makes good readable html code. But the php code becomes unreadable. And it is the php code we are working with....

Fair point. As I said - just a personal preference. Templating and heredoc syntax are much easier, but I like escaping \n\t - I'm just weird that way.:icon_rolleyes:

I added the code as suggested but i get an error message.

this is how i places it...

<?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");

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

while ($row= mysql_fetch_array($result)) {
	echo "<tr><td>".$row['indexx']."</td>
		<td>".$row['semester']."</td>
		<td>".$row['day']."</td>
		<td>".$row['time']."</td>
		<td>".$row['instructor']."</td>
		<td>".$row['location']."</td></tr>";
		$count++ ;
} // end WHILE

echo "</table><br/>End test link<br/>";

?>

Can you please tell us what the error message is?

Sorry stupid of me to not post the error message...

Parse error: syntax error, unexpected $end in /xxx/testermax.php on line 355

line 355 is the last line of the code and contains list a plain html tag.

thanks for the help so far...

Oh and this the the php

<?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");

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

while ($row= mysql_fetch_array($result)) {
	echo "<tr><td>".$row['indexx']."</td>
		<td>".$row['semester']."</td>
		<td>".$row['day']."</td>
		<td>".$row['time']."</td>
		<td>".$row['instructor']."</td>
		<td>".$row['location']."</td></tr>";
		$count++ ;
} // end WHILE

echo "</table><br/>End test link<br/>";

?>
Member Avatar for diafol

You haven't closed the first 'if brace'. Add '}' just before the ?>.

What a rookie mistake... thanks guys... i will try to see who i can help out on the forum. Everything works...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.