ShoeLace1291 Posted September 14, 2007 Share Posted September 14, 2007 I have a function that is supposed to fetch all the results from my table "games". There are two database records in that table. My function is supposed to display all of the records, but it only displays one. I also use a function to display a certain template. Here is the coding for both functions and my index. Index.php: <?php include('config.php'); include('functions.php'); $getgames = get_games(); $getnews = get_news(); $temps = array( 'GAMESLIST' => $getgames, 'DISPLAYNEWS' => $getnews ); $file = "themes/default/index_body.tpl"; $setindex = set_template($file, $temps); echo $setindex; ?> Functions.php <?php function set_template($file, $temps){ $buildtemp = file_get_contents($file); foreach($temps as $template_var => $replacement_var){ $buildtemp = str_replace($template_var, $replacement_var, $buildtemp); } return $buildtemp; } function generate_actcode($long=7) { $chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; mt_srand((double)microtime()*1000000); $i=0; while ($i != $long) { $rand=mt_rand() % strlen($chars); $tmp=$chars[$rand]; $pass=$pass . $tmp; $chars=str_replace($tmp, "", $chars); $i++; } return strrev($pass); } function get_games(){ $query = mysql_query("SELECT gameID,gameTitle,gameDescription FROM games ORDER BY gameID"); while($fetch=mysql_fetch_array($query)){ $gameID=$fetch["gameID"]; $gameTitle=$fetch["gameTitle"]; $gameDescription=$fetch["gameDescription"]; $temps = array( 'GAMEID' => $gameID, 'GAMETITLE' => $gameTitle, 'GAMEDESC' => $gameDescription ); $file="themes/default/game_list.tpl"; $setgames = set_template($file, $temps); return $setgames; } return $data; } function get_news(){ $query = mysql_query("SELECT newsID,authorID,newsTitle,newsContent,newsDate FROM news ORDER BY newsID"); while($fetch=mysql_fetch_array($query)){ $newsID=$fetch["newsID"]; $authorID=$fetch["authorID"]; $newsTitle=$fetch["newsTitle"]; $newsContent=$fetch["newsContent"]; $newsDate=$fetch["newsDate"]; $temps = array( 'newsID' => $newsID, 'authorID' => $authorID, 'newsTitle' => $newsTitle, 'newsContent'=> $newsContent, 'newsDate' => $newsDate ); $file="themes/default/news_body.tpl"; $setnews = set_template($file, $temps); return $setgames; } } ?> Any help would be greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/69268-function-to-fetch-database-results/ Share on other sites More sharing options...
marcus Posted September 14, 2007 Share Posted September 14, 2007 Try: foreach($temps AS $temp){ $setgames .= set_template($file,$temp); } Quote Link to comment https://forums.phpfreaks.com/topic/69268-function-to-fetch-database-results/#findComment-348073 Share on other sites More sharing options...
Jessica Posted September 14, 2007 Share Posted September 14, 2007 You have a return in your while loop, which will end the loop. You need to keep adding records to an array and then return that whole array when the loop is over. Also, indenting your code more neatly will help. Quote Link to comment https://forums.phpfreaks.com/topic/69268-function-to-fetch-database-results/#findComment-348075 Share on other sites More sharing options...
ShoeLace1291 Posted September 14, 2007 Author Share Posted September 14, 2007 This is what I have so far but it doesn't seem to work. function get_games(){ $query = mysql_query("SELECT gameID,gameTitle,gameDescription FROM games"); $numgames = mysql_num_rows($query); $i = 0; while($i != $numgames){ while($fetch=mysql_fetch_array($query)){ $gameID=$fetch["gameID"]; $gameTitle=$fetch["gameTitle"]; $gameDescription=$fetch["gameDescription"]; $temps = array( 'GAMEID' => $gameID, 'GAMETITLE' => $gameTitle, 'GAMEDESC' => $gameDescription ); $file="themes/default/game_list.tpl"; $setgames = set_template($file, $temps); $game[$i] = $setgames; $i = $i + 1; } return $game[$i]; } } Quote Link to comment https://forums.phpfreaks.com/topic/69268-function-to-fetch-database-results/#findComment-348076 Share on other sites More sharing options...
ShoeLace1291 Posted September 14, 2007 Author Share Posted September 14, 2007 Ok, I figured that out, but now I'm back to my original problem. Using echo to display the list just displays it at the top of the page since it's in the file that's included at the beginning of the coding. Here's my code: function get_games(){ $query = mysql_query("SELECT gameID,gameTitle,gameDescription FROM games"); $numgames = mysql_num_rows($query); for ($i = 0; $i < $numgames; $i++){ while($fetch=mysql_fetch_array($query)){ $gameID=$fetch["gameID"]; $gameTitle=$fetch["gameTitle"]; $gameDescription=$fetch["gameDescription"]; $temps = array( 'GAMEID' => $gameID, 'GAMETITLE' => $gameTitle, 'GAMEDESC' => $gameDescription ); $file="themes/default/game_list.tpl"; $setgames = set_template($file, $temps); $game[$i] = $setgames; return $game[$i] . ' '; } } Quote Link to comment https://forums.phpfreaks.com/topic/69268-function-to-fetch-database-results/#findComment-348093 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.