Viewer Order By

3 posts by 2 authors in: Forums > CMS Builder
Last Post: June 18, 2008   (RSS)

By rjbathgate - June 17, 2008

Hi,

Using CMS Builder, I want to enable the user/viewer to determine the order results are displayed (orderby).

I can only figure out how to set it as fixed in Step 1 of the list viewer, not giving the live viewer the flexibility.

I want to implement it in two ways:

* Implemented in a drop down (select) box on the list page

* In a search engine (based on a form such as (see Sort By last field in below example):

<form method="post" action="commercialsearchresults.php">
<p>Price From:<select name="price_min"><option value="">All</option><option value="100,000">100,000</option><option value="200,000">200,000</option>
</select></p>
<p>Price To:<select name="price_max"><option value="">All</option><option value="100,000">100,000</option><option value="200,000">200,000</option><option value="300,000">300,000</option>
</select>
</p>
<p>Sort By:<select><option value="">Option</option><option value="">Option</option></select></p>
<input type="submit" class="submit" value="" />
</form>

Any help would be most appreciated,

Cheers in advance
Rob

Re: [rjbathgate] Viewer Order By

By Dave - June 18, 2008

Hi Rob,

You can manually set the sort order with the 'orderBy' option. Here's an example:

list($newsRecords, $newsMetaData) = getRecords(array(
'tableName' => 'news',
'orderBy' => 'title DESC',
));


So, say you named your select field "order" like this:

<select name="order">
<option value="a">price (highest first)</option>
<option value="b">price (lowest first)</option>
...etc...
</select>



You could add some code like this to change the orderBy based on the form value:

$orderBy = "";
if (@$FORM['order'] == 'a') { $orderBy = "price DESC"; }
if (@$FORM['order'] == 'b') { $orderBy = "price"; }
# ... etc ...


list($newsRecords, $newsMetaData) = getRecords(array(
'tableName' => 'news',
'orderBy' => $orderBy,
));


The reason to do it that way by passing a letter (or word or code, it doesn't matter) and testing for that instead of just specifying the order by in the option value directly is because you don't want users to be able to pass MySQL directly into your program or it's a security risk.

Give it a try and let me know how it goes!
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] Viewer Order By

By rjbathgate - June 18, 2008

Hi Dave,

Seems to have worked a treat,

Thanks very much
Rb