Page 1 of 1

Php get function

Posted: Tue Nov 17, 2009 10:49 pm
by gippo
I want to make my website like.

Code: Select all

http://sitename.com/?=guide
And it will show the guide. And so on.

I have read some tutorials on the $_get[] functions. But I do no't know how to use it for linking other php but using the "?=" instead of "guide.php"

Re: Php get function

Posted: Wed Nov 18, 2009 1:06 am
by angelicodin
Hi and welcome com phpdn.

With the get method it's all a matter of making sure you are setting and getting the data right. Here is a simple example with mark up.

Code: Select all

<?
if(isset($_GET['isitfriday'])){ //check to see if the value is friday or not
    $isitfriday = $_GET['isitfriday'];
    if(strtolower($isitfriday) === "yes"){
        echo "Thank god it's FRIDAY!!!"; //if=yes echo
    }else{
        echo "Not quite Friday yet."; //if!=yes echo
    }
}else{ //if there is no value set, then echo links.
    echo "<a href=\"page.php?isitfriday=yes\">If today is Friday, click here</a><br><a href=\"page.php?isitfriday=no\">If today is not Friday, click here</a>";
}
?>
so when you are going to send the data to another page (what ever page that may be) first do the link to the page "http://www.yoursite.com/test_page.com" then add the $_GET method to it. "http://www.yoursite.com/test_page.com?v ... x&var3=xxx". var1, 2, and 3 being your own data of course.

Any questions about this?

Re: Php get function

Posted: Wed Nov 18, 2009 10:12 pm
by gippo
I really still don't get it. :[

Re: Php get function

Posted: Thu Nov 19, 2009 12:26 am
by Weiry
When a url uses a link like what your after, the main file is not called guide.php, its actually index.php
Because index.htm, index.html, index.php etc are default pages when a web page opens, it looks for these files first.

So if you have an index.php in a "myDirectory" folder, then you dont actually need to type /index.php
Example:

Code: Select all

http://mywebsite.com/myDirectory/index.php
http://mywebsite.com/myDirectory/
These two links are the same page (assuming you dont have anything other than index.php in the folder that could override it)

So when you want a link like this:

Code: Select all

http://mywebsite.com/myDirectory/?page=guide
Actually refers to the link:

Code: Select all

http://mywebsite.com/myDirectory/index.php?page=guide
To get the page value from the index.php file, you would need something like this:

Code: Select all

<?php
if(!isset($_GET['page']){
    $page = "index";  // if there is no page specified, use the default page
}else{ 
    $page = $_GET['page'];  // if there is a page specified by $_GET, use it instead.
}
 
/*
*  This is where you can now call each page as you need it.
*  There are many ways in which you can do this, but if you
*  wanted to INCLUDE a file 'guide.php' when the page specified
*  is 'guide', then you would use something like this.
*/
 
if($page == "guide"){
    include_once("guide.php");
}elseif($page == "info"){
    include_once("info.php");
}else{
    include_once("index.php"); // the index page is only shown if no other pages are specified.
}
?>
Hope that clears up a few things :D

Re: Php get function

Posted: Thu Nov 19, 2009 4:11 am
by fubariser
Just contributing...dirty, but I DO hope you get the concept

Code: Select all

 
 
<?php
 
if( isset($_GET['page']) ):
    
    $page = $_GET['page']; // pass the value of $_GET['page'] to a local variable for easy use;
    
    switch($page): // use a switch condition to refer pages from the var $page
    
        case 'home':
            include_once('home.php');
        break;
        case 'guide';
            include_once('guide.php'); // load the guide page
        break;
        case 'about us';
            include_once('about_us.php');
        break;
        default:
            include_once('home.php');
        break;
    
    endswitch;
 
endif;
 
?>
 
 

Posted: Thu Nov 19, 2009 9:20 am
by Jonah Bron
But, just a note, if that's all you are trying to do, you might as well just go to http://mywebsite.com/myDirectory/guide.php