Viewer Order By

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

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