perk3c251 Posted January 30, 2007 Share Posted January 30, 2007 Can I use a variable to store an include statement? If so what is the right syntax for it? Everything I have tried just calls the include and pretty much ignores the variable. Link to comment Share on other sites More sharing options...
aspnetguy Posted January 30, 2007 Share Posted January 30, 2007 I have never tried it but this should work $var = "file.php";include($var); Link to comment Share on other sites More sharing options...
perk3c251 Posted January 30, 2007 Author Share Posted January 30, 2007 That gave me the same result.I need something like this $s.=include('filepath'); I have tried many variations of the code, putting the quotes in different places, etc. Any other ideas? Link to comment Share on other sites More sharing options...
aspnetguy Posted January 30, 2007 Share Posted January 30, 2007 I doubt what you are tryign to do will work. use fopen() and physically open the file and read the contents into a variable. Link to comment Share on other sites More sharing options...
justsomeguy Posted January 30, 2007 Share Posted January 30, 2007 What are you trying to do? Including a file causes the code in that file to be executed, you can only save the result in a variable if the included file has a return statement in it. So, what are you trying to accomplish by assigning the results of an include to a variable? Link to comment Share on other sites More sharing options...
perk3c251 Posted January 30, 2007 Author Share Posted January 30, 2007 I am trying to include a file in the middle of a function. I am trying to mod a piece of Noahs Classifieds. The functions writes everything to the variable $s. . The problem I am having is when I include the file, it executes as soon as it reads it, and then the function returns $s. . I need a way to include the file in the function. I thought about writing another function that has the include in it, but only calling it from the first function. Would that even work? Tried using fopen, get_file_contents etc, but the file I am trying to include also has include statements and they end up not being read. Link to comment Share on other sites More sharing options...
justsomeguy Posted January 30, 2007 Share Posted January 30, 2007 Can you post the code that you're working with? Link to comment Share on other sites More sharing options...
perk3c251 Posted January 30, 2007 Author Share Posted January 30, 2007 function templateMain($userMenuStr,$adminMenuStr,$userStatus, $navBarText,$infoText,$gorumcontent,$footer){ global $leftads; global $xi; $s=""; $s.="<center>"; $s.="<table border='1' cellpadding='0' cellspacing='0'". " width='100%'>"; $s.="<tr><td colspan='3'>". "<img src='$xi/b.gif' height='20'></td></tr>"; $s.="<tr>"; $s.="<td width='10%'>";//left side ads[b]$s.=include('ads/leftsideads.php');[/b] <-------------------------------------------Here is where I am trying to insert the include $s.="</td>";//left $s.="<td valign='top' align='center' width='80%'>"; $s.="<table border='0' cellpadding='0' cellspacing='0'>"; $s.="<tr><td><img src='$xi/headpic.gif' width='705' height='61'>"; $s.="</td></tr>"; $s.="<tr><td align='center'><h3>Proudly serving the men"; $s.=" and women in Camp Victory, Iraq"; $s.="</td></tr></table>\n"; $s.=vertSpacer(); $s.="<span class='userstatus'>$userStatus</span>\n"; $s.=vertSpacer("3"); $s.=$userMenuStr;//lot of code return $s;} What ends up displaying is a 1 showing that it did execute the include, but I think it is executing it as soon as it reaches that part, instead of waiting until $s. is returned. The file does get displayed just on top of the table instead of inside that cell. Link to comment Share on other sites More sharing options...
justsomeguy Posted January 30, 2007 Share Posted January 30, 2007 OK, there are two ways to solve this. One way would be to change the include file. If you can change the include file and not screw up other pages, that would probably be preferred. If changing that file will mess up other things, then you can use output buffering to solve this. So, the question now is would you rather change the include file or leave it? If you want to change it, post a short sample of the code that's in it. Link to comment Share on other sites More sharing options...
perk3c251 Posted January 30, 2007 Author Share Posted January 30, 2007 Here is leftside ads <table bgcolor=#6E7382 border=0 cellpadding=3 cellspacing=0><tr><td><?include 'ad1.php';?></td></tr><tr><td><?include 'ad2.php';?></td></tr><tr><td><?include 'ad3.php';?></td></tr><tr><td><?include 'ad4.php';?></td></tr><tr><td><?include 'ad19.php';?></td></tr><tr><td><?include 'ad20.php';?></td></tr></table> Here is ad1. They are all identical, just query different fields. <?include './ads/config.php';include './ads/opendb.php';$query = "SELECT ad1 FROM ads";$result = mysql_query($query);while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ echo "{$row['ad1']}" ; } print mysql_error();include './ads/closedb.php';?> I know this is probably the worst coding ever seen, but I only starting writing php and sql queries about 3 days ago, so dont bash me too hard. I think I would rather not change the include file though. Link to comment Share on other sites More sharing options...
Mr_CHISOL Posted January 30, 2007 Share Posted January 30, 2007 Got this at the include man-page: <?php$string = get_include_contents('somefile.php');function get_include_contents($filename) { if (is_file($filename)) { ob_start(); include $filename; $contents = ob_get_contents(); ob_end_clean(); return $contents; } return false;}?> It works like a charm and I've used it several times. Link to comment Share on other sites More sharing options...
justsomeguy Posted January 30, 2007 Share Posted January 30, 2007 Since you don't want to change the include file, and doing so would be fairly time-consuming in this case, the output buffering function above is definately the way to go. Link to comment Share on other sites More sharing options...
perk3c251 Posted January 30, 2007 Author Share Posted January 30, 2007 Ok, but I am a bit confused on how to use that. Do I have to define $filename? Would I call the function using $s.=get_include_contents($filename) or $s.=$string ? Link to comment Share on other sites More sharing options...
perk3c251 Posted January 30, 2007 Author Share Posted January 30, 2007 Nevermind, I figured it out. I used $s.=$string, just forgot to add it to global.You guys absolutely rock. I would have never figured that one out on my own. Thanks again very very much. Link to comment Share on other sites More sharing options...
Mr_CHISOL Posted January 30, 2007 Share Posted January 30, 2007 You don't need to make anything global...Just use $s .= get_include_contents('ads/leftsideads.php'); Just make sure that the function is "included" somewhere... Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now