...

View Full Version : Incrementing variables



ccwebstuff
09-27-2004, 06:48 PM
I know this may sound dumb - in other languages this would have been SO easy ... but here goes.

I'm trying to step through entire database to determine how many of each item ...

while ( $row = mysql_fetch_array($result) ) {
// process the row...
$cStatus = trim( $ROW["status"])
$winshow = $winshow + if( $cStatus == "WIN", 1, 0)
$loseshow = $loseshow + if( $cStatus == "Lose", 1, 0)
$pushshow = $pushshow + if( $cStatus == "PUSH", 1, 0)

}

Needless to say this code DOES not work
tried using a IF statement using
$winshow++
and each of the 3 numbers got the exact same quantity which I know is not correct.

syntax I was familiar with was

do case
case cStatus = "WIN"
winshow++
case cStatus = "Lose"
loseshow++
case cStatus = "Push"
pushshow++
end

Any help in this stupid and probably obvious error would be greatly appreciated!

marek_mar
09-27-2004, 07:11 PM
Incrementing


// Incrementing
$i++;
// Decrementing
$i--;

If statement:


if(true)
{
// Code
}
else
{
// code
}

Ternary expression (assigning variables):


$my_variable = ($foo == true) ? 'true' : 'false';

I would also read about the SQL function COUNT()

Celtboy
09-27-2004, 07:21 PM
You have *several* issues with your code. Makes me feel like you've got experience with other languages, but not with php syntax.

Nothing too major, but here's the cleanup:



while ( $row = mysql_fetch_array($result) ) {
// process the row...
$cStatus = trim($row["status"]);

switch ($cStatus) {
case "WIN" :
$windshow++;
break;
case "Lose" :
$losehow++;
break;
case "PUSH" :
$pushshow++;
break;
deafult :
echo "Status returned was: " . $cStatus . ", which is an unknown value";
break;
}
}



That should do it. You were attempting to use the ternary operator in your statements. The correct syntax for that is:

(expression) ? return_true : return_false;

you would have wanted:



...
$winshow += ($cStatus == "WIN") ? 1 : 0;
$loseshow += ($cStatus == "Lose") ? 1 : 0;
$pushshow += ($cStatus == "PUSH") ? 1 : 0;
...



hth,
-Celt



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum