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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

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

Post 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! :)
User avatar
werlop
Forum Commoner
Posts: 68
Joined: Sat Mar 22, 2003 2:50 am
Location: /dev/null

Post 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 :)
jmarcv
Forum Contributor
Posts: 131
Joined: Tue Jul 29, 2003 7:17 pm
Location: Colorado

Post 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.
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post 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
Post Reply