Jump to content


Photo

Fetch Stats


  • Please log in to reply
2 replies to this topic

#1 JackJack

JackJack
  • Members
  • PipPipPip
  • Advanced Member
  • 38 posts

Posted 06 April 2006 - 05:00 PM

It always selects the last if option.

<?
include("../inc/headers.inc.php");

                            $lmao = mysql_query("SELECT * FROM test WHERE id=1");
                               while ($test = mysql_fetch_array($lmao)) {

$hungry = $test[current_hunger];

if ($hungry = '0'){

$hunger = 'Starving';
}

if ($hungry = '1'){

$hunger = 'Famished';
}

if ($hungry = '2'){

$hunger = 'Hungry';
}

if ($hungry = '3'){

$hunger = 'Unsatisfied';
}

if ($hungry = '4'){

$hunger = 'Unsatisfied';
}

if ($hungry = '5'){

$hunger = 'Satisfied';

}

if ($hungry = '6'){

$hunger = 'Full';

}

if ($hungry = '7'){

$hunger = 'Bloated';

}
echo "Hunger - $hunger";
echo "<br>$hungry";

}
?>





Thank you

JJ

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 06 April 2006 - 05:14 PM

You are using the assignment operater (=) rathar than the comparison operator (==). Also chnage your if statments to an if/elseif statment like so:
if ($hungry == '0') {
    $hunger = 'Starving';
}
elseif ($hungry == '1') {
    $hunger = 'Famished';
}
elseif ($hungry == '2') {
    $hunger = 'Hungry';
}
elseif ($hungry == '3') {
    $hunger = 'Unsatisfied';
}
elseif ($hungry == '4') {
    $hunger = 'Unsatisfied';
}
elseif ($hungry == '5') {
    $hunger = 'Satisfied';
}
elseif ($hungry = '6') {
    $hunger = 'Full';
}
elseif ($hungry = '7') {
    $hunger = 'Bloated';
}


#3 underparnv

underparnv
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts
  • LocationReno, Nevada

Posted 06 April 2006 - 08:34 PM

[!--quoteo(post=362282:date=Apr 6 2006, 10:14 AM:name=wildteen88)--][div class=\'quotetop\']QUOTE(wildteen88 @ Apr 6 2006, 10:14 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
if ($hungry == '0') {
    $hunger = 'Starving';
}
elseif ($hungry == '1') {
    $hunger = 'Famished';
}
elseif ($hungry == '2') {
    $hunger = 'Hungry';
}
elseif ($hungry == '3') {
    $hunger = 'Unsatisfied';
}
elseif ($hungry == '4') {
    $hunger = 'Unsatisfied';
}
elseif ($hungry == '5') {
    $hunger = 'Satisfied';
}
elseif ($hungry = '6') {
    $hunger = 'Full';
}
elseif ($hungry = '7') {
    $hunger = 'Bloated';
}
[/quote]
I personally prefer the switch statement:

<?php
switch ($hungry) {
    case 0:
        $hunger = "Starving";
        break;
    case 1:
        $hunger = "Famished";
        break;
    case 2:
        $hunger = "Hungry";
        break;
    case 3:
    case 4:
        $hunger = "Unsatisfied";
        break;
    case 5:
        $hunger = "Satisfied";
        break;
    case 6:
        $hunger = "Full";
        break;
    case 7:
        $hunger = "Bloated";
        break;
    default:
        $hunger = "Something other than 0-7...";
}
?>

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning."

The Sporting Edge - Free NFL Football Pool





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users