You are here:  » customize searchresults.php

Support Forum



customize searchresults.php

Submitted by yoyd on Mon, 2009-12-07 09:35 in

I was wondering if there was a way to customize different searchresults.php pages. So for example, if someone clicked on men's clothing, they would go to a customized men's searchresults.php page where I could put some text about men's clothing with the results below it. Same for women's and kid's clothing.

Anyway to hardcode a link so it goes to a customized page instead of the regular searchresults.php page. I would still want the regular page to show up when they do a regular search.

Thanks for all the ongoing support!

Submitted by support on Mon, 2009-12-07 09:53

Hello yoyd,

Sure - the very easiest way to do this is with a series of IF statements at the top of html/searchresults.php (SWITCH would be slightly more efficient, but less easy to manage). To do this, insert the following code at the very top of the file:

<?php if ($q == "category:Mens Clothing:"): ?>
<p>Your additional men's clothing text here...</p>
<?php endif; ?>
<?php if ($q == "category:Womens Clothing:"): ?>
<p>Your additional womens's clothing text here...</p>
<?php endif; ?>

...just add more blocks with a different value in the IF statement for different queries for which you wish to add text to the page.

To hardcode a link to one of your customised pages; simply link to search.php with the appropriate q parameter; for example:

<a href='/search.php?q=category:Mens Clothing:'>Mens Clothing</a>

Hope this helps!

Cheers,
David.

Submitted by yoyd on Wed, 2010-03-31 09:33

David,

I had some followup questions for using the if statement.

1) how to add an "or"? if $q == "category:Mens Clothing:" or $q == "category:Boys Clothing:" then...

2) how to specify the url instead of $q? if url = www.mysite/mensclothing then...

3) how to do an if statement based on the number of results on the page in searchresults.php
For example, I want to have a skyscaper banner in my side navigation only if there are more than 20 results on that page (I have the side navigation already, just want to add a banner if there is extra empty space)

Thanks!

Submitted by support on Wed, 2010-03-31 11:59

Hi,

Answers inline...

> 1) how to add an "or"? if $q == "category:Mens Clothing:"
> or $q == "category:Boys Clothing:" then...

Use a line like this:

<?php if (($q == "category:Mens Clothing:") || ($q == "category:Boys Clothing:")): ?>

> 2) how to specify the url instead of $q? if
> url = www.mysite/mensclothing then...

The path (not the server) is in the variable $_SERVER["REQUEST_URI"], so you could use:

<?php if ($_SERVER["REQUEST_URI"] == "/mensclothing"): ?>

If you need to know the host name also, that should be in $_SERVER["HTTP_HOST"], but note that depending on your web server configuration, it may or may not include "www.", so I would always recommend testing it using strpos() and just look for the [sub]domain you're interested in; and then combine it with a comparison on $_SERVER["REQUEST_URI"], for example:

<?php
  if (
     (strpos($_SERVER["HTTP_HOST"],"mysite.com")!==FALSE)
     &&
     ($_SERVER["REQUEST_URI"] == "/mensclothing")
     ):
?>

> 3) how to do an if statement based on the number of results
> on the page in searchresults.php

Anywhere within the template; the number of results can be found using count($searchresults["products"]), so for example:

<?php if (count($searchresults["products"]) >= 20): ?>
<!-- banner HTML here -->
<?php endif; ?>

Hope this helps!

Cheers,
David.

Submitted by yoyd on Thu, 2010-04-01 03:52

Thank You!