Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.