We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 32017
    • 151 Posts
    Ive been reading for 2 days the dozens of threads using filters and all sorts but all i want to do is have a drop down list on my front end which effectively changes the order by field so i can then sort results in however way I choose.

    What i have done so far

    Chunk
    <form action="[~[*id*]~]" method="post">
    <select name="sort">
    <option value="pricelow">Price Cheapest First</option>
    <option value="pricehigh">Price Most Expensive</option>
    <option value="starhigh">Star Rating High</option>
    <option value="starlow">Star Rating Low</option>
    <option value="title">A-Z</option>
    <option value="new">Newest First</option>
    </select>
    <input name="Submit" type="submit" value="Sort" />
    </form>
    
    [!hotel-list!]
    
    


    hotel-list Snippet

    <?php
    global $modx;
    $order = "price ASC";
    if (!empty($_POST['pricelow'])) $order .= (empty($order)?"":"|")."tvprice,".mysql_escape_string($_POST['price ASC']).",1";
    
    if (!empty($_POST['pricehigh'])) $order .= (empty($order)?"":"|")."tvprice,".mysql_escape_string($_POST['price DESC']).",1";
    
    if (!empty($_POST['starhigh'])) $order .= (empty($order)?"":"|")."tvstar-rating,".mysql_escape_string($_POST['star-rating ASC']).",1";
    
    if (!empty($_POST['starlow'])) $order .= (empty($order)?"":"|")."tvstar-rating,".mysql_escape_string($_POST['star-rating DESC']).",1";
    
    if (!empty($_POST['title'])) $order .= "menuindex";
    
    if (!empty($_POST['new'])) $order .= "createdon";
    
    return $modx->runSnippet('Ditto', array('parents'=>'8', 'tpl'=>'hotel_summary', 'orderBy'=>$order, 'summarize'=>'55', 'display'=>'10', 'paginateAlwaysShowLinks'=>'1', 'paginate'=>'1', 'noResults'=>'no results'));
    ?>
    
    ?>
    



    No working sad

    Can you see what im trying to do? just change the order parameter based on a drop down selection.

    Anyone able to help ?
      London web design - MODx specialists
      • 16278
      • 928 Posts
      When I have set &orderBy from config files, I have needed to make it a complex array, like so:
      $orderBy = array('parsed'=>array(), 'custom'=>array(), 'unparsed'=>'pkDate DESC, menuindex DESC');


      So you could try it like this:
      $orderBy = array('parsed'=>array(), 'custom'=>array(), 'unparsed'=>$order;
      return $modx->runSnippet('Ditto', array('parents'=>'8', 'tpl'=>'hotel_summary', 'orderBy'=>$orderBy, 'summarize'=>'55', 'display'=>'10', 'paginateAlwaysShowLinks'=>'1', 'paginate'=>'1', 'noResults'=>'no results'));
      


      :) KP
        • 32017
        • 151 Posts
        You mean just add that line to my snippet call? because no joy,

        im starting to think my if statements dont work


        if (!empty($_POST['starhigh'])) $order .= (empty($order)?"":"|")."tvstar-rating,".mysql_escape_string($_POST['star-rating ASC']).",1";


        basically if the drop down list star rating high is selected just have the orderby change

        its not working sad


        So ive tried this ....

        if($_REQUEST["sort"] == "latest") {$order = 'createdon';} 


        and the $order does return the correct code but theres a parse error in front end..

        You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'pagetitle' at line 4 »

        [ed. note: waynep16 last edited this post 12 years, 4 months ago.]
          London web design - MODx specialists
          • 16278
          • 928 Posts
          You always have to specify the sort direction (ASC or DESC) with orderBy:
          if($_REQUEST["sort"] == "latest") {$order = 'createdon DESC';}


          The snippet code looks a bit confused too, on closer inpection. You have a single-selection list, so no need to build up a multiple orderBy string. And if you did need to do that, the separator for orderBy would be a comma, not a vertical bar (possibly you picked that up from posts about filters?). I'll see if I can suggest a correct and simpler verion.

          :) KP

          [ed. note: kp52 last edited this post 12 years, 4 months ago.]
            • 16278
            • 928 Posts
            Try this:
            <?php
            
            switch ($_POST['sort']) {
            	case 'pricelow':
            		$order = 'price ASC';
            		break;
            	case 'pricehigh':
            		$order = 'price DESC';
            		break;
            	case 'starhigh':
            		$order = 'star-rating DESC';
            		break;
            	case 'starlow':
            		$order = 'star-rating ASC';
            		break;
            	case 'title':
            		$order = 'menuindex ASC';
            		break;
            	case 'new':
            		$order = 'createdon DESC';
            		break;
            	default:
            		$order = 'price ASC';
            }
            
            return $modx->runSnippet('Ditto', array('parents'=>'8', 'tpl'=>'hotel_summary', 'orderBy'=>$order, 'display'=>'10', 'paginateAlwaysShowLinks'=>'1', 'paginate'=>'1', 'noResults'=>'no results'));
            ?>


            Note: I did a limited test and found no need to make $order an array in this case. You only need to make $modx global in your own functions. And &summarize is obsolete, but if given, it will overwrite &display.

            :) KP
              • 32017
              • 151 Posts
              Genuis Cant thank you enough smiley

              works perfect.

              I changed this 'parents'=> $parents, so it wasn't a hard-coded number





                London web design - MODx specialists