You are here:  » Common search for multiple installs

Support Forum



Common search for multiple installs

Submitted by Convergence on Fri, 2012-06-01 01:08 in

v12/10B

Greetings,

I'm baaaaaaaaaaaaack, LOL.

Have three installs on one domain.

example.com/install-1/
example.com/install-2/
example.com/install-3/

Have a static html template on index.php - example.com/index.php
Have replaced the index.php on each install with a static html template.

Want to be to be able to search all three databases from example.com/index.php

On the index.php page for each install have a search box that we would like to configure to search only the db for that install. Think that is pretty straight forward to do, but any snippet of code would be helpful.

Thank you!

Submitted by support on Fri, 2012-06-01 08:19

Hi,

Combining search from multiple installs is actually quite complex - e.g. to query different databases and collate into a tabular form with pagination. What is straight forward however, and creates a tidy interface where the sub-domain based installs are effectively a "master" category is to have a drop down box from which the user selects the category to search in. A redirection script is required to forward the query to the selected installation.

First, create a redirection script e.g. searchredir.php in the top level as follows:

<?php
  header
("Location: /".$_GET["site"]."/search.php?q=".urlencode($_GET["q"]));
?>

...and then your form HTML:

<form method='GET' action='/searchredir.php'>
<input type='text' name='q' />
<select name='site'>
<option value='install-1'>Install 1</option>
<option value='install-2'>Install 2</option>
<option value='install-3'>Install 3</option>
</select>
<input type='submit' value='Search' />
</form>

Cheers,
David.
--
PriceTapestry.com

Submitted by Convergence on Fri, 2012-06-01 15:47

Hi David,

WOW, thank you! That is just too slick :) Works perfectly.

Thanks again!

Submitted by Convergence on Tue, 2012-11-06 22:35

Hi David,

We have a twist to this feature. We are moving our installs to sub-domains.

From:
example.com/install-1/
example.com/install-2/
example.com/install-3/

To:
install-1.example.com/
install-2.example.com/
install-3.example.com/

Any assistance would be greatly appreciated.

Thank you!

Submitted by support on Wed, 2012-11-07 12:01

Hi,

In your redirection script use this in place of the version described above:

<?php
  header
("Location: http://".$_GET["site"].".example.com/search.php?q=".urlencode($_GET["q"]));
?>

No changes required to the form at all!

Cheers,
David.
--
PriceTapestry.com

Submitted by Convergence on Wed, 2012-11-07 14:59

Hi David,

Ooops. I didn't think this through properly. Sorry.

Actually the changes are:

From:
example.com/install-1/
example.com/install-2/
example.com/install-3/

To:
somecategory1.example.com/install-1/
somecategory2.example.com/install-2/
somecategory2.example.com/install-3/

Thanks again!

Submitted by support on Wed, 2012-11-07 15:32

Hi,

If category-n and /install-n/ are different you could use a look-up table in the redirection script. I assume the drop-down would be category based e.g.

<form method='GET' action='/searchredir.php'>
<input type='text' name='q' />
<select name='category'>
<option value='category-1'>Category 1</option>
<option value='category-2'>Category 2</option>
<option value='category-3'>Category 3</option>
</select>
<input type='submit' value='Search' />
</form>

And then as your redirect code:

<?php
  $baseHREF
["category-1"] = "install-1";
  
$baseHREF["category-2"] = "install-2";
  
$baseHREF["category-3"] = "install-3";
  
header("Location: http://".$_GET["category"].".example.com/".$baseHREF[$_GET["category"]]."/search.php?q=".urlencode($_GET["q"]));
?>

Note that the value of the drop down in the form could simply be the full path to the search.php that you wish to handle the query and construct the location: header from that but for some reason I don't really like posting URLs around in forms...!

Cheers,
David.
--
PriceTapestry.com

Submitted by Convergence on Wed, 2012-11-07 15:49

Hi David,

That's just beautiful - Thanks!

Submitted by philstone on Thu, 2012-11-08 23:24

Hi David

Trust you are well!!

It's been a while since i worked on my site, but just a short query

i'm using the above search code now on my site, however is it possible that if using subdomain "/electronics/" that it will automatically be the "selected" on the search form?

regards

Phil Stone
www.buy24-7.net

Submitted by support on Fri, 2012-11-09 10:40

Hi Phil,

Sure - let's say one line of your select box is as follows:

<option value='electronics'>Electronics</option>

You could use:

<option value='electronics' <?php print (strpos($_SERVER["REQUEST_URI"],"/electronics/")?"selected='selected'":""); ?>>Electronics</option>

However that could become a bit tedious to manage if you have many /category/ installations; so a neater way would be to generate the drop-down completely in PHP from an array:

<?php
  $categories 
= array();
  
$categories["electronics"] = "Electronics";
  
$categories["home-garden"] = "Home and Garden";
  
// etc.
  
print "<select name='category'>";
  foreach(
$categories as $directory => $displayName)
  {
    
$selected = (strpos($_SERVER["REQUEST_URI"],"/".$directory."/")?"selected='selected'":"");
    print 
"<option value='".$directory."' ".$selected.">".$displayName."</option>";
  }
  print 
"</select>";
?>

Cheers,
David.
--
PriceTapestry.com

Submitted by philstone on Fri, 2012-11-09 21:30

Hi David

thanks for that, It's not actually for categories but rather the script i'm using for navigating from one installation to another

it currently looks like

<form method='GET' action='/searchredir.php'>
<input type='text' name='q' />
<select name='site'>
<option value='appliances'>Appliances</option>
<option value='computers'>Computers & Laptops</option>
<option value='electronics'>Electricals</option>
<option value='gadgets'>Gadgets & Toys</option>
</select>
<input type='submit' value='Search' />
</form>

what I'm imagining is an amendment to the above which would allow someone who is on www.site.com/electronics/ not to have to change the drop down option unless they wish to search another part of the website, at present I use one /html/ folder to serve all installations, otherwise i know i could just add 'selected' beside the chosen option in each installation, which i can do if there is not another way of doing it

thanks again for your great support!!

regards

Phil Stone
www.buy24-7.net

Submitted by support on Sat, 2012-11-10 09:21

Hi Phil,

Pretty much the same code should work - have a go with the following as a direct replace for your existing block:

<form method='GET' action='/searchredir.php'>
<input type='text' name='q' />
<select name='site'>
<?php
  $sites = array();
  $sites["appliances"] = "Appliances";
  $sites["computers"] = "Computers & Laptops";
  $sites["electronics"] = "Electricals";
  $sites["gadgets"] = "Gadgets & Toys";
  foreach($sites as $site => $name)
  {
    $selected = (strpos($_SERVER["REQUEST_URI"],"/".$site."/")!==FALSE?"selected='selected'":"");
    print "<option value='".$site."' ".$selected.">".$name."</option>";
  }
?>
</select>
<input type='submit' value='Search' />
</form>

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by philstone on Sat, 2012-11-10 09:38

Perfect David!!

Thanks so much!

regards

Phil Stone
www.buy24-7.net

Submitted by Convergence on Mon, 2012-11-26 03:17

Hi David,

Yet another twist as installs are structured as follows:

category-1.example.com/install-1/
category-1.example.com/install-2/
category-1.example.com/install-3/
category-2.example.com/install-4/
category-2.example.com/install-5/
category-2.example.com/install-6/

Currently when we try to use the above, it always ends up searching the last install in the category, example:

category-1.example.com/install-3/
category-2.example.com/install-6/

Suggestions?

Thanks!

Submitted by support on Mon, 2012-11-26 08:26

Hi,

Ah then in that case you will need to pass both category-n and install-n through in the form POST. Perhaps easiest way is to separate using ":" and then they can be split easily using explode() - have a go with your form as follows:

<form method='GET' action='/searchredir.php'>
<input type='text' name='q' />
<select name='category'>
<option value='category-1:install-1'>Option 1</option>
<option value='category-2:install-2'>Option 2</option>
<option value='category-2:install-3'>Option 3</option>
</select>
<input type='submit' value='Search' />
</form>

And then as your redirect code:

<?php
  $parts = explode(":",$_GET["category"]);
  header("Location: http://".$parts[0].".example.com/".$parts[1]."/search.php?q=".urlencode($_GET["q"]));
?>

Cheers,
David.
--
PriceTapestry.com

Submitted by Convergence on Fri, 2012-11-30 15:51

Hi David,

Perfect execution.

Thanks, again!