Jump to content


Photo

*SOLVED* variable problem


  • Please log in to reply
7 replies to this topic

#1 salvador2001

salvador2001
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts

Posted 07 April 2006 - 06:44 PM

hi all,

I am very noob with PHP and i cant get this script working right.
Now i got a variable error on the section "load in directory"

Can anyone please adjust this script for me ?

<?php

// load existing pages

if(isset($_GET['load'])) {
switch($_GET['load']) {
case 'news':
case 'about':
case 'members':
$load = $_GET['load'];
break;
default:
$load = 'welcome';
}
}

// load in root directory

if (is_file($load . ".php")) {
include($load . ".php");
}

// load errorpage

else {
include("error404.php");
}

?>


#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 07 April 2006 - 06:51 PM

What is the error message you are getting? PLease could you post them here.

#3 salvador2001

salvador2001
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts

Posted 07 April 2006 - 09:52 PM

Notice: Undefined variable: load in e:\webserver\.........\script.php on line 19

Line 19 is the part with this code if (is_file($load . ".php")) {
include($load . ".php");
}


#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 07 April 2006 - 11:53 PM

The problem is that if $_GET['load'] isn't set, then the variable $load isn't defined. Do this before your switch to make sure that $load is always defined:
<?php if (!isset($_GET['load'])) $load = 'welcome'; ?>
or something similar.

Ken


#5 Yesideez

Yesideez
  • Members
  • PipPipPip
  • Advanced Member
  • 2,334 posts
  • LocationDevon, UK

Posted 07 April 2006 - 11:54 PM

By the looks of it you've not set $load to be anything before its used/checked.

Try using this:
if (!empty($load)) {
  if (is_file($load . ".php")) { 
    include($load . ".php"); 
  }
}

Not a pro just an enthusiast :)

if (empty($coffee)) {$coffee=new coffee();}

Please surround any code using the CODE tags - I rarely look at anything without them

#6 salvador2001

salvador2001
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts

Posted 08 April 2006 - 12:17 PM

Thanks a lot !, the error message is gone.
But now there is one thing left, it keeps loading the errorpage at start while it should display the welcome page. Its a dynamic page so he doesnt load the content it should.
any idea what is going wrong ?

This is the script so far;

<?php

// only this pages are allowed

if(isset($_GET['load'])) {
switch($_GET['load']) {
case 'news':
case 'about':
case 'members':
$load = $_GET['load'];
break;
default:
$load = 'welcome';
}
}

// load files in rootdirectory

if (!empty($load)) {
if (is_file($load . ".php")) {
include($load . ".php");
}
}

// loading error page

else {
include("error404.php");
}

?>

#7 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 08 April 2006 - 12:48 PM

If you had implemented my suggestion instead of the one from Yesideez you it would be working.

Here is my suggestion again:
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Do this before your switch to make sure that $load is always defined:
<?php if (!isset($_GET['load'])) $load = 'welcome'; ?>
[/quote]

Ken

#8 salvador2001

salvador2001
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts

Posted 08 April 2006 - 01:30 PM

Thanks, someone got it solved for me and its working. For other people this could be a good example so i post the working script here.

<?php   

// only this pages are allowed 

if(isset($_GET['load'])) {   
  switch($_GET['load']) {   
    case 'news':   
    case 'about':   
    case 'members':   
      $load = $_GET['load'];   
      break;   
    default:   
      $load = 'welcome';   
  } 
} else { 
  $load = 'welcome';    // -- if $_GET['load'] is not defined
}   

// load files in rootdirectory 

if (is_file($load . ".php")) {   
include($load . ".php");   
}   

// loading errorpage 

else {   
include("error404.php");   
}   

?> 





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users