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

	$args = array(
		's' => $_GET['s'],
		'post_type' => 'deals',
		'orderby' => 'title',
		'order' => 'ASC',
		'paged' => get_query_var('paged')
	);

This is my form

<form name=frm action='thispage.php'>
         <select name=qryorder onchange='document.frm.submit()'>
      	<OPTION VALUE='asc'>Location: select</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

'order' => '$variable',

 <OPTION VALUE='$variable = Asc'>Ascending</OPTION>
 <OPTION VALUE='$variable = DESC'>Descending</OPTION>

Please advise? thanks in adavance

According to your form, it will be available as a get variable.

So what you can do is

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

Thanks for the help

Do you mean like this

	$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

 <form  method="post" onchange='document.location.href=this.options[this.selectedIndex].value;'>
                                                <select name=qryorder onchange='document.frm.submit()'>
                                                	<OPTION VALUE='asc'>Location: select</OPTION>
                                                    <OPTION VALUE='desc'>Ascending</OPTION>
                                                    <OPTION VALUE='desc'>Descending</OPTION>
                                                </select>
                                            </form>

Form:


<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:


 $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 :slight_smile:

Hi thanks for the help but it doesnt do anything the page doesnt even move.
The form

<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

$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);
	

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)


<form name=[B][COLOR="#FF0000"]"frm" [/COLOR][/B][B][COLOR="#FF0000"]method="post"[/COLOR][/B] action="search.php">

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.

   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

<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>

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

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')
	);

  }
 <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

Your form is missing the action variable and onChange="this.submit() should be in the select tag.

Hi I have it like this with the onchange on the select

 <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??

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;

  }

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.

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()”>

I tested it and it works as expected. Here is the code I used:

<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;
   }
?>

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

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”>

Like this nope it didint work?

 <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>

Again, it works for me. Check it out yourself:

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

Hope other members can comment on this.

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.