ShoeLace1291 Posted September 19, 2007 Share Posted September 19, 2007 I have a function called "get_games()" that pulls database info from a mysql table. This table is called "games" and serves the purpose of storing games that people will want to create clan ladders for. I have this function stored in a file called "functions.php". My problem is this: since I don't want the results to dispay on the function page, only when it is called, I have to use return to do this. But return stops the looping of the database fetch. If I use "echo", the results are displayed at the top of my page since I use "include('functions.php');" to include these functions in my page. I know it's not my database coding that's my problem because when I change "return" to "echo", it displays it perfectly, except for the fact that it's at the top of the page, above all of my html and tables and such. Is there another way or another method of using return for loops? This is my function 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/69849-solved-still-stuck-on-this/ Share on other sites More sharing options...
claudej2007 Posted September 19, 2007 Share Posted September 19, 2007 Hello Shouldn't call your function when you include the fucntions.php file. Something is wrong there. what template engine do you use? maybe that is causing an issue. Do you have other stuff in function.php?I could try to look closer if you post some more details Thanks Read another interesting programming tips and tricks on my site: <a href="http://www.assistprogramming.com">Programming tips and tricks</a> Quote Link to comment https://forums.phpfreaks.com/topic/69849-solved-still-stuck-on-this/#findComment-350891 Share on other sites More sharing options...
ShoeLace1291 Posted September 19, 2007 Author Share Posted September 19, 2007 Well, this is my index.php page(where the problem is occuring). <?php include('config.php'); require_once('functions.php'); $getgames = get_games(); $getnews = get_news(); $getstats = get_stats(); $temps = array( 'GAMESLIST' => $getgames, 'DISPLAYNEWS' => $getnews, 'DISPLAYSTATS' => $getstats ); $file = "themes/default/index_body.tpl"; $setindex = set_template($file, $temps); echo $setindex; ?> This is 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"); $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]; } } } function get_news(){ $query = mysql_query("SELECT newsID,authorID,newsTitle,newsContent,newsDate FROM headlines ORDER BY newsID") or die("Error: ".mysql_error()); 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 $setnews; } } function get_gametypes($gid){ $query = mysql_query("SELECT ladderID,gameID,ladderName,ladderDescription FROM ladders WHERE gameID = '$gid' ORDER BY ladderID") or die("Error: ".mysql_error()); $numladders = mysql_num_rows($query); if($numladders < 1){ echo "There are no ladders for this game."; } else { while($fetch=mysql_fetch_array($query)){ $ladderID=$fetch["ladderID"]; $gameID=$fetch["gameID"]; $ladderName=$fetch["ladderName"]; $ladderDescription=$fetch["ladderDescription"]; $temps = array( 'ladderID' => $ladderID, 'gameID' => $gameID, 'ladderName' => $ladderName, 'ladderDesc' => $ladderDesc ); $file="themes/default/ladderslist_body.tpl"; $setladders = set_template($file, $temps); return $setladders; } } } function get_stats(){ $query = mysql_query("SELECT * FROM sg_players"); $numreg = mysql_num_rows($query); $query = mysql_query("SELECT * FROM matches"); $nummatches = mysql_num_rows($query); $query = mysql_query("SELECT * FROM matches WHERE matchTime = '$today'"); $matchestoday = mysql_num_rows($query); $temps = array( 'REGISTERED' => $numreg, 'TOTAL_MATCHES' => $nummatches, 'TODAYS_MATCHES' => $matchestoday ); $file="themes/default/stats_body.tpl"; $setstats = set_template($file, $temps); return $setstats; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/69849-solved-still-stuck-on-this/#findComment-350894 Share on other sites More sharing options...
claudej2007 Posted September 19, 2007 Share Posted September 19, 2007 Ok now I see what you problem is! You can just echo the "games" because you need them in a variable for sending to the template file. I see two work arounds: let's see the first. Mod the get_games() function to look like this: function get_games(){ $query = mysql_query("SELECT gameID,gameTitle,gameDescription FROM games"); $numgames = mysql_num_rows($query); ob_start(); 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; echo $game[$i]; } } $ret=ob_get_contents(); ob_end_clean(); return $ret; } I've used here the php output buffering functions. You can read a this complete tutorial about <a href="http://php.assistprogramming.com/php-buffer-output-put-contents-in-a-variable.html">PHP output buffering functions</a>. That should be it. now it would work as you want. let me know if you have troubles making this work and I'll help you. I won't present the other workaround just yet since I dont think that is necessary Cheers Claude Quote Link to comment https://forums.phpfreaks.com/topic/69849-solved-still-stuck-on-this/#findComment-350908 Share on other sites More sharing options...
ShoeLace1291 Posted September 19, 2007 Author Share Posted September 19, 2007 Woo! Yes! Thank you so much, claude. I've been trying to figure this out for the past four or five days but your mod worked perfectly! Thanks a bunch. This was the only thing stopping me from finishing the rest of my software. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/69849-solved-still-stuck-on-this/#findComment-350923 Share on other sites More sharing options...
claudej2007 Posted September 19, 2007 Share Posted September 19, 2007 Your wellcome;) I'll soon(1-2days) lunch a board on my site where ppl can ask questions and I'll personally do my best to help them. So you guys can try to get helped there too Quote Link to comment https://forums.phpfreaks.com/topic/69849-solved-still-stuck-on-this/#findComment-350930 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.