Search Form to Control orderBy and perPage functions

7 posts by 2 authors in: Forums > CMS Builder
Last Post: August 1, 2014   (RSS)

By andybarn - July 27, 2014

Hi

I have 2 questions which I hope you can help me with.

Question 1

I have  a search form for a property website and I want to order the results by price.

I create a hidden field in my search form as below:

<input type ="hidden" name ="orderBy" value ="price" />

This seems to work in ascending order, however it appears to ignore the thousand delimiter and orders the price as follows:


1,250,000
895,000
89,700
90,000
95,000
995,000

How can I put a hidden field in the search form so that it would order as follows:-

89,700
90,000
95,000
895,000
995,000
1,250,000

Now I know in the header I could put the following statement

'orderBy'     => 'price+0'

But that would override whatever I put in the search form. I need the search form
to control the 'orderBy' function,

Is this possible and if so could you show me how?

Question 2

I also have a search form that I want to control the number of listings per page.

Is there a command I could put in the search form that would do this. This could either
be a hidden input or a drop box select option.

Again I know I could put the following statement in the header

'perPage'     => '10'

But that would override whatever I put in the search form. I need the search form to control
the 'perPage ' function.

I can’t seem to find anything that would do this in a search form.

Is this possible and if so could you show me how?

Thanks for your help.
Andy

By claire - July 28, 2014

Hi Andy

For the first question - you'll need to tell the database to order the records as if the price were a number, not a text string. So try changing the orderBy line to this:

'orderBy='     => 'CAST(price AS UNSIGNED)'

For the second question - what you'd need to do is add a GET parameter to the URL that specifies the number of listings per page, then check for it and insert it as needed in every link on the page. This really depends on your level of experience with PHP, as there's no quick way of adding it. I'll see if I can find an example for you.

--------------------

Claire Ryan
interactivetools.com

Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

By claire - July 30, 2014

Hi Andy

Yeah, I think I understand. For the orderBy form, what you'll have to do is check the $_REQUEST['price'] variable when the page loads, and insert the right kind of ordering.

So, just before the getRecords function, add this:

$orderby = "CAST(price AS UNSIGNED)"; // default
if(@$_REQUEST['orderby'] == 'price DESC') {
    $orderby = "CAST(price AS UNSIGNED) DESC";
}

Then in the getRecords function, change the orderBy like so:

'orderBy'     => $orderby

If you want to do different variations on the ordering, all you need to do is test for them like above and change the $orderby clause as needed.

For the perPage problem, you can do something very similar, like so:

$perPage = 10; // whatever your default needs to be
if(isset($_REQUEST['perPage'])) {
     $perPage = mysql_escape($_REQUEST['perPage']);
}

And the perPage clause in getRecords:

'perPage' => $perPage

Does this help?

--------------------

Claire Ryan
interactivetools.com

Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

By andybarn - July 31, 2014

Hi Claire

I thought I would concentrate on the orderby price issue first however I am struggling with this.

I have changed the form method to "get" so I can see what is selected in the URL and also added a php echo command to see the order by value <?php echo @$_REQUEST['orderBy'] ?>

I am still unable to get this to work - sorry

Could you take a look at my page to see if you can spot the problem please?

I attach for you.

You can also see the page in action on my development server at

http://37.220.93.134/~javeahom/property/listings.php

I am sure this would be helpful to other people once we get it working.

Kind regards

Andy

Attachments:

listings.php 26K

By claire - July 31, 2014

Hi Andy

The variable names are a little off. Can you change $_REQUEST['orderby'] to $_REQUEST['orderBy']?

--------------------

Claire Ryan
interactivetools.com

Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

By andybarn - August 1, 2014

Hi Claire

Great - that was it!

For anyone reading this, the code that works is as follows:-

    $orderby = "CAST(price AS UNSIGNED)"; // default
if(@$_REQUEST['orderBy'] == 'price DESC') {
    $orderby = "CAST(price AS UNSIGNED) DESC";
}

Thanks for your help and perseverance with this Claire - great support!

Now I will try the listings per page code and get back to you.

Kind regards

Andy