Sort custom search results
Permalink
Hello all, I have a site using a custom search that returns property listings. The code is below. It works great, however it's not sorting the results in any way. There are Platinum, Gold, Silver and FREE properties. I need them to sort by the "package_type" attribute in that order.
<?php defined('C5_EXECUTE') or die("Access Denied."); $th = Loader::helper('text'); $im = Loader::helper('image'); $nh = Loader::helper('navigation'); //echo "<pre>"; //print_r($pages); //echo "</pre>"; //die(); if(count($pages) > 0){ //make sure there is at least one featured page if($pages[0]->getAttribute('is_featured')){ ?> <div class="property_list_wrapper"> <div class="col-xs-12">
Viewing 15 lines of 142 lines. View entire code block.
<?php defined('C5_EXECUTE') or die("Access Denied."); $th = Loader::helper('text'); $im = Loader::helper('image'); $nh = Loader::helper('navigation'); //echo "<pre>"; //print_r($pages); //echo "</pre>"; //die(); if(count($pages) > 0){ //make sure there is at least one featured page if($pages[0]->getAttribute('is_featured')){ ?> <div class="property_list_wrapper"> <div class="col-xs-12"> <div class="alg-property-list-feature-slider-title">Featured Property</div> <div class="alg-property-list-feature-slider-wrap"> <p class="slideIndex"></p> <script> $('.flexslider').flexslider({ after: function(slider) { slider.find('.slideIndex').html(slider.currentSlide + 1); } }); </script> <div class="cycle-slideshow" data-cycle-slides="> div.alg-property" data-cycle-speed="1000" data-cycle-timeout="7000" data-cycle-pause-on-hover="true" data-cycle-fx="carousel" data-cycle-auto-height="calc"> <?php foreach($pages as $page){ //since we're ordering by is_featured desc we can just break out of the loop once we've gotten all the featured properties if(!$page->getAttribute('is_featured')){ break; } $image = $page->getAttribute('property_list_image'); ?> <div class="alg-property"> <div class="alg-property-list-feature-slider-headline"><?php echo $th->entities($page->getCollectionName()); ?></div> <div class="row alg-property-list-feature-slider-inner-row"> <div class="col-md-5 featured_slider_image"> <?php if($image) $im->output($image, $th->entities($page->getCollectionName())); ?> </div> <div class="col-md-6"> <div class="alg-property-list-feature-slider-address"> <?php echo $th->entities($page->getAttribute('street_address')); ?> <br /> <?php echo $th->entities($page->getAttribute('city')); ?>, <?php echo $th->entities($page->getAttribute('state')); ?> <?php echo $th->entities($page->getAttribute('zip_code')); ?> <br /> <?php echo $th->entities($page->getAttribute('property_phone')); ?> </div> <hr> <div class="alg-property-list-feature-slider-short-desc col-sm-12"> <?php //$description = $page->getCollectionDescription(); $description = $page->getAttribute('property_description'); if (strpos($description, '<p>') !== 0) { $description = '<p>'.$description.'</p>'; } //echo $description; ?> <?php echo substr($description, 0, 320); ?>... </div> <div class="col-sm-12"> <a class="button alg-property-list-feature-slider-learnmore" href="<?php echo $nh->getCollectionURL($page); ?>">Learn More</a> </div> </div> </div> </div> <?php } ?> <div class="cycle-prev"><span></span></div> <div class="cycle-next"><span></span></div> <!--<button data-cycle-cmd="pause">Pause</button>--> </div> </div> </div> <?php } ?> <div class="sbs_plp_pageResults properties col-xs-12 col-sm-9"> <?php //$pagelist->sortBy('package_type'); //$pageList->sortBy('package_type', 'asc'); foreach($pages as $page){ //print_r($page); $image = $page->getAttribute('property_list_image'); //$featured = $page->getAttribute('is_featured'); ?> <?php //echo $th->entities($page->getAttribute('is_featured')); ?> <?php $inactive = $page->getAttribute('inactive'); if ($inactive) { ?> <div class="col-xs-12 property_list_item_wrapper hidden"> <?php } else { ?> <div class="col-xs-12 property_list_item_wrapper"> <?php } ?> <div class="col-xs-12 col-md-5 property_list_image"> <a href="<?php echo $nh->getCollectionURL($page); ?>" title="<?php echo $th->entities($page->getCollectionName()); ?>"> <?php if($image) $im->output($image, $th->entities($page->getCollectionName())); ?> <?php $featured = $page->getAttribute('package_type'); //echo $featured = $page->getAttribute('package_type'); ?> <div class="package_type"><?php echo $th->entities($page->getAttribute('package_type')); ?></div> <?php //echo $th->entities($page->getAttribute('is_featured')); ?> </a> </div> <div class="col-xs-12 col-md-4 property_list_content"> <h3 class="ccm-page-list-title"> <a href="<?php echo $nh->getCollectionURL($page); ?>" title="<?php echo $th->entities($page->getCollectionName()); ?>"> <?php echo $th->entities($page->getCollectionName()); ?> </a> </h3> <div class="ccm-page-list-description"> <p><?php echo $th->entities($page->getAttribute('street_address')); ?><br /><?php echo $th->entities($page->getAttribute('city')); ?>, <?php echo $th->entities($page->getAttribute('state')); ?> <?php echo $th->entities($page->getAttribute('zip_code')); ?></p> <h2><?php echo $th->entities($page->getAttribute('property_phone')); ?></h2> </div> <div class="check_availability"> <a href="<?php echo $nh->getCollectionURL($page); ?>" title="<?php echo $th->entities($page->getCollectionName()); ?>"><?php echo t('Check Availability'); ?></a> </div> </div> <div class="unit_info col-xs-12 col-sm-3"> <?php if($page->getAttribute('studio') == 1){ echo '<p>Studio $'.$page->getAttribute('studio_rent').'</p>'; } $numbers = array(1 => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five', 6 => 'six'); for($i = 1; $i < 7; $i++){ if(strlen($page->getAttribute('unit_'.$numbers[$i])) > 0){ echo '<p>'.$page->getAttribute('unit_'.$numbers[$i]).' $'.$page->getAttribute('unit_'.$numbers[$i].'_rent').'</p>'; } } ?> </div> </div> <?php } ?> </div> <div class="col-xs-0 col-sm-3 ads_bar"><?php $a = new GlobalArea('Property Sidebar'); $a->display($c); ?></div> </div> <?php echo $pageList->displayPaging(); }
You could add some code into your template that loops through $pages, retrieving the package_type attribute and using its value to built another sorted array to replace $pages which you then let the rest of your template output.
The alternative is to override the page list block controller and use the base method's sortBy method, passing in the package_type attribute key object. You'd probably have to alter the block interface too, so that you could select package_type as a search otherwise all page lists would do this which is probably no what you want.