Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Undefined index?

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Undefined index?

Postby Dimbata » Fri Mar 29, 2013 7:55 am

OK, so I have this code:
Code: Select all
<?php
if ($page=null) {$page='index';} else {$page = $_GET['page'];}
switch($page){
default: include "index2.php"; break;
case "index": include "index2.php"; break;
case "champions": include "champs.php"; break;
case "gallery": include "gallery.php"; break;
case "about": include "about.php"; break;
case "upload": include "uploading.php"; break;
}
?>

and a map for un image:
Code: Select all
<map name="menu">
   <area shape="rect" coords="90,30,325,115" href="index.php?page=index" alt="Home">
   <area shape="rect" coords="330,64,429,79" href="index.php?page=index" alt="Home">
   <area shape="rect" coords="431,64,528,79" href="index.php?page=champions" alt="Champions">
   <area shape="rect" coords="530,64,628,79" href="index.php?page=gallery" alt="Gallery">
   <area shape="rect" coords="630,64,727,79" href="index.php?page=about" alt="About">
   </map>

but if I don't place "?page=..." at the end of the "index.php" I get an error:
Code: Select all
Notice: Undefined index: page in C:\wamp\www\LoL Fan Site\index.php on line 14


How can I fix it so if I don't place "?pace=.." to include index2.php ?
Dimbata
New php-forum User
New php-forum User
 
Posts: 7
Joined: Fri Mar 29, 2013 7:45 am

Re: Undefined index?

Postby seandisanti » Fri Mar 29, 2013 7:59 am

if ($page=null) should be if ($page==null). the way you have it you're assigning null to $page, and then immediately assigning 'index'
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Undefined index?

Postby Dimbata » Fri Mar 29, 2013 8:00 am

Still getting the same error.

Edit: Oops i get this:
Code: Select all
 Notice: Undefined variable: page in C:\wamp\www\LoL Fan Site\index.php on line 14
Dimbata
New php-forum User
New php-forum User
 
Posts: 7
Joined: Fri Mar 29, 2013 7:45 am

Re: Undefined index?

Postby seandisanti » Fri Mar 29, 2013 8:15 am

use isset() and empty() to check if a variable is set or empty
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Undefined index?

Postby Dimbata » Fri Mar 29, 2013 8:20 am

So
Code: Select all
<?php
if (empty($page)) {$page=$_GET['page'];}
if (empty($page)) {$page='index';}
switch($page){
default: include "index2.php"; break;
case "index": include "index2.php"; break;
case "champions": include "champs.php"; break;
case "gallery": include "gallery.php"; break;
case "about": include "about.php"; break;
case "upload": include "uploading.php"; break;
}
?>

but I still get an error
Code: Select all
Notice: Undefined index: page in C:\wamp\www\LoL Fan Site\index.php on line 14
Dimbata
New php-forum User
New php-forum User
 
Posts: 7
Joined: Fri Mar 29, 2013 7:45 am

Re: Undefined index?

Postby seandisanti » Fri Mar 29, 2013 8:36 am

hard to identify which is line 14 of the 12 lines you've posted... but probably something more like this...
Code: Select all

if (empty($page){ 
if (isset(
$_GET['page']){
$page $_GET['page']
} else {
$page 'index';
}
}
 
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Undefined index?

Postby Dimbata » Fri Mar 29, 2013 8:41 am

Thank you!

Edit: btw, can you help me in the other post?
Dimbata
New php-forum User
New php-forum User
 
Posts: 7
Joined: Fri Mar 29, 2013 7:45 am


Return to PHP coding => General

Who is online

Users browsing this forum: Bing [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.