SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    SitePoint Addict
    Join Date
    Apr 2010
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    sort by form asc or desc

    Hi I have this query which display the data in ASC but now I am trying to create a drop down where the user can change to display asc or desc is it possible to change the value of the function depending on the option the users select?

    here the script that choose asc or desc

    PHP Code:
        $args = array(
            
    's' => $_GET['s'],
            
    'post_type' => 'deals',
            
    'orderby' => 'title',
            
    'order' => 'ASC',
            
    'paged' => get_query_var('paged')
        ); 
    This is my form
    PHP Code:
    <form name=frm action='thispage.php'>
             <
    select name=qryorder onchange='document.frm.submit()'>
              <
    OPTION VALUE='asc'>Locationselect</OPTION>
            <
    OPTION VALUE='asc'>Ascending</OPTION>
            <
    OPTION VALUE='desc'>Descending</OPTION>
         </
    select>
     </
    form
    So my plans was to set the as a variable that would be populated by the value of the options would this be the right way? somthing like
    PHP Code:
    'order' => '$variable',

     <
    OPTION VALUE='$variable = Asc'>Ascending</OPTION>
     <
    OPTION VALUE='$variable = DESC'>Descending</OPTION
    Please advise? thanks in adavance

  2. #2
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    According to your form, it will be available as a get variable.

    So what you can do is

    'order' => $_GET['qryorder'] == 'desc' ? 'desc' : 'asc',

  3. #3
    SitePoint Addict
    Join Date
    Apr 2010
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the help

    Do you mean like this

    PHP Code:
        $args = array(
            
    's' => $_GET['s'],
            
    'post_type' => 'deals',
            
    'orderby' => 'title',
            
    'order' => $_GET['qryorder'] == 'desc' 'desc' 'asc',
            
    'paged' => get_query_var('paged')
        ); 
    if so still not working I dont know if its cz i am missing something on the form
    PHP Code:
     <form  method="post" onchange='document.location.href=this.options[this.selectedIndex].value;'>
                                                    <
    select name=qryorder onchange='document.frm.submit()'>
                                                        <
    OPTION VALUE='asc'>Locationselect</OPTION>
                                                        <
    OPTION VALUE='desc'>Ascending</OPTION>
                                                        <
    OPTION VALUE='desc'>Descending</OPTION>
                                                    </
    select>
                                                </
    form

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,500
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Form:
    Code:
    <form name=frm action='thispage.php'>
        <select name='qryorder' onchange='document.frm.submit()'>
            <OPTION VALUE='ASC'>Ascending</OPTION>
            <OPTION VALUE='DESC'>Descending</OPTION>
        </select>
    </form>
    In the script that is called by the form:
    PHP Code:
     $qryorders = array('ASC''DESC');
     if (!
    in_array($_POST['qryorder'], $qryorders)) {
        
    $_POST['qryorder'] = 'ASC';
     }
     
    $args = array(
            
    's' => $_GET['s'],
            
    'post_type' => 'deals',
            
    'orderby' => 'title',
            
    'order' => $_POST['qryorder'],
            
    'paged' => get_query_var('paged')
        ); 
    Edit: took me too long to answer

  5. #5
    SitePoint Addict
    Join Date
    Apr 2010
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi thanks for the help but it doesnt do anything the page doesnt even move.
    The form
    PHP Code:
    <form name=frm action='search.php'>
        <
    select name='qryorder' onchange='document.frm.submit()'>
            <
    OPTION VALUE='ASC'>Ascending</OPTION>
            <
    OPTION VALUE='DESC'>Descending</OPTION>
        </
    select>
    </
    form
    The script
    PHP Code:
    $qryorders = array('ASC''DESC');
     if (!
    in_array($_POST['qryorder'], $qryorders)) {
        
    $_POST['qryorder'] = 'ASC';
     }
     
    $args = array(
            
    's' => $_GET['s'],
            
    'post_type' => 'deals',
            
    'orderby' => 'title',
            
    'order' => $_POST['qryorder'],
            
    'paged' => get_query_var('paged')
        );
        if(!empty(
    $tq)) {
            
    $args['tax_query'] = $tq;
        }
        
        
    query_posts($args); 

  6. #6
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,500
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by macaela View Post
    Hi thanks for the help but it doesnt do anything the page doesnt even move.
    You could try putting quotes around the form name, and since I used $_POST in the php script, you'll have to specify that method as well (I forgot that in my first post)
    Code:
    <form name="frm" method="post" action="search.php">

  7. #7
    SitePoint Addict
    Join Date
    Apr 2010
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi thanks but still no effect

    this how the whole script is looking now
    The script on here everything works fine except the asc and desc.

    PHP Code:
       if(!empty($_GET['dl']) || !empty($_GET['dc'])) {
        
            
    $tq = array(
                
    'relation' => 'AND'
            
    );
            if(!empty(
    $_GET['dl'])) {
                
    $tq[] = array(
                    
    'taxonomy' => 'deal-locations',
                    
    'field' => 'slug',
                    
    'terms' => array(strtolower($_GET['dl']))
                );
            }
            if(!empty(
    $_GET['dc'])) {
                
    $tq[] = array(
                    
    'taxonomy' => 'deal-categories',
                    
    'field' => 'slug',
                    
    'terms' => array(strtolower($_GET['dc']))
                );
            }
        }
        
    $qryorders = array('ASC''DESC');
     if (!
    in_array($_POST['qryorder'], $qryorders)) {
        
    $_POST['qryorder'] = 'ASC';
     }
     
    $args = array(
            
    's' => $_GET['s'],
            
    'post_type' => 'deals',
            
    'orderby' => 'title',
            
    'order' => $_POST['qryorder'],
            
    'paged' => get_query_var('paged')
        );
        if(!empty(
    $tq)) {
            
    $args['tax_query'] = $tq;
        }
        
        
    query_posts($args); 
    This is the asc and desc form

    PHP Code:
    <form name="frm" method="GET" action="search.php">
                <
    select name='qryorder' onchange='document.frm.submit()'>
                      <
    OPTION VALUE='ASC'>Ascending</OPTION>
                      <
    OPTION VALUE='DESC'>Descending</OPTION>
                </
    select>
           </
    form

  8. #8
    SitePoint Addict
    Join Date
    Apr 2010
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi guys thanks for all the help in the end I just opted to use a switch function is working greate.


    here the whole thing
    PHP Code:
    switch ($_POST['sortby'])  { 
       case 
    "ASC":
                
    $args = array(
            
    's' => $_GET['s'],
            
    'post_type' => 'deals',
            
    'orderby' => 'title',
            
    'order' => 'ASC',
            
    'paged' => get_query_var('paged')
        );
        break;
        case 
    "DESC":
                        
    $args = array(
            
    's' => $_GET['s'],
            
    'post_type' => 'deals',
            
    'orderby' => 'title',
            
    'order' => 'DESC',
            
    'paged' => get_query_var('paged')
        );
            break;
    default:
       case 
    "ASC":
                        
    $args = array(
            
    's' => $_GET['s'],
            
    'post_type' => 'deals',
            
    'orderby' => 'title',
            
    'order' => 'ASC',
            
    'paged' => get_query_var('paged')
        );
     
      } 
    PHP Code:
     <form method="post"  onChange="this.submit()">
                                                <
    select name="sortby">
                                                    <
    option value="">Sort By</option>
                                                    <
    option value="DESC">DESC</option>
                                                    <
    option value="ASC">ASC</option>
                                                </
    select>
                                                <
    input type="submit" value="submit">
                                            </
    form
    I am trying to make perfect by using a onchange without having to submit the form by clicking on the submit button but the onchange not working if anyoe spot what i am doing wrong please let me know.

    Thanks again for all the help

  9. #9
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Your form is missing the action variable and onChange="this.submit() should be in the select tag.

  10. #10
    SitePoint Addict
    Join Date
    Apr 2010
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi I have it like this with the onchange on the select
    PHP Code:
     <form method="post">
                                                <
    select name="sortby" onChange="this.submit()">
                                                    <
    option value="">Sort By</option>
                                                    <
    option value="DESC">DESC</option>
                                                    <
    option value="ASC">ASC</option>
                                                </
    select>
                                                <
    input type="submit" value="submit">
                                            </
    form
    Do I need to ad the onchange somewhere here??
    PHP Code:
    switch ($_POST['sortby'])  { 
       case 
    "ASC":
                
    $args = array(
            
    's' => $_GET['s'],
            
    'post_type' => 'deals',
            
    'orderby' => 'title',
            
    'order' => 'ASC',
            
    'paged' => get_query_var('paged')
        );
        break;
        case 
    "DESC":
                        
    $args = array(
            
    's' => $_GET['s'],
            
    'post_type' => 'deals',
            
    'orderby' => 'title',
            
    'order' => 'DESC',
            
    'paged' => get_query_var('paged')
        );
            break;

     
      }
      
      
          
    // get the post from the form (sortbyexpiry) and change the switch depending on the form value see form bellow
    switch ($_POST['sortbyexpire'])  { 
       case 
    "ASC":
                
    $args = array(
            
    's' => $_GET['s'],
            
    'post_type' => 'deals',
            
    'orderby' => 'expiry',
            
    'order' => 'ASC',
            
    'paged' => get_query_var('paged')
        );
        break;
        case 
    "DESC":
                        
    $args = array(
            
    's' => $_GET['s'],
            
    'post_type' => 'deals',
            
    'orderby' => 'expiry',
            
    'order' => 'DESC',
            
    'paged' => get_query_var('paged')
        );
            break;
     
      } 

  11. #11
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    onChange="this.submit() is only needed in the form. Your switch statement is used for checking which one was selected. Note that $_POST['sortbyexpire'] will never be used since it was not in the form.

  12. #12
    SitePoint Addict
    Join Date
    Apr 2010
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry I do have two forms I just didnt wana post the both forms one is sortbyexpire which call the switch (sortbyexpire) and the other is form called sortby which has another switch ( sortby) but i am trying to implement the onchange on the forms but the onchange doesnt seem to be working so i am stuck with the submit button, which I prefer is the onchange but it doesnt seem to be working.
    Do I have the onchange correctly on the form or not? cz i been reading that you cannot use onchange on php script only javascript, is that why my onchange isnt working?
    <select name="sortby" onChange="this.submit()">

  13. #13
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I tested it and it works as expected. Here is the code I used:

    PHP Code:
    <form method="post" action="sortby.php">
       <select name="sortby" onChange="this.submit()">
          <option value="">Sort By</option>
          <option value="DESC">DESC</option>
          <option value="ASC">ASC</option>
       </select>
       <select name="sortbyexpire" onChange="this.submit()">
          <option value="">Sort By</option>
          <option value="DESC">DESC</option>
          <option value="ASC">ASC</option>
       </select>
       <input type="submit" value="submit">
    </form>

    <?php
       
    switch ($_POST['sortby'])
       { 
           case 
    "ASC": echo "<h3>ASC</h3>";
               
    $args = array(
                     
    's' => $_GET['s'],
                     
    'post_type' => 'deals',
                     
    'orderby' => 'title',
                     
    'order' => 'ASC',
                     
    'paged' => 'paged'
                    
    );
               break;
           case 
    "DESC": echo "<h3>DESC</h3>";
               
    $args = array(
                     
    's' => $_GET['s'],
                     
    'post_type' => 'deals',
                     
    'orderby' => 'title',
                     
    'order' => 'DESC',
                     
    'paged' => 'paged'
                    
    );
               break;
       }
      
      
          
    // get the post from the form (sortbyexpiry) and change the switch depending on the form value see form bellow
       
    switch ($_POST['sortbyexpire'])
       { 
           case 
    "ASC": echo "<h3>sortbyexpire: ASC</h3>";
               
    $args = array(
                     
    's' => $_GET['s'],
                     
    'post_type' => 'deals',
                     
    'orderby' => 'expiry',
                     
    'order' => 'ASC',
                     
    'paged' => 'paged'
                    
    );
               break;
           case 
    "DESC": echo "<h3>sortbyexpire: DESC</h3>";
               
    $args = array(
                     
    's' => $_GET['s'],
                     
    'post_type' => 'deals',
                     
    'orderby' => 'expiry',
                     
    'order' => 'DESC',
                     
    'paged' => 'paged'
                    
    );
               break;
       }
    ?>

  14. #14
    SitePoint Addict
    Join Date
    Apr 2010
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does your onchange work? cz the only way it does the sortby is by clicking the submit button i wanted without having to click submit

  15. #15
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    If you don't want to click on the submit button then change
    onChange="this.submit() to
    onChange="this.form.submit() and then remove
    <input type="submit" value="submit">

  16. #16
    SitePoint Addict
    Join Date
    Apr 2010
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Like this nope it didint work?

    PHP Code:
     <form method="post">
                                               <
    select name="sortby" onChange="this.form.submit()">
                                                  <
    option value="">Sort By alpha</option>
                                                  <
    option value="DESC">DESC</option>
                                                  <
    option value="ASC">ASC</option>
                                               </
    select>
                                               <
    select name="sortbyexpire" onChange="this.form.submit()">
                                                  <
    option value="">Sort By expiry</option>
                                                  <
    option value="DESC">DESC</option>
                                                  <
    option value="ASC">ASC</option>
                                               </
    select>
                                           </
    form

  17. #17
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Again, it works for me. Check it out yourself:

    http://www.w-cms.org/test/sortby.php

    Hope other members can comment on this.

  18. #18
    SitePoint Addict
    Join Date
    Apr 2010
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi thanks for the help I am not sure if is cz i am usning wordpress or a if I need to configure something with php ini but yes your example working exactly how mine should.

    Will keep working on it thanks alot for all the help.


Bookmarks

Posting Permissions

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