Very noobish navigation 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
User avatar
trowa
Forum Newbie
Posts: 2
Joined: Tue Oct 21, 2003 7:21 am

Very noobish navigation problem..!

Post by trowa »

Hi, needless to say i am totally new to PHP and have only made a small navigation system with it.

I have done a search for an article about my particular problem, but i suspect that it's a ridiculously easy problem to solve since i couldn't find any threads about it.

The problem that i'm having is that at the moment I have a navigation system that reloads the index page with a new content section depending on what you click in the navigation. The code looks like this;

on each link is something like this :

Code: Select all

<a href="index.php?page=blog">- 
          Personal blog</a>
And on the index page is this:

Code: Select all

<?php 			  
if ($page==blog) &#123; 
   include "cc/blog/blog.php"; 
&#125; 
if ($page==epc) &#123; 
   include "tutorials/flash/epc.php"; 
&#125; 
if ($page==news) &#123; 
   include "news/news.php"; 
&#125; 
if ($page==about) &#123; 
   include "about/about.php"; 
   &#125;
   if ($page==null) &#123; 
   include "news/news.php"; 
   &#125;
?>
Now as the code is at the moment i need to alter the index file every time i add a new page, adding a new 'if' statement for every new page.

My question is, how can i send a URL as a variable and then have the index page it open that variable as the content, so that i don't need to alter the index page everytime i add a page?? IE;

Have something like this on a link;

Code: Select all

<a href="index.php?page=news/news.php">- 
          News</a>
and then something like this in the index:

Code: Select all

<?php 			  
if ($page == null)&#123; 
$page = news/news.php
&#125;
   include $page 
&#125;
Any help would be greatly appreciated. d(^^)b

>>>> Edit >>>>>

Ok the link code works like this;

Code: Select all

<a href="index.php?page=news/news.php">- 
          News</a>
and on the index i have put this;

Code: Select all

<?php 			  
if ($page == null)&#123; 
$page = news/news.php
&#125;
   include $page 
&#125;
but when i go to the index i get this error,
Warning: Division by zero in /home/virtual/site47/fst/var/www/html/index2.php on line 112
the pages load in fine from the the navigation, but it is just when i load up the index for the first time that i get this error.
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

Code: Select all

<?php
$page = $_GET['page']; // get the query string
if ($page=="blog") {
include "cc/blog/blog.php";
} elseif ($page=="epc") {
include "tutorials/flash/epc.php";
} elseif ($page=="news") {
include "news/news.php";
} elseif ($page=="about") {
include "about/about.php";
} else {
   include "news/news.php";
}
?>
Some mistakes you made were:

Code: Select all

if($this==that) // WRONG UNLESS $THIS should be a number or so

if($this=="that") // CORRECT, use quotes
------

if($that=="that") // only for first, not any others, seconds and others use:
elseif($that=="this") // right
On you other code:

Code: Select all

<?php           
$page = $_GET['page']; // get query string
if(!isSet($page)) {
$page = "news/news.php"; // quotation marks AND semi colon!!!
}
include $page; 
?>
Hope that helps,

-Nay
User avatar
trowa
Forum Newbie
Posts: 2
Joined: Tue Oct 21, 2003 7:21 am

Post by trowa »

Thanks Nay, that code worked perfectly

Code: Select all

<?php          
$page = $_GET['page']; // get query string 
if(!isSet($page)) { 
$page = "news/news.php"; // quotation marks AND semi colon!!! 
} 
include $page; 
?>
But i also got this method to work:

Code: Select all

<?php
if ($page==NULL){
$page = "news/news.php";
}  
include $page; 

?>
Oh and the lack of quotation marks and semi colons in my code is a bad habit picked up from years of actionscripting..!!

Thanks a lot for the help..!
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

Code: Select all

if ($page==NULL)
{
include "news/news.php";
}
else
{
include "$page.php";
}
just make sure you send the value as the exact filename

ie. if your blog is called "blog.php" dont send it as "index.php?page=MyBlog"
Cruzado_Mainfrm
Forum Contributor
Posts: 346
Joined: Sun Jun 15, 2003 11:22 pm
Location: Miami, FL

Post by Cruzado_Mainfrm »

isset($page) will return true even if you put in your query string like this:
mypage.php?page=
without defining the parameter
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Code: Select all

<?php
$page = $_GET['page']; // get query string
if(!isSet($page))
	echo 'true';
?>
this will always print true. You might get an 'undefined index' warning but $page is always defined afterwards (as NULL if $_GET['page'] was not defined).
Post Reply