Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/69268-function-to-fetch-database-results/
Share on other sites

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

}



}

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

     }

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.