Page 1 of 1

Passing a variable to an included php file WITHOUT a form...

Posted: Fri Aug 01, 2003 3:29 pm
by robster
Hi all,

I've got a simple few lines of code that I use regularly for the boxing around some menus.

The code looks like this:

Code: Select all

<tr> 
<td width="20" align="left" valign="top"  class="td2"><img src="images/news_bar_end_left.gif"></td>
<td width="100%" align="center" valign="top"  class="td2">
<?
//get the variable from the page that will call this code
$boxtoptext   = $_GET['boxtop'];
print "$boxtoptext";
?>

</td>
<td width="20" align="right" valign="top"  class="td2"><img src="images/news_bar_end_right.gif"></td>
</tr>
I want to save this as a seperate file called "box_top.php".

In all the other files that call this file, I want this kind of thing to happen (without a form I presume)

Code: Select all

<? include("box_top.php?boxtop=News Options"); ?>
This would be an example where "News Options" was the text I wanted displayed.

Is there such a way to do this? I know it sounds a bit to good to be true, I'm sure it's going to be more complicated....

Thanks for reading, I hope somebody out there knows the answer! :)

Posted: Fri Aug 01, 2003 5:35 pm
by werlop
What I would do is make a function called boxtop like this:

Code: Select all

<?php

function boxtop( $boxtoptext ) {

?>

<tr> 
<td width="20" align="left" valign="top"  class="td2"><img src="images/news_bar_end_left.gif"></td> 
<td width="100%" align="center" valign="top"  class="td2"> 

<?php 
print "$boxtoptext"; 
?> 

</td> 
<td width="20" align="right" valign="top"  class="td2"><img src="images/news_bar_end_right.gif"></td> 
</tr> 

<?php
} // End of function
?>
to call it you would simpy do something like:

Code: Select all

<?php

boxtop( "News Options" );

?>
All you need to do is make sure the function decleration is in your code somewhere, and then you are free to call it.

David :)

Posted: Sat Aug 02, 2003 10:37 pm
by jmarcv
It's simpler than a function:

Code: Select all

<?php
<tr> 
<td width="20" align="left" valign="top"  class="td2"><img src="images/news_bar_end_left.gif"></td> 
<td width="100%" align="center" valign="top"  class="td2"> 
<? 
//get the variable from the page that will call this code 
print "$boxtoptext"; 
?> 

</td> 
<td width="20" align="right" valign="top"  class="td2"><img src="images/news_bar_end_right.gif"></td> 
</tr> 
?>

------------------

Code: Select all

$boxtoptext  ='News Options';
<? include("box_top.php"); ?>
All variables created in the parent are visible to the include.

Posted: Sun Aug 03, 2003 3:48 am
by robster
Thank you both so much! :)

All at once I feel silly (as it now seems so obvious) and thankful (for the wonderful people on these forums).

So yes, thank you all :)


Rob