Jump to content

Url $_GET


renfley

Recommended Posts

K hey guys hoping one of you can help me out lol,

 

Problem=

When someone clicks a link it directs them to index.php?page=home

<ul>

      <li><a href="index.php?page=home">Home</a></li>

      <li><a href="index.php?page=about">About us</a></li>

      <li><a href="index.php?page=contact">Contact us</a></li>

</ul>

 

What im trying to achieve is since it the same page i wanna display different content based on the page= ?

 

i was wondering if someone could write something quick basicly

 

<?php

          (verify to see if there is something after index.php ex: ?page=home)

        if there isnt

            echo 'welcome to my site';

        and if there is somethign there

          echo 'include/page/contact.php';

?>

 

Srry if this sound messed up lol

Link to comment
Share on other sites

u can actually do one thing change that to index_new.php

and then in the index_new.php

u can write something like this

<?php
include("index.php");
?>

 

so what it will do is that when ever some one cliks on the link it will send the request to index_new.php file and that in turn pass the values back to the index.php

 

so in the top of index page u can write something like this

 

$Page=$_GET['page'];

Link to comment
Share on other sites

actually you shouldnt really do that because then the user could set the url as http://www.example.com/index.php?page=phpfreaksisawesome and it wouldnt load the index even tho thats not a real page

 

Try:

<?php

$page = $_REQUEST['page'];

if ($page == "contact")
{
require('contact page');
}
else if ($page == "about")
{
require('about page');
}
else if (($page == "home") || (!$page))
{
// your code for the home page
}

?>

Link to comment
Share on other sites

actually you shouldnt really do that because then the user could set the url as http://www.example.com/index.php?page=phpfreaksisawesome and it wouldnt load the index even tho thats not a real page

 

Try:

<?php

$page = $_REQUEST['page'];

if ($page == "contact")
{
require('contact page');
}
else if ($page == "about")
{
require('about page');
}
else if (($page == "home") || (!$page))
{
// your code for the home page
}

?>

 

yeah...good point !

Link to comment
Share on other sites

Its better to use a switch statement tho

<?php

switch($_GET['page']){
case 'home':
// include or whatever on home
break;
case 'news':
// include or whatever on news
break;
default:
// include or whatever when $_GET['page'] doesnt match other case, or dont exist
}

?>

 

Link to comment
Share on other sites

ALPINE You got the ball lol

 

so ive changed this

 

<?php

 

$page = $_REQUEST['page'];

 

if ($page == "home")

{

include ('pages/home.php');

}

else if ($page == "products")

{

include ('pages/products.php');

}

else if ($page == "about")

{

include ('pages/about.php');

}

else if ($page == "order")

{

include ('pages/order.php');

}

else if ($page == "contact")

{

include ('pages/contact.php');

}

else if (($page == "home") || (!$page))

{

// your code for the home page

echo 'welcome home';

}

else

{

echo 'hello';

}

 

 

?>

 

The next issue is that because when the page loads its only index.php and not (index.php?page=home)

It give me

Notice: Undefined index: page in C:\wamp\www\Store Front\index.php on line 66

welcome home

 

Any suggestions

Link to comment
Share on other sites

If you are wondering found a quick solution that will save on code space lol

 

<?php

$page = $_GET['page'];

if ($page)

{

include ("include/".$page.".php");

}

else

{

echo 'Welcome to home page';

}

?>

 

Only problem is the original

Notice: Undefined index: page in C:\wamp\www\Store Front\index.php on line 66

Welcome to home page

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.