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 5 of 5
  1. #1
    New Coder
    Join Date
    Feb 2005
    Location
    France
    Posts
    89
    Thanks
    0
    Thanked 0 Times in 0 Posts

    'Sort by' problem

    Hello again everybody

    I'm facing a small problem.
    Code:
    <form name="f" action="finished.php" method="post">
    
    Sort by: <select name="sortby">
    <option value="Game asc">Game (ascending)</option>
    <option value="Game desc">Game (descending)</option>
    ...
    <option value="Opponent desc">Opponent (descending)</option>
    </select>
    <input type="submit" name="go" value="Go!">
    </form>
    PHP Code:
    $query1 'SELECT * FROM finished ORDER BY number';
    $query2 'SELECT * FROM finished ORDER BY number DESC';
    ...
    $query8 'SELECT * FROM finished ORDER BY opponent DESC';

    if ( !isset (
    $go) ) { // Here is the problem... me thinks... :S
        
    $r mysql_query ($query1); 
    } else {
        if (
    $sortby == 'Game asc') {
            
    $r mysql_query ($query1); 
        }
        if (
    $sortby == 'Game desc') {
            
    $r mysql_query ($query2); 
        }

        ...

        if (
    $sortby == 'Opponent desc') {
            
    $r mysql_query ($query8); 
        }

    Now THE problem is that whatever I choose in Sort by' and hit 'go' the entries are sorted and displayed BUT the option shown in the box goes back to "Game ascending" so I actually don't know what kind of sorting it was!!

    Please drop me a hint how the problem can be sorted out!
    Thank you in advance!
    Have an excellent weekend!

  • #2
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If you're displaying the results on the same page, you can find out which option should be selected using the following:
    PHP Code:
    <option value="Game asc"<?php echo (isset($sortby) && $sortby == 'Game asc' ' selected="selected" : ''); ?>>Game (ascending)</option>
    <option value="Game desc"<?php echo (isset($sortby) && $sortby == '
    Game desc' ? ' selected="selected" ''); ?>>Game (descending)</option>
    .
    .
    etc

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Is this:
    Code:
    <form name="f" action="finished.php" method="post">
    
    Sort by: <select name="sortby">
    <option value="Game asc">Game (ascending)</option>
    <option value="Game desc">Game (descending)</option>
    ...
    <option value="Opponent desc">Opponent (descending)</option>
    </select>
    <input type="submit" name="go" value="Go!">
    </form>
    within a php script? Cause if it is, you can have it dynamically set by the name. As well, I don't know why you have like 8 possible queries there, you can build it dynamically as well:
    PHP Code:
    <?php
    if (empty($_REQUEST))
    {
         
    $_REQUEST array_merge($_GET$_POST);
    }

    if (!isset(
    $_REQUEST['do']))
    {
         
    $_REQUEST['do'] = 'search';
    }

    if (
    $_REQUEST['do'] == 'search')
    {
    ?>
    <form name="f" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
    Sort By: <select name="sortby">
    <?php
    $options 
    = array(
         
    g_asc => 'Game (Ascending)',
         
    g_desc => 'Game (Descending)',
         .....
         
    opp_desc => 'Opponent (Descending)',
    );

    if (
    is_array($options))
    {
         foreach (
    $options AS $opval => $opname)
         {
              if (
    $opval == $_REQUEST['sortby'])
              {
                   
    $selected ' selected="selected"';
              }
              else
              {
                   
    $selected '';
              }
              echo 
    "<option value=\"" $optval $selected "\">" $optname "</option>\n";
         }
    }
    ?>
    </select>
    <input type="hidden" name="do" value="dosearch" />
    <input type="submit" name="go" value="Go!">
    </form>
    <?php
    }

    if (
    $_REQUEST['do'] == 'dosearch')
    {
         
    // Get the sorted options by its underscore split:
         
    $split explode('_'$_POST['sort']);
         
    $orderby '';
         
    $direction '';

         switch(
    $split[0])
         {
              case 
    'g':
                   
    $orderby 'number'// Maybe number?
                   
    break;
              case 
    'opp':
                   
    $orderby 'opponent';
                   break;
         }
         switch(
    $split[1])
         {
              case 
    'asc':
                   
    $direction 'ascending';
                   break;
              case 
    'desc':
                   
    $direction 'descending';
                   break;
         }

    if (!empty(
    $orderby))
    {
         
    $orderby ' ORDER BY ' $orderby;
    }
    if (!empty(
    $direction))
    {
         
    $direction ' ' $direction;
    }
    $query "SELECT * FROM `finished`" $orderby$direction;

    $r mysql_query($query);

    /* etc. */
    }
    Untested and unsure of all initializations. Hope that helps!

    Edit:
    Man... took me ten minutes to type that. I'm slipping, lol.

  • #4
    Regular Coder
    Join Date
    Jun 2003
    Location
    Australia
    Posts
    528
    Thanks
    8
    Thanked 8 Times in 8 Posts
    A small suggestion.. its no good for you now as you've already typed it.. but I'm bored so I'll suggest it anyway...

    To save typing ALL those options and ALL those queries, an alternative may have been:

    <form name="f" action="finished.php" method="post">

    Sort: <select name="sort">
    <option value="game">By Game</option>
    <option value="opponent">By Opponent</option>
    </select>
    Order: <select name="order">
    <option value="asc">Ascending</option>
    <option value="desc">Descending</option>
    </select>
    <input type="submit" name="go" value="Go!">
    </form>

    Then on your finsihed.php page:

    PHP Code:
    mysql_query('SELECT * FROM finished ORDER BY $sort $order'); 
    1 line would fix it all

  • #5
    New Coder
    Join Date
    Feb 2005
    Location
    France
    Posts
    89
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you very much for your help, guys. I'm an absolut rookie and I highly appreciate your time and consideration

    Have a wonderful week


  •  

    Posting Permissions

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