Cep Posted January 22, 2007 Share Posted January 22, 2007 Hello,I have a function that creates a string of html with values taken from my database.The function does this by taking in a date, querying the database game table for all games with that date and then querying the results table for all results with that date.The function will then create the string by looping through each game. Within each loop for each game I then loop through all the results that match the current game id.However, when my results are displayed I get the first game and its results, and then the second game but none of its results appear.This is the function,[code]<?php//***************************************** get_lastgames function ********************************************//function get_lastgames($lastsunday) { $gsql = "SELECT * FROM cw_games WHERE gamedate = '{$lastsunday}'"; $gresult = mysql_query($gsql, db()) or die("Function: get_lastgames<br />SELECT gsql: <br />{$gsql}<br /> SQL ERROR: ".mysql_error()); $gnum_rows = mysql_num_rows($gresult); $rsql = "SELECT cw_games.game_id as game_id, cw_games.gamedate, cw_games.starttime, cw_games.endtime, cw_games.stakes, cw_players.name, cw_players.pokername, cw_results.finalposition, cw_results.outhand, cw_results.gps, cw_players_1.name AS outby FROM cw_players AS cw_players_1 RIGHT JOIN ((cw_results LEFT JOIN cw_games ON cw_results.game_id = cw_games.game_id) LEFT JOIN cw_players ON cw_results.player_id = cw_players.player_id) ON cw_players_1.player_id = cw_results.outby_id WHERE cw_games.gamedate = '{$lastsunday}' ORDER BY cw_results.finalposition"; $rresult = mysql_query($rsql, db()) or die("Function: get_lastgames<br />SELECT rsql: <br />{$rsql}<br /> SQL ERROR: ".mysql_error()); $rnum_rows = mysql_num_rows($rresult); $latest_results = ""; for ($i = 1; $i <= $gnum_rows; $i++) { $grow = mysql_fetch_array($gresult, MYSQL_ASSOC); $latest_results .= "<tr> <td colspan='5'> Date: {$grow['gamedate']} Time: {$grow['starttime']} to {$grow['endtime']} Stakes: £{$grow['stakes']} </td> </tr>"; $gameid = $grow['game_id']; $playercount = 0; for($r = 1; $r <= $rnum_rows; $r++) { $rrow = mysql_fetch_array($rresult, MYSQL_ASSOC); if ($rrow['game_id']==$gameid) { $latest_results .= "<tr> <td> {$rrow['name']} </td> <td> {$rrow['finalposition']} </td> <td> {$rrow['outhand']} </td> <td> {$rrow['outby']} </td> <td> {$rrow['gps']} </td> </tr>"; $playercount++; } } $winnings = $grow['stakes'] * $playercount; $latest_results .= "<tr> <td colspan='5'> Total Winnings: £{$winnings} </td> </tr>"; }return $latest_results;}?>[/code]And here is the output source[code]<html><head></head><body> <div id="centerbox" align="center"> <table class="edge" width="80%"> <tr> <td> <table class="tbl" width="100%"> <tr> <td class="heading"> <h3>Latest Games</h3> </td> </tr> <tr> <td class="main"> <!-- latest_results.tpl start --><table class="textcenter" width="100%"> <tr> <th> Player </th> <th> Position </th> <th> Out Hand </th> <th> Out By </th> <th> gps </th> </tr> <tr> <td colspan='5'> Date: 2007-01-07 Time: 19:07:00 to 20:45:00 Stakes: £5 </td> </tr><tr> <td> Karl </td> <td> 1 </td> <td> 0 </td> <td> </td> <td> 0 </td> </tr><tr> <td> Bale </td> <td> 2 </td> <td> 28 </td> <td> Karl </td> <td> 0 </td> </tr><tr> <td> Plummy </td> <td> 3 </td> <td> 26 </td> <td> Bale </td> <td> 0 </td> </tr><tr> <td> Matt </td> <td> 4 </td> <td> 17 </td> <td> Karl </td> <td> 1 </td> </tr><tr> <td> David </td> <td> 5 </td> <td> 11 </td> <td> Bale </td> <td> 0 </td> </tr><tr> <td> Wurzel </td> <td> 6 </td> <td> 10 </td> <td> Karl </td> <td> 1 </td> </tr><tr> <td> Poultney </td> <td> 7 </td> <td> 1 </td> <td> Karl </td> <td> 1 </td> </tr><tr> <td colspan='5'> Total Winnings: £35 </td> </tr><tr> <td colspan='5'> Date: 2007-01-07 Time: 20:50:00 to 21:54:00 Stakes: £5 </td> </tr><tr> <td colspan='5'> Total Winnings: £0 </td> </tr></table><!-- latest_results.tpl end --> </td> </tr> </table> </td> </tr> </table> </div></div></body></html>[/code]Please ignore the fact the output isn't complete (no css dtd etc) I just added the body tags etc so you can see it easier. Link to comment https://forums.phpfreaks.com/topic/35227-loop-is-not-returning-all-the-results-from-sql-statements/ Share on other sites More sharing options...
Cep Posted January 22, 2007 Author Share Posted January 22, 2007 Anyone please? Is even the logic right?ps. whats the bbcode for getting the above boxes that redbullmarky put in? Link to comment https://forums.phpfreaks.com/topic/35227-loop-is-not-returning-all-the-results-from-sql-statements/#findComment-166432 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.