isset problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
stevengunn
Forum Newbie
Posts: 1
Joined: Sat Nov 10, 2007 2:22 am

isset problem

Post by stevengunn »

Hello, Heres my problem when viewing index.php the home.php is included if I click a link to say bio it goes to index.php?id=bio_gunn and instead of including gunn.php it includes home.php Could anyone tell me what I did wrong? I think it has to do with the $links and $url arrays?

Code: Select all

<? 


$links=array('bio_gunn');
$url=array('/home/crewdog/public_html/site/bio/gunn.php');




if(!isset($id)){ 
include('/home/crewdog/public_html/site/home.php'); 
} 
else 
{ 
    for($i=0; i<count($links); $i++) 
    { 
        if($id==$links[$i]) 
        { 
            include($url[$i]); 
            break; 
        } 
    } 
} 
?>
phpBuddy
Forum Commoner
Posts: 37
Joined: Mon Nov 05, 2007 3:42 am

Re: isset problem

Post by phpBuddy »

Code: Select all

<?php 

$links = array( 'bio_gunn' );
$url = array( '/home/crewdog/public_html/site/bio/gunn.php' );
// if id is submitted in URL, $id is = this value $_GET['id']
// else $id = false
$id = isset($_GET['id']) ? $_GET['id'] : false;

if( $id === false )){ 
    include( '/home/crewdog/public_html/site/home.php' ); 
} 
else 
{ 
    for($i=0; i<count($links); $i++) 
    { 
        if( $id == $links[$i] ) 
        { 
            include( $url[$i] ); 
            break; 
        } 
    } 
} 
?>
We use $_GET['var_name'] to get the value of:
-> index.php?var_name=value

And please, use <?php !!!
Using <? can get youinto troubles in the future,
if you try to run your code in some other web server
Many servers will not allow 'short_tags'!!!
phpBuddy
Forum Commoner
Posts: 37
Joined: Mon Nov 05, 2007 3:42 am

Post by phpBuddy »

This is something how I would do the same.
A little drawback with my script below is:
Will not display info: Page not found. Instead it will only load 'default' page.
But of course script can be modified to do this.

Code: Select all

<?php
/*                 script by phpBuddy 2007
Will include page id in requested in URL, in a link like:
    <a href="index.php?id=contact">Contact Me</a>

If no id, then will use the 'default' page for include
*/

$default = 'home'; // default page
$my_pages = array(
// key=id    value=page
'home'    => 'home.php',
'gunn'    => 'gunn.php',
'contact' => '/home/crewdog/public_html/site/bio/contact.php',
'about'   => '/home/crewdog/public_html/site/bio/about.php',
);

// See if we have a default page file
if(!isset($my_pages[$default]) || !is_file($my_pages[$default])){
    exit('ERROR: default page file does not exist');
}
// If in URL id=$_GET, else id=default
$id = isset($_GET['id'])? $_GET['id'] : $default;
// See if key=id exists and there is such file. If not use: $my_pages[$default] 
$page = array_key_exists($id,$my_pages) && is_file($my_pages[$id]) ? $my_pages[$id] : $my_pages[$default];
// Using 'require' we get an error if file can not be included
require($page);

?>
Post Reply