Reply LinkBack Thread Tools Search this Thread
Old 03-05-2008, 05:11   #1 (permalink)
hollywooood
Registered User
 
Join Date: Jan 2008
Posts: 55
The switch statement and PHP 5....

I have a website that is currently working perfectly on a PHP 4 server. I recently upgraded to PHP 5 and tested the site but only in vain. It seems that 5 is not picking up on the switch statement. I used the switch statement to make the "switch" for the content while keeping my template (index page) in order.

I have two computers so I went back to PHP 4 on one and using 5 in the other
It all work in 4 but 5...not so much.

Can anyone tell me what happened and what can I do to make this work in 5?

I hope my explanation was clear.

Thanks,

Will
  Reply With Quote
Old 03-05-2008, 06:39   #2 (permalink)
haku
shiro
 
haku's Avatar
 
Join Date: Aug 2007
Location: Yokohama, Japan
Posts: 1,306
no code?
__________________
This is not a signature.
  Reply With Quote
Old 03-05-2008, 06:52   #3 (permalink)
freelancr
Web Developer
 
freelancr's Avatar
 
Join Date: Oct 2006
Location: Stratford-upon-Avon, Warwickshire, UK
Posts: 1,848
Send a message via MSN to freelancr Send a message via Skype™ to freelancr
Blatently because you are relying on "register_globals" being on. In your switch statement I bet you are looking for $id when it should be $_GET['id']

and your url is like this:
index.php?id=home

Show us some code.
__________________
  Reply With Quote
Old 03-05-2008, 09:48   #4 (permalink)
hollywooood
Registered User
 
Join Date: Jan 2008
Posts: 55
My Apologies..

PHP Code:
<?php session_start();
strpos($HTTP_SERVER_VARS['HTTP_USER_AGENT'],"IE")!="" 
|| strpos($_SERVER['HTTP_USER_AGENT'],"IE")!="" ?
$css="<link href=\"iestyle.css\" rel=\"stylesheet\" type=\"text/css\" />" :
$css="<link href=\"style.css\" rel=\"stylesheet\" type=\"text/css\" />";
switch (
$page)
    {
    
    case 
about:
    
$title="MPower | About Us";
    
$includes="page/about.php";
    break;
    case 
contact:
    
$title="MPower | Contact Us";
    
$includes="page/contact.php";
    break;
    
    case 
products:
    
$title="MPower | Products";
    
$includes="page/products.php";
    break;
    case 
poetry:
    
$title="MPower | Poetry";
    
$includes="page/poetry.php";
    break;
    case 
events:
    
$title="MPower | Events";
    
$includes="page/events.php";
    break;
    case 
links:
    
$title="MPower | Links";
    
$includes="page/links.php";
    break;
    default:
    
$title="MPower | Home";
    
$includes="page/home.php";
    
$page="home";
    }
?>

My menu is as follows.....


HTML Code:
<div id="menu"> <ul> <li><a id="home" href="index.php?page=home"></a></li> <li><img src="http://www.designerstalk.com/forums/images/space1.png" /></li> <li><a id="about" href="index.php?page=about"></a></li> <li><img src="http://www.designerstalk.com/forums/images/space2.png" /></li> <li><a id="contact" href="index.php?page=contact"></a></li> <li><img src="http://www.designerstalk.com/forums/images/space3.png" /></li> <li><a id="products" href="index.php?page=products"></a></li> <li><img src="http://www.designerstalk.com/forums/images/space4.png" /></li> <li><a id="poetry" href="index.php?page=poetry"></a></li> <li><img src="http://www.designerstalk.com/forums/images/space5.png"/></li> <li><a id="events" href="index.php?page=events"></a></li> <li><img src="http://www.designerstalk.com/forums/images/space6.png"/></li> <li><a id="links" href="index.php?page=links"></a></li> </ul> </div>

Thanks for the help....

Last edited by hollywooood : 06-05-2008 at 03:21.
  Reply With Quote
Old 03-05-2008, 09:57   #5 (permalink)
haku
shiro
 
haku's Avatar
 
Join Date: Aug 2007
Location: Yokohama, Japan
Posts: 1,306
freelancr was right without even seeing the code. Nice one!

Hollywood: You need to change this line:

Code:
switch ($page)

to this:

Code:
switch ($_GET['page'])
__________________
This is not a signature.
  Reply With Quote
Old 03-05-2008, 10:00   #6 (permalink)
hollywooood
Registered User
 
Join Date: Jan 2008
Posts: 55
Dude....I can't thank you enough! You guys are amazing. Thank you big time.
  Reply With Quote
Old 03-05-2008, 10:18   #7 (permalink)
Hunch
Grumpy old man
 
Hunch's Avatar
 
Join Date: Oct 2007
Location: North Japan
Posts: 1,128
What's going on with the default case?

Firstly it has no break; so I think anything which takes the default branch will trickle down into the about case. Then you seem to have several redundant lines of code at the bottom of the switch without a case (is that supposed to be the default branch?) Presumably you make the assignment to $page somewhere else in the code?

Just a couple of other observations - why not use single quotes for the $css = '...' - then you don't need to escape every occurrence of double quotes:

$css = '<link href="iestyle.css" rel="stylesheet" type="text/css" />';

is a lot more readable than:

$css="<link href=\"iestyle.css\" rel=\"stylesheet\" type=\"text/css\" />";

Also, I think it's good practice to quote strings, even though in a case statement you aren't forced to. Again it makes the code clearer. Although since you use ternary form for your conditional, I guess clarity isn't a big concern for you.

Edit: I love it when someone else answers between starting and finishing a comment
  Reply With Quote
Old 03-05-2008, 14:31   #8 (permalink)
xENo
Jack of all trades
 
xENo's Avatar
 
Join Date: May 2008
Location: Orange County, CA
Posts: 51
Send a message via AIM to xENo Send a message via Yahoo to xENo
freelancr.... that was amazing. Props.
  Reply With Quote
Old 03-05-2008, 14:34   #9 (permalink)
freelancr
Web Developer
 
freelancr's Avatar
 
Join Date: Oct 2006
Location: Stratford-upon-Avon, Warwickshire, UK
Posts: 1,848
Send a message via MSN to freelancr Send a message via Skype™ to freelancr


I'm going to be taking my Zend PHP certification exam this year.
__________________
  Reply With Quote
Old 04-05-2008, 01:32   #10 (permalink)
hollywooood
Registered User
 
Join Date: Jan 2008
Posts: 55
Freelancer....What have you done to prepare for the exam? Expenses?
  Reply With Quote
Old 04-05-2008, 01:33   #11 (permalink)
pgo
Moderator
 
pgo's Avatar
 
Join Date: Jan 2005
Location: Brooklyn, NYC
Posts: 11,869
Better get ready, PHP6 is coming out this year last I heard!
__________________
  Reply With Quote
Old 04-05-2008, 01:42   #12 (permalink)
hollywooood
Registered User
 
Join Date: Jan 2008
Posts: 55
PGO...What do you recommend the best way to learn the in and outs of PHP. I live in Indonesia where information is not easily come by. I think I should be able to get what I need off the net but what and who can give me the best materials to learn it? Appreciate any suggestions.
  Reply With Quote
Old 05-05-2008, 10:47   #13 (permalink)
Hunch
Grumpy old man
 
Hunch's Avatar
 
Join Date: Oct 2007
Location: North Japan
Posts: 1,128
Think of a problem and try to solve it. Search for solutions as you go. Rinse and repeat.

Probably all you need to master a basic language like PHP is:

1. Basic syntax
2. Operators
3. Control structures
4. Defining classes and functions
5. Variables, arrays and variable scope

You don't need to memorize every available function call or class method. That's why php.net has a reference. Having said that, it would be useful to at least have a working knowledge of the most commonly used functions (such as string functions) so you don't have to keep going to look them up. They'll come with time though.
  Reply With Quote
Old 05-05-2008, 21:58   #14 (permalink)
hollywooood
Registered User
 
Join Date: Jan 2008
Posts: 55
Thanks hunch! It seems I spend a lot of time on one problem but that just may be how it is. Going to look up your suggestions. I'm sure it will help!
  Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Contact Us - Web Design Forums - Archive - Top
Search Engine Optimization by vBSEO 3.0.0 RC8