Jump to content

[SOLVED] Php link coding


zavin

Recommended Posts

you might want to try this

 


$page = $_GET['page'];

if(isset($page))
{
    switch($page)
    {
     case 'home':
     
     echo "this is home";
     break;

     case 'page2':
     
     echo "this is page2";
     break;

     case 'page3':
     
     echo "this is page3";
     break;
  }
}
else
{
     echo "this is home page which is the default";
}

<a href="index.php">home</a>
<a href="index.php?page=page2">Page 2</a>
<a href="index.php?page=page3">Page 3</a>

 

 

try this and let me know

Link to comment
Share on other sites

are you talking about a clickable link, or a header redirect, or submitting a form ?

 

how exactly are you getting from one page to the other in this case

In most cases it will be a link.

 

Inside of index.php, you have a script like this:

$page = $_GET['on'];
require_once("$page.php");

Will I need to add anything to each page after that as well?

Link to comment
Share on other sites

you might want to try this

 


$page = $_GET['page'];

if(isset($page))
{
    switch($page)
    {
     case 'home':
     
     echo "this is home";
     break;

     case 'page2':
     
     echo "this is page2";
     break;

     case 'page3':
     
     echo "this is page3";
     break;
  }
}
else
{
     echo "this is home page which is the default";
}

<a href="index.php">home</a>
<a href="index.php?page=page2">Page 2</a>
<a href="index.php?page=page3">Page 3</a>

 

 

try this and let me know

 

It seems that this is the long way to do it. I can see how it may work, but when I'm done with my site I will have over 30 pages. I was hoping for a shorter solution.

Link to comment
Share on other sites

irkevin gave some useful information, but what I think you're looking for is a mix:

$page = $_GET['page'];
if((isset($page)) && ($page != ''))
    require_once("$page.php");
else
    require_once('default.php');

 

That is just a basic shell. As long as 'page' is set, you'll open up that page, otherwise it goes to default.php.

This allows you to load in a single navigation file in the index, but show up on the same page. You don't need to include anything additional, but here is an example of what a basic site would look like with a header, navigation, main page, and footer:

<?php
$page = $_GET['page'];
echo '<table><tr><td colspan="2">';
require_once('header.php');
echo '</td></tr><tr><td>';
require_once('navigation.php');
echo '</td><td>';
if((isset($page)) && ($page != ''))
    require_once("$page.php");
else
    require_once('default.php');
echo '</td></tr><tr><td colspan="2">';
require_once('footer.php');
echo '</td></tr></table>';
?>

 

I hope that puts things into perspective.

Link to comment
Share on other sites

Well, there might ber some sort of solution, but that should give you an idea..  :D

 

<?php
$my_pages = array('home', 'music', 'stuffs', 'aboutus');

// check that $_GET['page'] is present, if its not we'll setup a default page
if(isset($_GET['page'])) && !empty($_GET['page']))
{
    $requested_page = strtolower($_GET['page']);
}
else // $_GET['page'] not found or is empty, so we'll setup a default page to be requested 
{
    $requested_page = 'home';
}

// setup an array of all your sites pages
$my_pages = array('home', 'music', 'stuffs', 'aboutus');

$requested_page = strtolower($_GET['page']);

// make sue the request page is in $my_Pages array
if(in_array($requested_page, $my_pages))
{
    // the requested page will be included from a folder called my_pages in your websites root folder.
    // I assume the files to be included are html files
    include $_SERVER['DOCUMENT_ROOT'] . '/my_pages/' . $requested_page . 'html';
}
else
{
    // requested_page not found
    die( 'The requested page (' . $requested_page ') was not found!' );
}
?>

 

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.