Page 1 of 1

Switch Statement with Operators

Posted: Mon Mar 02, 2009 3:18 pm
by MIGO
Hello, I need some help to sort this out ( in Red ):

Code: Select all

<?php
 
require_once("library/connect.php");
 
$user_id = $_GET["number"];
 
$null = "";
 
$idget = ("SELECT MAX(user_id)FROM applications");
$user_idd = mysql_query($idget);
$useridd = mysql_fetch_array($user_idd);
$useridd = $useridd[0];
 
switch ($user_id) {
 
   case "$null" :
   echo "<p>Please enter a search...</p>";
    include('pages/listbackbutton.php');
    print $useridd;
   break;
 
   case "0" :
   echo "<p>No result found on the database with that number, please press back to search again.</p>";
    include('pages/listbackbutton.php');
   break;
 
   case"[color=#BF0000][b]$user_id > $useridd[/b][/color]" :
   echo "<p>No result found on the database with that number, please press back to search again.</p>";
         include('pages/listbackbutton.php');
    break;
 
   default :
    $list = "SELECT * FROM applications WHERE user_id = '$user_id'";
    $applist = mysql_query($list);
 
while($row = mysql_fetch_array($applist))
{
    
    echo 
        "<b>Application Number</b>: {$row['user_id']}<br><br>".
 
    "<b>Nickname:</b> {$row['nickname']}<br>".
    "<b>Age:</b> {$row['age']}<br>".
    "<b>Clan:</b> {$row['clan_name']}<br>".
    "<b>Country:</b> {$row['country']}<br>".
    "<b>League:</b> {$row['league']}<br>".
    "<b>Member</b> Since: {$row['member_since']}<br><br>".
    "<b>Average Value:</b> {$row['avg_value']}<br>".
    "<b>Average Skills:</b> {$row['avg_skills']}<br>".
    "<b>Average Talent:</b> {$row['avg_talent']}<br><br>";
 
 include('pages/listbackbutton.php');
}
 
mysql_free_result($applist);
   break;
 
               }
?>
I am not an expert on php :oops: when I type the command print $useridd I get the exact number of applications number in my db but when I try to use that string in the switch case with operator, that case do not work at all :(

Could you please help me sort it out ;) thank you.

Re: Switch Statement with Operators

Posted: Mon Mar 02, 2009 3:30 pm
by mickeyunderscore
If you want to test an expression then you should use an if/elseif, it's not really practical to use a switch, though it is possible in the following manner:

Code: Select all

<?php 
switch(true){
    case ($a > 5):
        //stuff
        break;
    case ($a < 5):
        //stuff
        break;
    default:
        //stuff
        break;
}
?>

Re: Switch Statement with Operators

Posted: Mon Mar 02, 2009 4:14 pm
by MIGO
mickeyunderscore wrote:If you want to test an expression then you should use an if/elseif, it's not really practical to use a switch, though it is possible in the following manner:

Code: Select all

<?php 
switch(true){
    case ($a > 5):
        //stuff
        break;
    case ($a < 5):
        //stuff
        break;
    default:
        //stuff
        break;
}
?>
Actually I am using that way to do the switch statement, if/elseif could be a option but I think with cases its easy and organized :D