Jump to content

Archived

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

salvador2001

*SOLVED* variable problem

Recommended Posts

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");
}

?>

Share this post


Link to post
Share on other sites
What is the error message you are getting? PLease could you post them here.

Share this post


Link to post
Share on other sites
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");
}

Share this post


Link to post
Share on other sites
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:
[code]<?php if (!isset($_GET['load'])) $load = 'welcome'; ?>[/code] or something similar.

Ken

Share this post


Link to post
Share on other sites
By the looks of it you've not set $load to be anything before its used/checked.

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

Share this post


Link to post
Share on other sites
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");
}

?>

Share this post


Link to post
Share on other sites
If you had implemented my suggestion instead of the one from [b]Yesideez[/b] 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:
[code]<?php if (!isset($_GET['load'])) $load = 'welcome'; ?>[/code][/quote]

Ken

Share this post


Link to post
Share on other sites
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.

[code]<?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");  
}  

?>
[/code]

Share this post


Link to post
Share on other sites

×

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.