ShoeLace1291 Posted September 13, 2007 Share Posted September 13, 2007 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'); Any help would be greatly appreciated. [/code] Quote Link to comment https://forums.phpfreaks.com/topic/69161-using-a-variable-to-include-a-file-later/ Share on other sites More sharing options...
Wuhtzu Posted September 13, 2007 Share Posted September 13, 2007 $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. Quote Link to comment https://forums.phpfreaks.com/topic/69161-using-a-variable-to-include-a-file-later/#findComment-347607 Share on other sites More sharing options...
ShoeLace1291 Posted September 13, 2007 Author Share Posted September 13, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/69161-using-a-variable-to-include-a-file-later/#findComment-347608 Share on other sites More sharing options...
Wuhtzu Posted September 13, 2007 Share Posted September 13, 2007 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>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/69161-using-a-variable-to-include-a-file-later/#findComment-347613 Share on other sites More sharing options...
ShoeLace1291 Posted September 13, 2007 Author Share Posted September 13, 2007 Hmm, never thought of it that way. So then if my function to fetch the games from the database table were function get_games(){ dbcodes } then what whould I use to display that function? Quote Link to comment https://forums.phpfreaks.com/topic/69161-using-a-variable-to-include-a-file-later/#findComment-347615 Share on other sites More sharing options...
Wuhtzu Posted September 13, 2007 Share Posted September 13, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/69161-using-a-variable-to-include-a-file-later/#findComment-347616 Share on other sites More sharing options...
Kurrel Posted September 13, 2007 Share Posted September 13, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/69161-using-a-variable-to-include-a-file-later/#findComment-347618 Share on other sites More sharing options...
ShoeLace1291 Posted September 13, 2007 Author Share Posted September 13, 2007 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; } } Quote Link to comment https://forums.phpfreaks.com/topic/69161-using-a-variable-to-include-a-file-later/#findComment-347630 Share on other sites More sharing options...
ShoeLace1291 Posted September 13, 2007 Author Share Posted September 13, 2007 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; Quote Link to comment https://forums.phpfreaks.com/topic/69161-using-a-variable-to-include-a-file-later/#findComment-347631 Share on other sites More sharing options...
Kurrel Posted September 13, 2007 Share Posted September 13, 2007 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; } Quote Link to comment https://forums.phpfreaks.com/topic/69161-using-a-variable-to-include-a-file-later/#findComment-347655 Share on other sites More sharing options...
ShoeLace1291 Posted September 13, 2007 Author Share Posted September 13, 2007 Now it just displays the word "Array". Quote Link to comment https://forums.phpfreaks.com/topic/69161-using-a-variable-to-include-a-file-later/#findComment-347973 Share on other sites More sharing options...
Kurrel Posted September 14, 2007 Share Posted September 14, 2007 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>"); } Quote Link to comment https://forums.phpfreaks.com/topic/69161-using-a-variable-to-include-a-file-later/#findComment-348199 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.