Forum Moderators: coopster

Message Too Old, No Replies

using a variable as an include

is it do-able?

         

mylungsarempty

10:29 am on Dec 31, 2003 (gmt 0)

10+ Year Member



What i'm basically trying to achieve is this:

(not aiming for accuracy here, just an exlaination...)

<?

$now1 = (the name of a .php file here);

if(user clicks a particular link){

$now1 = (the value passed by the link)
}

?>

<A HREF="<?=PHP_SELF;?>">somehow also let the link pass the name of a specified .php file</A>

<TABLE><TR><TD><?

include('$now1');

?></TD></TR></TABLE>

i know that's kindof... erm... crappy... but does anyone get what i'm trying to accomplish?

everyone has been so helpful thus far. i really appreciate it.

travelbuff

3:16 pm on Dec 31, 2003 (gmt 0)

10+ Year Member



lungs, I am trying to follow you...

what you want is
1. page loads
2. user selects some option by clicking a link
3. page reloads and new link is set based on that option
Am I close?

Would the link that they clicked to set the option be on that same page, or another page? Could you do this with a dropdown menu instead?

What you will probably need to do is set some session variables or use GET to pass the variable through the url.

slade7

3:30 pm on Dec 31, 2003 (gmt 0)

10+ Year Member



If I'm not mistaken, when you set $now1 to the name of a file, and then you conditionally reset it to the value in a link, then $now1 is just the value in the link?

Lots of times I will do two or three different things with one basic script like this:

Say my main page is mainpage.php and I have two different header layouts for mainpage.php that I may want to show depending on what the user has indicated he would like to see by clicking or not clicking on a link that would pass a variable "var" (using var1 or var2 for values in this example) I'll make these header layouts seperate files for include ahead of time (header1.php and header2.php for this example)

mainpage.php would look like this:

<?php
// show links if no variable

if(!var){
echo "<a href=\"/mainpage.php?var=var1\">click here to see this</a>
<br>
or
<br>
<a href=\"/mainpage.php?var=var1\">click here to see that</a>";

//or show whatever the variable indicates
}elseif($var == "var1"){
include(header1.php);
echo "and whatever else";

}elseif($var == "var2"){
include(header2.php);
echo "and whatever else";

}else{
echo "You screwed up my script with a faulty variable and you must pay. Please wire \$10,000 US to $myswissbankacctno";
}
?>

of course, you could go a lot farther with the variable dependency, making the header files dependent on the variable, etc... but this is a nutshell version. Is this what you are looking to do?

mylungsarempty

12:15 am on Jan 1, 2004 (gmt 0)

10+ Year Member



Slade, i think that is exactly what i'm talking about. I will toy with that and let you know if it works out for me. Thankyou!

mylungsarempty

3:23 am on Jan 1, 2004 (gmt 0)

10+ Year Member



That worked out perfectly for me. I'm glad you knew what i was saying... this will save me so much time on my site in the long run . . . Thankyou

slade7

3:57 pm on Jan 1, 2004 (gmt 0)

10+ Year Member



Yer Welcome - I used to try to manage 700+ static pages on one site, so php is well worth the time it takes to learn for me. There's almost ALWAYS a way to make one less page w/ php.

You might consider this: that code I think is dependant on register_globals being on in php. It's really a better idea to code things so that they will work with register_globals being off - that means when you are passing variables thru the url, you'll have to $_GET the variable you are passing thru the url before it will be set for the script. I have not done things this way very much, but a lot of other users here have and can tell you the most efficient way to handle that. If I remember correctly, you can add one line at the top of the script to get the variable, and I think it applies to forms as well - you'd have to add lines to set every form variable before you can use them.

Somebody correct me if I'm wrong. - Thanks.