Sorting my records using the Jump menu

Discussion in 'PHP' started by adsegzy, Aug 11, 2010.

  1. #1
    Hello Friends,

    I have a page hwere i echo out my search results from my mysql database. by defualt the echo results are sort by date desc. but i want to jump menu that will have the following;

    Sort by Date in Ascendind order
    Sort by Date in Descending order
    Sort by IP in Ascendind order
    Sort by IP in Descending order
    Sort by Location

    What i want to do is that if i select "Sort by IP in Ascendind order" from the jump menu, the result on the page should be sorted by IP in ascending order, or if i select "Sort by Location" the result should be sorted by Location on this same page.

    Plsease how do i go about it?

    Regards
    adsegzy
     
    adsegzy, Aug 11, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    This should get you started, replace the table and column names accordingly.

    <?php
    $sort = NULL;
    if (isset($_POST['submit'])) {
        if ($_POST['sort'] == 'date_asc') {
            $sort = 'ORDER BY date ASC';
        } elseif ($_POST['sort'] == 'date_desc') {
            $sort = 'ORDER BY date DESC';
        } elseif ($_POST['sort'] == 'ip_asc') {
            $sort = 'ORDER BY ip ASC';
        } elseif ($_POST['sort'] == 'ip_desc') {
            $sort = 'ORDER BY ip DESC';
        } elseif ($_POST['sort'] == 'location') {
            $sort = 'ORDER BY location';
        }
    }
    
    $result = mysql_query("SELECT something FROM table_name {$sort}");
    
    //now use the $result....
    ?>
    <form method="POST">
    <select name="sort">
    <option value="date_asc">Sort by Date in Ascending order</option>
    <option value="date_desc">Sort by Date in Descending order</option>
    <option value="ip_asc">Sort by IP in Ascending order</option>
    <option value="ip_desc">Sort by IP in Descending order</option>
    <option value="location">Sort by Location</option>
    </select>
    <input type="submit" name="submit" value="Sort" />
    </form>
    PHP:
     
    danx10, Aug 11, 2010 IP