how can i create a form that sort query asc or desc?

Discussion in 'PHP' started by macaela, Apr 27, 2012.

  1. #1
    Hi I am trying to create a query that will sort the products display in ASC or DESC depending on the option the user selects from teh drop down.
    I thought a switch statement would be a good option to achive this so this is how I have tried so far but fail miserable.

    The switch statment
    switch ($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;
    }
    PHP:
    The form
                                               <form name="myForm">
                                               		<select id="sortby"> 
                                                         <option value="ASC">ASC</option>
                                                          <option value="DESC">DESC</option>
                                                          <!-- <option value="pc">Postcode</option> -->
                                                    </select>
                                               </form>
    PHP:
    Can someone help me out please
     
    macaela, Apr 27, 2012 IP
  2. Oli3L

    Oli3L Active Member

    Messages:
    207
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    70
    #2
    what's the problem? i don't get it

    SELECT * FROM `table` WHERE `title` = 'hello world' ORDER BY `id` DESC

    EDITED:

    on a second look, change id="sortby" to name="sortby"
     
    Oli3L, Apr 27, 2012 IP
  3. macaela

    macaela Active Member

    Messages:
    181
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #3
    Hi this is what i am trying to achieve the clause WHERE= $variable bacause the drop down will be coming froma drop down.

          // get the post from the form (sortbyexpiry) and change the switch depending on the form value see form bellow
       switch ($_POST['location'])
       { 
           case "location": echo "<h3>location: $variable </h3>";
               $args = array(
                     's' => $_GET['s'],
                     'post_type' => 'deals',
                     'WHERE' => ' $variable',
                     'paged' => 'paged'
                    );
               break;
       } 
    PHP:
     
    macaela, Apr 27, 2012 IP
  4. Oli3L

    Oli3L Active Member

    Messages:
    207
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    70
    #4
    $variable = isset($_POST['sortby']) ? ($_POST['sortby'] == "DESC" ? "DESC" : "ASC") : "DESC";

    is that it?
     
    Oli3L, Apr 27, 2012 IP