[SOLVED] switch /case

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
dizeta
Forum Commoner
Posts: 47
Joined: Mon Feb 02, 2004 9:53 am

[SOLVED] switch /case

Post by dizeta »

Sami | Help us, help you. Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

hi, 
i'm learning to use switch/case but i've this problem.
a very simple question:

Code: Select all

<?php 

switch ($test) {

case "40" : 
echo "The value equals 40."; 
break;

case "32" : 
echo "The value equals 32."; 
break;

default : 

echo "10";
    
break;

} 

echo"<br /><a href="?test=32">32</a>";
?>
why it doesn't work? :roll:
User avatar
wwwapu
Forum Contributor
Posts: 197
Joined: Wed Apr 07, 2004 11:57 am
Location: Turku, Finland

Post by wwwapu »

Your anchor is somewhat strange... Should not it be more like somepage.php?test=32 Also you could try testing $_GET["test"]
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

The link is okay, but it would be better to identify it with a page more clearly. If it's the index, use "./?test=32" or "./index.php?test=32", for example.

Also if you have register_globals set to OFF, then that will not work.

Try adding:

Code: Select all

if ( isset ($_GET['test']) )
{
    $test = trim ( $_GET['test'] );
}
Before that and see what happens.

Resource link: [php_man]register_globals[/php_man]
User avatar
angrytuna
Forum Newbie
Posts: 12
Joined: Mon Feb 23, 2004 1:01 am

not working?

Post by angrytuna »

this is not working for you? It worked all right for me. I exended your test a bit:

Code: Select all

<?php

switch ($test) {

case "40" :
echo "The value equals 40.";
break;

case "32" :
echo "The value equals 32.";
break;

default :
echo "10"; 
break;

}

echo"<br /><a href="?test=32">32</a>";
echo"<br /><a href="?test=40">40</a>";
echo"<br /><a href="?test=48">48</a>";

?>
This successfully recognized 32, 40, and defaulted for 48. Of course, I've got register_globals turned on in the server, as I'm using an older version of PHP. This is a bit of a security concern, and I don't recommend it for public use. That said, if you appropriately enough do not have this turned on in the server (which you can find out using the php function phpinfo()), you must get the information from either the $HTTP_GET_VARS array(php version < 4.1), or the $_GET array(version >= 4.1). e.g. change your switch statement to:

Code: Select all

switch($HTTP_GET_VARS['test']);
You can read more about these predefined variables here.
User avatar
dizeta
Forum Commoner
Posts: 47
Joined: Mon Feb 02, 2004 9:53 am

Post by dizeta »

ok, that it was a test to understand how works switch / case .
the real problem that i have, is to use it to make paginations.

this is the sample:

Code: Select all

<?php
include("../pannello/dbconnect.php");

switch ($_REQUEST['pagina']) {

case 2: 

$query = "SELECT presentazione, titolo_sommario, corpo_sommario, titolo_documento, corpo_documento FROM studi WHERE studi.id_studio= '" . $_REQUEST["id_studio"]  . "'";
$result = mysql_query($query)or die(mysql_error()); 
$dati = mysql_fetch_array($result);
echo $dati["presentazione"]."<br />";
echo $dati["titolo_documento"]."<br />";
echo  $dati["corpo_documento"];

break;

default:

$query = "SELECT titolo_analisi, corpo_analisi FROM studi WHERE studi.id_studio= '" . $_REQUEST["id_studio"]  . "'";
$result = mysql_query($query)or die(mysql_error()); 
$dati = mysql_fetch_array($result);

echo" 
                    ".$dati["titolo_analisi"]."<br />
	       ".$dati["corpo_analisi"]."<br /> ";
	       
break;
}

echo"<a href="{$_SERVER["PHP_SELF"]}?pagina=2">".$dati["titolo_analisi"]."</a>";
?>
i get the var from a page , i printed the values of the query, and its work, i've tested the values of the variables and i get its. but works only default page, when i try to get case "2" returns a blank page.
i don't know why. i've my limits :cry: try to learning
User avatar
dizeta
Forum Commoner
Posts: 47
Joined: Mon Feb 02, 2004 9:53 am

Post by dizeta »

i forgot to tell you that my register_globals is set to off , and i'm testing the script localhost, but on server i've register _ globals set to on...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

make sure error_reporting is set to E_ALL and display_errors is on/true/1
User avatar
dizeta
Forum Commoner
Posts: 47
Joined: Mon Feb 02, 2004 9:53 am

Post by dizeta »

error_reporting = E_ALL
display_errors = On

this is my setup on php.ini

but , there's no difference...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

then it should echo something, not necessarily visible in the browser though (like strictly html) ... make sure it your selection returns at least 1 row..
User avatar
dizeta
Forum Commoner
Posts: 47
Joined: Mon Feb 02, 2004 9:53 am

Post by dizeta »

:?:
i'm sorry feyd, but i didn't understand , what i have to do....

i've tested several times all the vars, and tested what i get from $_REQUEST ,

it's seems that all works, but i don't understand where is the problem...

this is the first time that i try to use Switch/ case clause, for sure there's something that i miss....
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

well.. you didn't pass id_studio in your page 2 link... so that may be a major component.
User avatar
dizeta
Forum Commoner
Posts: 47
Joined: Mon Feb 02, 2004 9:53 am

Post by dizeta »

how can do it?

i thought something like that?

Code: Select all

echo"<a href="{$_SERVER["PHP_SELF"]}?".$_REQUEST["id_studio"]."&pagina=2">".$dati["titolo_analisi"]."</a>";
but it's a stupidity i suppose.....
User avatar
dizeta
Forum Commoner
Posts: 47
Joined: Mon Feb 02, 2004 9:53 am

Post by dizeta »

solved!
i had a vision....:lol:

Code: Select all

echo"<a href="{$_SERVER["PHP_SELF"]}?pagina=2&id_studio= " . $_REQUEST["id_studio"]  . "">".$dati["titolo_analisi"]."</a>";
thanks to all!
Post Reply