Order by with php

Discussion in 'PHP' started by cuni18, Aug 21, 2008.

  1. #1
    Hi
    Can anyone help me

    i have database with data and now i like an option when uses looks at categories to sort data by hits and date

    if he clicks on link hits data automaticly orders by hits
    or date then data automaticly orders by data..

    can someone help me

    here is the page category

    http://www.netbaza.net/index.php?cat_id=24

    sample code this code orders by hits but i want a second option when it ordes by date also
    $sql = mysql_query("SELECT * FROM netbaza_movies WHERE catergory_id=".$cat_id." AND published=1 ORDER BY hits DESC LIMIT $from, $max_results"


    Thanks
     
    cuni18, Aug 21, 2008 IP
  2. William[ws]

    William[ws] Peon

    Messages:
    47
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    $sql = mysql_query("SELECT * FROM netbaza_movies WHERE catergory_id=".$cat_id." AND published=1 ORDER BY hits,date DESC LIMIT $from, $max_results"

    And just add a , after date and add other rows u wanna order by after that
     
    William[ws], Aug 21, 2008 IP
  3. cuni18

    cuni18 Well-Known Member

    Messages:
    314
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    105
    #3
    i didn#T mean like that i want to have like two links on my category page and when you click link 1 data sorts by date and when you click link 2 data sorty by hits

    thanks
     
    cuni18, Aug 21, 2008 IP
  4. nice.wallpapers

    nice.wallpapers Active Member

    Messages:
    142
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    60
    #4
    Hi ,

    I have seen your website you want sort system on them right "Date Aded | Top Rated | Most Watched"

    Thanks,
     
    nice.wallpapers, Aug 21, 2008 IP
  5. lfhost

    lfhost Peon

    Messages:
    232
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #5
    add links like

    
    <a href="index.php?cat_id=<?php echo intval($_GET['cat_id']);?>&sort=1">Hits</a> | <a href="index.php?cat_id=<?php echo intval($_GET['cat_id']);?>&sort=2">Date</a> | <a href="index.php?cat_id=<?php echo intval($_GET['cat_id']);?>&sort=3">Title</a>
    PHP:
    you then need to run an if statement or switch to get the sort order

    
    $sort = intval($_GET['sort']);
    if(!empty($_GET['sort']) && $sort == 3)
    {
        $orderby = " ORDER BY title ASC ";
    }
    elseif(!empty($_GET['sort']) && $sort == 2)
    {
        $orderby = " ORDER BY date DESC ";
    }
    else
    {
        //this is our default
        $orderby = " ORDER BY hits DESC ";
    }
    
    $sql = mysql_query("SELECT * FROM netbaza_movies WHERE catergory_id=".$cat_id." AND published=1 ".$orderby." LIMIT $from, $max_results");
    
    PHP:
     
    lfhost, Aug 21, 2008 IP
  6. cuni18

    cuni18 Well-Known Member

    Messages:
    314
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    105
    #6
    thanks :) it works :)
     
    cuni18, Aug 22, 2008 IP
  7. William[ws]

    William[ws] Peon

    Messages:
    47
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    good stuff lfhost got to it before me .
     
    William[ws], Aug 22, 2008 IP