Jump to content

[SOLVED] Displaying Query Results in a Table


PHP_Idiot

Recommended Posts

I hope this is in the right place!

 

I have the following query:

SELECT SUM(Position.Points), Player.FirstName, Player.LastName
FROM Position, Player, Results, Venue
WHERE Player.MembershipNo=Results.MembershipNo
AND Results.Position=Position.Position
AND Venue.VenueID = Results.VenueID
GROUP BY Player.FirstName
ORDER BY SUM(Position.Points) DESC

Which works and shows the results I need, but I'm stuggling to display it properly.

I need a dynamic table that is three columns wide (FirstName, LastName, SUM(Position.Points)) and as many rows long as is required.

 

I know I can use a simple loop to define number of rows, but I'm really struggling to get any of it to work! I can establish a connection and run the query, but can't figure out how to extract the data stored in $result.

 

I know this is probably pretty simple but after spending all afternoon searching the web and trying various things, I'm getting now where fast!

All you need is a while loop really:

 

$result = mysql_query("...");

//Now display the top of the table.
echo <<<html
<table border="0" width="600" cellpadding="2" cellspacing="1">
<tr>
	<td>First Name</td>
	<td>Last Name</td>
	<td>SUM</td>
</tr>
html;

//Now start the loop.
while($r = mysql_fetch_array($result)){
//and echo each new row
echo <<<html
<tr>
	<td>{$r['firstname']}</td>
	<td>{$r['lastname']}</td>
	<td>{$r['sum']}</td>
</tr>
html;
}

//And close the table.
echo "</table>";

Thank you so much for the help, I've had to make a minor change but it works a treat now, heres what I have now:

<?php
// Make a MySQL Connection
mysql_connect("localhost", "myusername", "mypassword") or die(mysql_error());
mysql_select_db("myDB") or die(mysql_error());


//mySQL queries
$query = "SELECT SUM(Position.Points), Player.FirstName, Player.LastName
FROM Position, Player, Results, Venue
WHERE Player.MembershipNo=Results.MembershipNo
AND Results.Position=Position.Position
AND Venue.VenueID = Results.VenueID
GROUP BY Player.FirstName
ORDER BY SUM(Position.Points) DESC"; 

$result=mysql_query($query)
	or die ("couldn't execute query");

echo <<<html
<table border="0" width="400" cellpadding="2" cellspacing="1">
<tr>
	<td>First Name</td>
	<td>Last Name</td>
	<td>SUM</td>
</tr>
html;

//Now start the loop.
while($r = mysql_fetch_array($result)){
//and echo each new row
echo <<<html
<tr>
	<td>{$r['FirstName']}</td>
	<td>{$r['LastName']}</td>
	<td>{$r['SUM(Position.Points)']}</td>
</tr>
html;
}

//And close the table.
echo "</table>";

?>

 

Thank you so much for all the help, you have no idea how big my grin is right now  ;D  ;D

 

tytytytyty

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.