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

[eluser]Ramania[/eluser]
hello people,

i'm thankful again for this great framework and this great community of professionals, and as much as i love finding out how things work by my own as much as it makes me bothered to write a question, but since the idea of any community is to help, i'd be thankful if someone asked the following two questions:

1. i am trying to make it possible to sort data either by different factors, but the result in the listing page keeps giving me the same order, even though the query is correct and it works!, here's the code below:

Code:
function Index(){
    
    #get sorting method 0=name, 1=country, 3=populartiy.
    if ($this->uri->segment(3) ==  null){
    $content['sortby'] = "0";
    $sort_by_case = "0";    
    } else {
    $content ['sortby'] = $this->uri->segment(3);
    $sort_by_case = $this->uri->segment(3);
    }
    #do the query based on the sorting method.
    switch($sort_by_case){
        case 0:
            $sort_by_query = ('SELECT artist_id, artist_name, artist_country FROM artists Order by artist_name');
        case 1:
            $sort_by_query = ('SELECT artist_id, artist_name, artist_country FROM artists order by artist_country');
        case 2:
            $sort_by_query = ('SELECT artist_id, artist_name, artist_country FROM artists order by view_count');
            }

    $content['query'] = $this->db->query($sort_by_query);
    $content['viewname'] = "list_artists";
    $this->load->view('mainview',$content);
    
    
    }

and here is the code for the view page

Code:
<?
                    $sorting_options = array ('By name','By country','By populartiy');
                    $js_onchange =  'onChange = "get_item();"';
                    echo "<form name=\"rami\">";
                    echo "Sorting options:".form_dropdown('artists',$sorting_options,$sortby,$js_onchange);
                    echo "</form>";
                            ?>
                    <ul>
                    &lt;?php foreach ($query->result() as $item): ?&gt;
                    <li>
                    
                    &lt;?php
                    echo anchor('list_artists/artistpage/'.$item->artist_id ,$item->artist_name."- ".$item->artist_country);
                    ?&gt;

                    </li>
                    &lt;?php endforeach; ?&gt;
                    </ul>

now i know i am not a professional coder, but since this's my first application so far, i am trying my best to write the "best" code.

the second question is: am i doing extra work in my code? or it's ok? i don't like shortcuts, but since it's web, and bandwidth is involved then the lesser the code the best the results! or that's what i guess!

thank you folks, i appreciate your time.

ps: the uri third segment is routed correctly and i tested it many times, as i tried an echo to make sure everything work, and it's passed through a javascript as the following..

Code:
function get_item()
                    {
                    [removed] = "http://localhost/mmdportal/index.php/list_artists/sortby/"+document.rami.artists.value;
                    }

the removed code refers to the document location.
#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
#3

[eluser]Ramania[/eluser]
thank you very much for the reply, i applied it and it worked wonderfully, and i fixed the case 2 so it'll be descending too, Big Grin thnx mate.




Theme © iAndrew 2016 - Forum software by © MyBB