Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Php Switch Case

0 views
Skip to first unread message

jfcby

unread,
Sep 8, 2010, 2:54:11 PM9/8/10
to
I'm trying to get this php script to either include a web page,
redirect to another webpage, or echo text. I have this page in the
main www folder on the server.

My problem is if I type the webpage address like so www.ebooktaught.com/prodtest.php
then it displays the first switch case instead of the default.

How can I get the following script to:

1. After I type in www.ebooktaught.com/prodtest.php it will display
the default echo?
2. How can I get the script to work if I type in
www.ebooktaught.com/prodtest.php?pg=pd&nb=2 for it to display the
second switch case?


[code]
<?php
$gpg = $_GET["pg"];
$gnb = $_GET["nb"];
Switch ($gpg){

Case $gpg == 'pd' && $gnb == 1:
echo "Product Page = " . $_GET['pg'] . " & Product Page # = " .
$_GET['nb'];
break;
Case $gpg == 'pd' && $gnb == 2:
echo "Product Page = " . $_GET['pg'] . " & Product Page # = " .
$_GET['nb'];
break;

Case $gpg == 'sp' && $gnb == 1:
echo "Product Page = " . $_GET['pg'] . " & Product Page # = " .
$_GET['nb'];
break;
Case $gpg == 'sp' && $gnb == 2:
echo "Product Page = " . $_GET['pg'] . " & Product Page # = " .
$_GET['nb'];
break;

default:
echo "Default";
}
?>
[/code]

Thank you for your help,
jfcby

matt

unread,
Sep 8, 2010, 3:10:11 PM9/8/10
to
On Sep 8, 2:54 pm, jfcby <jamesf...@gmail.com> wrote:
> I'm trying to get this php script to either include a web page,
> redirect to another webpage, or echo text. I have this page in the
> main www folder on the server.

[snip]

>         Switch ($gpg){
>
>                 Case $gpg == 'pd' && $gnb == 1:

[snip]

You don't understand the switch/case syntax:
http://us2.php.net/manual/en/control-structures.switch.php

jfcby

unread,
Sep 8, 2010, 3:20:26 PM9/8/10
to
On Sep 8, 3:10 pm, matt <matthew.leonha...@gmail.com> wrote:

> You don't understand the switch/case syntax:http://us2.php.net/manual/en/control-structures.switch.php

matt,

I've read through that website and there are no examples of what I'm
trying to do.

This is what I'm trying to do. I want to be able to type in a web
address like so www.ebooktaught.com/prodtest.php?pg=pd&nb=2 and have
my php switch case statement display the correct information. Is this
possible with a switch case statement and where can I find some
examples of the correct way to do it because the link you provided
does not show how to do that?

The Natural Philosopher

unread,
Sep 8, 2010, 3:58:11 PM9/8/10
to

Switch (x) takes VALUES OF (x) as the arguments in each 'case'

Tim Roberts

unread,
Sep 9, 2010, 2:30:00 AM9/9/10
to
jfcby <jame...@gmail.com> wrote:
>
>This is what I'm trying to do. I want to be able to type in a web
>address like so www.ebooktaught.com/prodtest.php?pg=pd&nb=2 and have
>my php switch case statement display the correct information. Is this
>possible with a switch case statement...

No, it's not. You will need to use a series of if/else if statements.
--
Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.

"Álvaro G. Vicario"

unread,
Sep 9, 2010, 3:10:01 AM9/9/10
to
El 08/09/2010 20:54, jfcby escribió/wrote:
> [code]

Please do not insert bbcode tags in Usenet messages. This is it not a
fancy HTML forum.


> <?php
> $gpg = $_GET["pg"];
> $gnb = $_GET["nb"];

These values may not exist. Try this instead:

<?php

error_reporting(E_ALL);
ini_set('display_errors', TRUE);

$gpg = isset($_GET['pg']) ? $_GET['pg'] : NULL;
$gnb = isset($_GET['nb']) ? $_GET['nb'] : NULL;


> Switch ($gpg){

Perhaps it's a pet peeve of mine but my eyes bleed when I see PHP
control structures in sentence case... :)


>
> Case $gpg == 'pd'&& $gnb == 1:

Right, this is a boolean expression so it will return either TRUE or
FALSE. You can *never* get a match: even if $gpg equals 'pd' and $gnb
equals '1', the expression is TRUE, which is different from 'pd'. I
presume you want this:

http://es.php.net/manual/en/control-structures.if.php
http://es.php.net/manual/en/control-structures.else.php
http://es.php.net/manual/en/control-structures.elseif.php


--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--

0 new messages