Welcome Guest, Not a member yet? Register   Sign In
problem with ordering mysql results
#2

[eluser]Frank Berger[/eluser]
Hi,

try this for your case-block:
Code:
switch($sort_by_case){
        case 0:
            $sort_by_query = ('SELECT artist_id, artist_name, artist_country FROM artists Order by artist_name');
            break;
        case 1:
            $sort_by_query = ('SELECT artist_id, artist_name, artist_country FROM artists order by artist_country');
            break;
        case 2:
            $sort_by_query = ('SELECT artist_id, artist_name, artist_country FROM artists order by view_count');
             break;
        default:
             // bad, something went wrong
             exit;
}

Unless you want the effect and chain the cases together you _have_ to add the break; statement to the end of each case. Furthermore every switch should have a default: case, even if it is the same as another case or empty.

switch in php works like this: it will look for the first matching case and execute all code after this case (even if the code is part of another case following the found statement) until the end of the switch loop or until it encounters a break; or continue; statement.

This behavior can be useful to avoid code duplication, but makes the code harder to understand and opens possible security holes and bugs. So to be on the safe side, always add the breaks; and the default:-case.

more info on this here: http://php.net/switch

cheers
Frank


Messages In This Thread
problem with ordering mysql results - by El Forum - 09-07-2008, 06:06 PM
problem with ordering mysql results - by El Forum - 09-07-2008, 07:25 PM
problem with ordering mysql results - by El Forum - 09-07-2008, 07:29 PM



Theme © iAndrew 2016 - Forum software by © MyBB