Jump to content

Recommended Posts

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];
	   
	   } 



 }



}

Link to comment
https://forums.phpfreaks.com/topic/69849-solved-still-stuck-on-this/
Share on other sites

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>

 

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;
			   
}

?>

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

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.

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.