PHP_Idiot Posted April 18, 2009 Share Posted April 18, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/154700-solved-displaying-query-results-in-a-table/ Share on other sites More sharing options...
JasonLewis Posted April 18, 2009 Share Posted April 18, 2009 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>"; Quote Link to comment https://forums.phpfreaks.com/topic/154700-solved-displaying-query-results-in-a-table/#findComment-813473 Share on other sites More sharing options...
PHP_Idiot Posted April 19, 2009 Author Share Posted April 19, 2009 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 tytytytyty Quote Link to comment https://forums.phpfreaks.com/topic/154700-solved-displaying-query-results-in-a-table/#findComment-813497 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.