Saturday, March 15, 2008

Variabel include link in PHP, use $_GET

This is my experience when I use link include variables inside them. Example I will open another page from my link and that page will open with the same page of index.php.
So I use a href="index.php?cont=file2">file2 /a> wrote in index.php.
"?" means that I set variable cont that the value is file2. Thats way, in index.php I write script for include file2 in the page.
the script is like this:
1. in index.php
href="index.php?cont=file2">file2

2. still in index.php that will placed content to load data from page file2 I write:
php
if(isset($cont)){
include("$cont");
}else{
include("home.php");
}
?>//
That's mean I will open again index.php but include file2 to open by condition if variabel cont is set before. If variable set is not arise, index will include home.php to open.

3. file2 content is file that content may be information, open database and list data, and will show at the place we want to place it in index.php page.

This is my experience, I have write the code like above and run on my computer where I design that. It's can be run well. But when I run to other server It's not run. For one days I try to find the trouble. I change the server by APPserv that include Web Server, MySQL, and MyPhpAdmim. The trouble can fix. I'm so glad can finish my job.

So I want to upload the program to the remote web server. But... ohhh.. the same problem arise, the program can't run well. I find that the variable set by ? can't read. So the programs never know that variable.

I try to find help in PHP, try to search for I can solve that problems by another way. The alternative is make a template, or set the variable into a database. But for some reasons this is not effective for my job case.

Try and try again. Finally I saw a code by that read variable by _GET. I try to use it even that was usually use by form method (get, post). I try this because I see the case of execution Get is the same like I write link by ?
(exp. http://www.myserver.com/index.php?id=data1&cont=file2> Oh
Oh.. may I can use that code to solve my problem.

Ok.. and finally I can see my program run. Oh.. I'm so happy.

The way is I must write php code and set variable first before my include file, and I used if(cont!="") to my a logical not isset().
Get Method.
php
$cont=$_GET['cont'];
if($cont!=""){
include("$cont");
}else{
include("home.php");
}
?>

The link is the same as first point.
a href="index.php?cont=file2">file2 /a>

but the code to run include is change with the last code above.

* Where is the exactly problem so that's not run well. I think the problems is in PHP program. For the code I write first, it's will run well when I used PHP version 5 above. But in Version 4 and under, may I will the same problem.

But.. it's ok because I can use another way and can run in my remote place. But you can try another way like request() , and require. I see that it's run too to read variable as ? link set.

No comments: