Jump to content

tabulating sql query results


sonnyboy

Recommended Posts

Hello folk I have a football league table and I'm trying to number my rows in the column named "Pos" but so far I not getting any success, someone please bail me out find below my script, this script displays my results fine but without my Positions (Pos);

 

<?php
function printTable($result)
{	
$i=0;
//get the first row in the table:
if(!$row = mysql_fetch_assoc($result))
{
	return false;
}

//set up the table:
print("<table><tr>");
print ("<th>Pos</th>");

//print the column names as table headers:
foreach($row as $key=>$value)
{
	print("<th>$key</th>");
}
print("</tr>");

//loop through the table, printing
//the field values in table cells:
do
{
	$i++;
	print("<tr>");
	print ("<td>$i</td>");
	foreach($row as $key=>$value)
	{
		print("<td>$value</td>");
	}
	print("</tr>");
}
while ($row = mysql_fetch_assoc($result));

//close out the table:
print("</tr></table>");
return true;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/45633-tabulating-sql-query-results/
Share on other sites

Running your code on a table of mine gave

 

[pre]

Pos date        location    qty  value    area id

1  2003-01-08  Ashton      542    4216.87  E  1

2  2003-01-08  Bolton      317    2896.7    N  2

3  2003-01-08  Bury        1089  15161.2    N  3

4  2003-01-08  Oldham      866    7607.68  E  4

5  2003-01-08  Piccadilly  1106  25598.7    C  5

[/pre]

 

What do you expect?

 

 

Myfull code (the table shouldn't matter as it should work for any)

 

<?php
include 'db.php';

function printTable($result)
{	
$i=0;
//get the first row in the table:
if(!$row = mysql_fetch_assoc($result))
{
	return false;
}

//set up the table:
print("<table><tr>");
print ("<th>Pos</th>");

//print the column names as table headers:
foreach($row as $key=>$value)
{
	print("<th>$key</th>");
}
print("</tr>");

//loop through the table, printing
//the field values in table cells:
do
{
	$i++;
	print("<tr>");
	print ("<td>$i</td>");
	foreach($row as $key=>$value)
	{
		print("<td>$value</td>");
	}
	print("</tr>");
}
while ($row = mysql_fetch_assoc($result));

//close out the table:
print("</tr></table>");
return true;
}
$res = mysql_query("SELECT * FROM baagriddata");
printTable($res);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.