Jump to content


Php Switch dont work?


8 replies to this topic

#1 Vic Vance

    Young Padawan

  • Members
  • Pip
  • 20 posts

Posted 27 February 2008 - 01:31 PM

I have a site and it has php switch system, if you know what is that. It is a php page that opens rest of the other pages in the center on your index, but mines dont work, I think its because I have php5 and I think the case_swith is php4

This is my switch: case_switch.php

<?
switch ($page) {

case home:
require 'home.php';
break;

case staff:
require 'staff.php';
break;

case history:
require 'history.php';
break;

}
?>

My navigatios are like this "home" linked up with www.mysite.com?page=home

It is meand to work it works on my old host witch in php 4, the problem starts with the new host that is php5. Let me inform that the case switch php is a script that was made on php 4. I have not modyfied it yet becuase I dont know what makes a script php 5. this is my old host and it works on there click here. I know when you click other pages a error comes up, but it is meant to be like that becuase I have not uploaded the pages. This is my php5 host, and no pages load or even come on click here. I know that it dont work, because if it did it would load a page, or show a error saying the page dont exist.

Please!!!!! help me

Edited by Vic Vance, 27 February 2008 - 01:32 PM.


#2 dotbart

    Young Padawan

  • Members
  • Pip
  • 141 posts
  • Gender:Male
  • Location:Diepenbeek
  • Interests:Webdesign, Webdeveloppement, DJ, ...

Posted 27 February 2008 - 03:20 PM

You're checking for a 'string' value in $page. Strings should be set between " " ..
Also, require() is a function and all parameters should be between brackets () and quotes "":

... require("mypage.php") ...



This would be your solution:

<?
switch ($page) {

case "home":
require("home.php");
break;

case "staff":
require("staff.php");
break;

case "history":
require("history.php");
break;

}
?>


#3 Leibrockoli

    Young Padawan

  • Members
  • Pip
  • 27 posts

Posted 27 February 2008 - 05:04 PM

PHP switch works when you have a $_GET variable set. You can't just have a www link with a ?page setup like that unless you have the $_GET variable.

Try....

<?
   
   if(isset($_GET['page'])){			  //check if the $page variable is set
	   $page = $_GET['page'];		   // then form it into a string
   }else{									  // if it's not....
	   $page = NULL;					   // this would be a default value. Change NULL to "home" maybe.
   }
   
   switch ($page) {
   
   case "home":
   require ('home.php');
   break;
   
   case "staff":
   require ('staff.php');
   break;
   
   case "history":
   require ('history.php');
   break;
   
   }  
   ?>

You're also not limited to using Switch cases as your navigation. You can also use If statements with the $_GET variables.

#4 Vic Vance

    Young Padawan

  • Members
  • Pip
  • 20 posts

Posted 28 February 2008 - 10:51 AM

Thanks it worked, both worked. I wa to just ask one more question, that is How can i make "home " my defualt page form the case_switch?

Edited by Vic Vance, 28 February 2008 - 11:48 AM.


#5 communiti.ch

    Young Padawan

  • Members
  • Pip
  • 52 posts

Posted 28 February 2008 - 12:22 PM

View PostVic Vance, on Feb 28 2008, 11:51 AM, said:

Thanks it worked, both worked. I wa to just ask one more question, that is How can i make "home " my defualt page form the case_switch?

here is a little example:

switch($_GET['page'])
{
	case "banana":
	code();
	break;
	case "apple":
	code();
	break;
	default:
	code();
}


#6 Vic Vance

    Young Padawan

  • Members
  • Pip
  • 20 posts

Posted 28 February 2008 - 01:56 PM

View Postcommuniti.ch, on Feb 28 2008, 12:22 PM, said:

View PostVic Vance, on Feb 28 2008, 11:51 AM, said:

Thanks it worked, both worked. I wa to just ask one more question, that is How can i make "home " my defualt page form the case_switch?

here is a little example:

switch($_GET['page'])
{
	case "banana":
	code();
	break;
	case "apple":
	code();
	break;
	default:
	code();
}


<?
   
   if(isset($_GET['page'])){			  //check if the $page variable is set
	   $page = $_GET['page'];		   // then form it into a string
   }else{									  // if it's not....
	   $page = NULL;					   // this would be a default value. Change NULL to "home" maybe.
   }
   
   switch ($page) {
   
   case "home":
   require ('home.php');
   break;
   default:
   
   case "staff":
   require ('staff.php');
   break;
   
   case "history":
   require ('history.php');
   break;
   
   }  
   ?>

I putted the defult at the home link is it like that?

Edited by Vic Vance, 28 February 2008 - 01:57 PM.


#7 Leibrockoli

    Young Padawan

  • Members
  • Pip
  • 27 posts

Posted 28 February 2008 - 09:15 PM

<?
   
   if(isset($_GET['page'])){			  //check if the $page variable is set
	   $page = $_GET['page'];		   // then form it into a string
   }else{									  // if it's not....
	   $page = home;					  //Defaulted value if $page is empty
   }
   
   switch ($page) {
   
   case "home":
   require ('home.php');
   break;
   
   case "staff":
   require ('staff.php');
   break;
   
   case "history":
   require ('history.php');
   break;
   
   }  
   ?>

I meant changing the $page value in the beginning If statement. This way if the $_GET is not set, it's automatically changed to Home. Hopefully this helps you.

#8 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 28 February 2008 - 11:51 PM

In elaboration of leibrockoli's comment and in answer to your question, here's what you should use:
   switch ($_GET['page']) {

   case "staff":
   require ('staff.php');
   break;
   
   case "history":
   require ('history.php');
   break;

   default:
   require ('home.php');
   break;

   }
Ref: http://php.net/manua...ures.switch.php

Note that i removed the "home" case and used just default. The reasoning is that, since there exists no case for "home" it will fall back on the default, and the end result will still be the same.

#9 Vic Vance

    Young Padawan

  • Members
  • Pip
  • 20 posts

Posted 01 March 2008 - 05:52 AM

Thank all of you people, if it was not for you, I would had been struggling.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users