Jump to content

Using a Variable To Include a File Later


ShoeLace1291

Recommended Posts

How do I use a variable to include a file later on in the coding?  For example, I want $file = include('includes/getgames.php'); but I don't want it to display it at that point in the coding.  When I use file_get_contents, it just displays the text from the document.  Here is my code:

 

Index.php:

<?php

$readfile = include("includes/getgames.php");

$temps = array(
	  		  'GAMESLIST' => $readfile
			  );
			  
$displaygames = file_get_contents("themes/default/index_body.tpl");

      			 foreach($temps as $template_var => $replacement_var){

         		 				$displaygames= str_replace($template_var, $replacement_var, $displaygames);

         }

    echo $displaygames;

?>

Getgames.php:
[code]<?php

$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
			  );
			  

$buildtemp = file_get_contents("themes/default/game_list.tpl");

      			 foreach($temps as $template_var => $replacement_var){

         		 				$buildtemp = str_replace($template_var, $replacement_var, $buildtemp);

         }

	  echo $buildtemp;
}

?>

 

Index_body.tpl:

	 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">

<html>
<head>
  <title>Your Site Name- Some catchy slogan.</title>
  <link href="themes/default/default.css" type="text/css" rel="stylesheet">
</head>

<body>
  <div id="logodiv">
    Your Site Name
  </div>

  <div id="topmenu">
    Home | Help | Staff | Advertise | Contact
  </div>
  <div id='leftmenu'>
  Main Navigation<br>
  <ul>
  <li><a href='index.php'>Index</a></li>
  <li><a href='join.php'>Join</a></li>
  <li><a href='search.php'>Search</a></li>
  <li><a href='help.php'>Help</a></li>
  <li><a href='affiliates.php'>Affiliates</a>
  </ul><br>
  
  <hr width='50%' color='#3E606F'></hr>
  
  Battle Grounds<br>
  <ul>
  
  GAMESLIST</ul>

 

This is what happens when I use $file = include('includes/getgames.php');

laddersec1.jpg

Any help would be greatly appreciated.

 

[/code]

Link to comment
https://forums.phpfreaks.com/topic/69161-using-a-variable-to-include-a-file-later/
Share on other sites

$file = include() is somewhat meaningless.

 

You have 2 options to achieve what you want:

 

#1: Include the file when needed at a later point

 

#2: What ever code the included file contains make it to a function, include the file at the beginning and call the function later on.

I want to use something like file_get_contents but that just displays the acutal coding.  It doesn't display when you use $file = file_get_contents(file); only when you echo $file.  Is there something that I can use like that that will process the coding?

Here is an example:

include.php

<?php

if(something) {
  echo "Something";
}

if(something else) {
  echo "Something else";
}
?>

 

 

page.php

<?php

include('include.php');

echo "Something which should have been displayed before the stuff in include.php";

?>

 

 

This result in "Something" and "Something else" being displayed to early. Now what would be smart is to make your code in include.php into a function:

 

 

include.php

<?php

function do_something() {

  if(something) {
    echo "Something";
  }

  if(something else) {
    echo "Something else";
  }
?>

 

 

Now you will be able to neatly include your file at the top but use / call the code (function) later in your script:

 

page.php

<?php

include('include.php');

echo "Something which should have been displayed before the stuff in include.php";

do_something();

?>

 

 

Thats the way to do it...

 

 

The function does not have to be PHP.. it could be creating a table or menu or something in HTML:

 

alt function

<?php

function create_table(){
  echo "<table>";
  echo "<tr>";
  echo "<td></td>";

}

?>

 

What ever is in your getgames.php right now will basically (maybe with minor modifications) go into your function:

 

<?php

function getgames() {

  connect to db server
  select db

  mysql_query

  while($var = mysql_fetch...) {

    echo $var['game_name'];
    echo $var['description'];

  }

}

?>

 

I will not reply for the next 10 hours, I'm off to the university, just so you know why I'm not replying :)

Wuhtzu illustrated the point perfectly.  Expanding on it in your example, you could do the following (If I'm reading you right)

 

include.php

<?php
function pull_games($where) {
		$sql = "SELECT * FROM games_table WHERE ".$where;

		if ($resultset = mysql_query($sql)) {
			while ($line = mysql_fetch_array($resultset, MYSQL_ASSOC)) {
				$key = "";
				$key = $line["id"];
				$resultlist[$key] = $line;
			}

			return $resultlist;
		} else {
			echo 'Error pulling from SQL Database: ' . mysql_error() . "\n";
		}
}
?>

The above should query a database and return the results to the caller through $resultset.

 

And then in

 

code.php

<?php
include('include.php');

echo "Games List";

$gameslist = pull_games("1=1");

?>

 

$gameslist will then have all the results in it, and you can display it's contents in-function rather than through include.php.  Just use a foreach and run through the array to spew the data.

Hmm, I'm closer, but now it only displays one database result when there are two.  This is my getgames function:

 

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

$buildtemp = file_get_contents("themes/default/game_list.tpl");

      			 foreach($temps as $template_var => $replacement_var){

         		 				$buildtemp = str_replace($template_var, $replacement_var, $buildtemp);

         }

	 return $buildtemp;
}

}

If it's not my getgames function, then here's my index code:

include('functions.php');

$getgames = get_games();
  
$temps = array(
	  		  'GAMESLIST' => $getgames
			  );
			  
$file = "themes/default/index_body.tpl";
			  
  $setindex = set_template($file, $temps);
  
  echo $setindex;
			  

You've taken quite a different tack, but I think I can see what might be the cause.

 

Original Code

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

$buildtemp = file_get_contents("themes/default/game_list.tpl");

      			 foreach($temps as $template_var => $replacement_var){

         		 				$buildtemp = str_replace($template_var, $replacement_var, $buildtemp);

         }

	 return $buildtemp;
}

}

 

You're filling buildtemp with the details of one record, and then returning it.

 

Here's my take on it.

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

$buildtemp = file_get_contents("themes/default/game_list.tpl");

      			 foreach($temps as $template_var => $replacement_var){

         		 				$data[] = str_replace($template_var, $replacement_var, $buildtemp);

         }

}
return $data;
}

Okay, that should mean that the function get_games returned an array of data to the variable 'getgames'.

 

Try this

include('functions.php');

$getgames = get_games();

print("<pre>");
print_r($getgames);
print("</pre>");

$temps = array(
'GAMESLIST' => $getgames
);

print("<pre>");
print_r($temps);
print("</pre>");		  

$file = "themes/default/index_body.tpl";
$setindex = set_template($file, $temps);

print("<pre>");
print_r($setindex);
print("</pre>");
  
echo $setindex;

 

This won't fix the code but will show you the data structure you've pulled back into the variables 'getgames', 'temps' and 'setindex'.  I'm not certain what set_template function does, but you should have an array of all the results passed from one variable to the next, from there you can write a foreach loop and display the data.

 

If 'setindex' is a 2d array,

foreach ($setindex as $sikey=>$sival) {
print($sival."<br>");
}

should work, otherwise try

foreach ($setindex as $sikey=>$sival) {
foreach ($sival as $singlekey=>$singleval) {
  print($singleval);
}
print("<br>");
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.