Page 1 of 1
[SOLVED] switch /case
Posted: Tue Oct 19, 2004 10:47 am
by dizeta
Sami | Help us, help you. Please use 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?

Posted: Tue Oct 19, 2004 10:59 am
by wwwapu
Your anchor is somewhat strange... Should not it be more like somepage.php?test=32 Also you could try testing $_GET["test"]
Posted: Tue Oct 19, 2004 11:13 am
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]
not working?
Posted: Tue Oct 19, 2004 11:15 am
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:
You can read more about these predefined variables
here.
Posted: Tue Oct 19, 2004 11:50 am
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

try to learning
Posted: Tue Oct 19, 2004 11:53 am
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...
Posted: Tue Oct 19, 2004 2:14 pm
by feyd
make sure error_reporting is set to E_ALL and display_errors is on/true/1
Posted: Tue Oct 19, 2004 2:37 pm
by dizeta
error_reporting = E_ALL
display_errors = On
this is my setup on php.ini
but , there's no difference...
Posted: Tue Oct 19, 2004 2:40 pm
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..
Posted: Tue Oct 19, 2004 2:59 pm
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....
Posted: Tue Oct 19, 2004 3:08 pm
by feyd
well.. you didn't pass id_studio in your page 2 link... so that may be a major component.
Posted: Tue Oct 19, 2004 3:23 pm
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.....
Posted: Tue Oct 19, 2004 3:46 pm
by dizeta
solved!
i had a vision....
Code: Select all
echo"<a href="{$_SERVER["PHP_SELF"]}?pagina=2&id_studio= " . $_REQUEST["id_studio"] . "">".$dati["titolo_analisi"]."</a>";
thanks to all!