Jump to content

Variable to store an include statement?


perk3c251

Recommended Posts

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

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

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

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

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

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...