Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Dec 2003
    Location
    Las Vegas
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Incrementing variables

    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!

  • #2
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Incrementing
    PHP Code:
    // Incrementing
    $i++;
    // Decrementing
    $i--; 
    If statement:
    PHP Code:
    if(true)
    {
      
    // Code
    }
    else
    {
      
    // code

    Ternary expression (assigning variables):
    PHP Code:
    $my_variable = ($foo == true) ? 'true' 'false'
    I would also read about the SQL function COUNT()

  • #3
    Regular Coder
    Join Date
    May 2002
    Location
    Virginia, USA
    Posts
    621
    Thanks
    0
    Thanked 6 Times in 6 Posts
    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:

    PHP Code:
    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:

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

    hth,
    -Celt


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •