Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

include() problem

Status
Not open for further replies.

martinb7

Programmer
Joined
Jan 5, 2003
Messages
235
Location
GB
Hi, i am working on this 2page site that work together, can you tell me whats wrong?

whats happening is that when i use the include(); it places it at the top of the page instead of in the table where i want it.

##code for dotemplate.php##
Code:
<?php 
$main = &quot;testing&quot;;
$logo = &quot;logo&quot;;
$left = include(&quot;navleft.php&quot;);
$right = include(&quot;middlenav.php&quot;);

$pagehtml = implode(&quot;&quot;, file(&quot;template.template&quot;)); // for change layout use &quot;.$_SESSION['template'].'&quot;
           $pagehtml = str_replace(&quot;[## titlebar ##]&quot;, $logo, $pagehtml);
           $pagehtml = str_replace(&quot;[## copyright2 ##]&quot;, $copyright, $pagehtml);
           $pagehtml = str_replace(&quot;[## leftnav ##]&quot;, $left, $pagehtml);
           $pagehtml = str_replace(&quot;[## main ##]&quot;, $main, $pagehtml);
           $pagehtml = str_replace(&quot;[## rightnav ##]&quot;, $right ,$pagehtml);

echo($pagehtml);
?>

##code for template.template##
Code:
<html>

<head>
<meta http-equiv=&quot;Content-Language&quot; content=&quot;en-gb&quot;>
<meta name=&quot;GENERATOR&quot; content=&quot;Microsoft FrontPage 5.0&quot;>
<meta name=&quot;ProgId&quot; content=&quot;FrontPage.Editor.Document&quot;>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=windows-1252&quot;>
<title>New Page 1</title>
</head>

<body>

<table border=&quot;1&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; style=&quot;border-collapse: collapse&quot; bordercolor=&quot;#111111&quot; width=&quot;800&quot; id=&quot;AutoNumber1&quot;>
  <tr>
  <td width=&quot;100%&quot;>[## titlebar ##]</td>
  </tr>
  <tr>
  <td width=&quot;200&quot;>[## leftnav ##]</td>
  <td width=&quot;400&quot;>[## middle ##]</td>
  <td width=&quot;200&quot;>[## rightnav ##]</td>
  </tr>
  <tr>
  <td width=&quot;800&quot;>[## copyright ##]</td>
  </tr>
</table>

</body>

</html>

website url:


im trying to create the same effect as

any ideas?? am i doing it the right way??

thanx


Martin

Computing help and info:

 
$left = include(&quot;navleft.php&quot;);
$right = include(&quot;middlenav.php&quot;);

navleft.php and middlenav.php should have only functions... if you have them as normal php files, they will be showed as normal pages. So, you should have:

Code:
#navleft.php
<?php
   function navleft(){
     code..
   }
?>

#main.php
   $left = include(&quot;navleft.php&quot;);
   code
   ....
   navleft();
   ....
   code

Hope this helps.
 
i cant get it to work :'(

it display's it in the same place:(

where do i put the navleft() function call?

thanx


Martin

Computing help and info:

 
sorry, I get it... the error is the following:

$left = include ('somthing') will assign to $left the result of &quot;include&quot; statement... now, &quot;include&quot; returns nothing or errors, *not the page* so $left is blank.

in leftnav you should do a funtion leftnav() in order to return the string (page) and then:

include 'leftnav.php';
$leftnav=leftnav();
 
sorry:( i still cant get it 2 work, could you please write out the dotemplate.php code 4 me??

sorry again!!

thanx


Martin

Computing help and info:

 
pls post the leftnav.php (or a piece) to get an example.
 
Code:
<?php function navleft(){ ?>
<?php include(&quot;pagecolors.php&quot;); ?>
<?php include(&quot;config.php&quot;); ?>
<?php include(&quot;functions.php&quot;); ?>

<link href=&quot;<?php echo($sitecss); ?>&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot;>
<body topmargin=&quot;0&quot; leftmargin=&quot;0&quot;>

<table width=&quot;200&quot; border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; bgcolor=&quot;<?php echo(&quot;$sideColor&quot;); ?>&quot;>
  <!--DWLayoutTable-->
  <tr> 
    <td width=&quot;200&quot; height=&quot;15&quot; valign=&quot;top&quot; bgcolor=&quot;<?php echo(&quot;$barColor&quot;); ?>&quot;><div align=&quot;center&quot;>Main 
        Pages </div></td>
  </tr>
  <tr> 
    <td height=&quot;14&quot; valign=&quot;top&quot; bgcolor=&quot;<?php echo(&quot;$sideColor&quot;); ?>&quot;><div align=&quot;center&quot;><font size=&quot;2&quot;> 
        <?php $query = mysql_query(&quot;SELECT * FROM wrV2pages WHERE section = 'main'&quot;); doWhileLoop($query); ?>
        </font></div></td>
  </tr>
  <tr> 
    <td height=&quot;15&quot;></td>
  </tr>
  <tr> 
    <td height=&quot;18&quot; valign=&quot;top&quot; bgcolor=&quot;<?php echo(&quot;$barColor&quot;); ?>&quot;><div align=&quot;center&quot;>Computing</div></td>
  </tr>
  <tr> 
    <td height=&quot;15&quot; valign=&quot;top&quot; bgcolor=&quot;<?php echo(&quot;$sideColor&quot;); ?>&quot;><div align=&quot;center&quot;><font size=&quot;2&quot;> 
        <?php $query = mysql_query(&quot;SELECT * FROM wrV2pages WHERE section = 'computing'&quot;); doWhileLoop($query); ?>
        </font></div></td>
  </tr>
  <tr> 
    <td height=&quot;13&quot;></td>
  </tr>
  <tr> 
    <td height=&quot;13&quot; valign=&quot;top&quot; bgcolor=&quot;<?php echo(&quot;$barColor&quot;); ?>&quot;><div align=&quot;center&quot;>Community</div></td>
  </tr>
  <tr> 
    <td height=&quot;15&quot; valign=&quot;top&quot; bgcolor=&quot;<?php echo(&quot;$sideColor&quot;); ?>&quot;><div align=&quot;center&quot;><font size=&quot;2&quot;> 
        <?php $query = mysql_query(&quot;SELECT * FROM wrV2pages WHERE section = 'community'&quot;); doWhileLoop($query); ?>
        </font></div></td>
  </tr>
  <tr> 
    <td height=&quot;17&quot;></td>
  </tr>
  <tr> 
    <td height=&quot;10&quot; valign=&quot;top&quot; bgcolor=&quot;<?php echo(&quot;$barColor&quot;); ?>&quot;><div align=&quot;center&quot;>Other</div></td>
  </tr>
  <tr> 
    <td height=&quot;14&quot; valign=&quot;top&quot; bgcolor=&quot;<?php echo(&quot;$sideColor&quot;); ?>&quot;><div align=&quot;center&quot;><font size=&quot;2&quot;> 
        <?php $query = mysql_query(&quot;SELECT * FROM wrV2pages WHERE section = 'other'&quot;); doWhileLoop($query); ?>
        </font></div></td>
  </tr>
</table>
</body>
<?php } ?>

Martin

Computing help and info:

 
The include statement includes PHP source code in the calling document. If your included PHP file has statements that produce output the generated output appears in the very place where the include statement is. Same is true for any HTML that it outside the <?php ?> tags.

You are trying to make your code modular. That's a great idea. As Chacalinc suggests, wrap your modules into functions. You can also consolidate them in a common include that you load at the top of every page that uses the common parts. The commoninclude can contain any number of functions you rely on in all scripts.
 
ok, may be this is not the best but here we go, I gonna make it short...

Code:
#leftnav.php

<?php 
	include(&quot;pagecolors.php&quot;);
	include(&quot;config.php&quot;);
	include(&quot;functions.php&quot;);
	
	function navleft(){
		//Note: all is one var
		$out = '<link href=&quot;' . $sitecss . '&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot;>';
		$out .= '<body topmargin=&quot;0&quot; leftmargin=&quot;0&quot;>';
		$out .= '<table width=&quot;200&quot; border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; bgcolor=&quot;\'.$sideColor.\'&quot;); ?>&quot;>';
		$out .= ...go ahead...
		
		$query = mysql_query(&quot;SELECT * FROM wrV2pages WHERE section = 'main'&quot;);
		
		//doWhileLoop($query);
		
		...make a loop in order to add all options lines to $out *not display them*
		
		$out .= '</tr>\n</table>\n</body>\n';

		return $out;
	} // end of navleft function
?>

now, in main.php:

Code:
<?php
	include &quot;leftnav.php&quot;;
	
	code here...
	...
	$left_nav=leftnav();  //it will have the code
	...
	code here ..
	printf $left_nav;
	...
?>

Hope this help.

PD: I would use OOP for this...
 
Let's say you wrap your left nav into a function:
Code:
function leftNav(){
   .... your code here ....
}
When you want the output in a specific place trigger the function:
[/code]
... any code ...
echo(&quot;<tr><td>&quot;);
# left nav insert here
leftNav();
... etc....[/code]

You could actually use the code you have in the include file and just modify it by adding the function structure around the include statement:
Code:
<?php
$main = &quot;testing&quot;;
$logo = &quot;logo&quot;;
# load code into navLeft()
function navLeft(){
   include(&quot;navleft.php&quot;);
}
# load code into navRight()
function navRight(){
   include(&quot;middlenav.php&quot;);
} ...
etc.

However, it is stylistically not very good to use the above solution. I suggest - as before - that you create the functions in a common include that is used site wide.
 
>>what is OOP??
Object Oriented Programming... php does support it. (search on as &quot;classes&quot;).

>>and is main.php the same as dotemplate.php??

yes, I forget the name of your main page..
 
I don't know what your tables have but an example:

Code:
$qry=mysql_query(&quot;SELECT * FROM clientes ORDER BY cliente&quot;)
     or die (&quot;Failed Query: &quot; . mysql_errno());

while ($row = mysql_fetch_array($qry)) {
   $out .= '<td bgcolor=&quot;' . $row[&quot;bgcolor&quot;] . '&quot;>' . $row[&quot;option&quot;] . '</td>';
   $out .= 'somthing more else..';
   }
 
oops... the page get the [ and ] ...

all $row are $row[ &quot;bgcolor&quot; ] with *no spaces*
 
ive tried a new layout


its basically the same apart from the layout is called template2.template

it replaces [## leftnav ##] with the page, but i cant get it to go ther, it gets replaced, but in the wrong place??

can u av a look??

thanx


Martin

Computing help and info:

 
I got:
Not Found
The requested URL /dolayout.php was not found on this server.
 
did you change leftnav.php?
how do you have it now?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top