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

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?

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.

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.

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!' );
}
?>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.