View Single Post
  #1  
Old 03-17-2003, 11:45 AM
  minorgod's Avatar 
minorgod minorgod is offline
 

X-Adept
  
Join Date: Sep 2002
Location: Arivaca, AZ
Posts: 402
 

Default Add a true Google search to your X-cart.

Hello all,
I've just finished adding a site-specific google-search to our advanced search page. For anyone interested, here's how to do it for x-cart 3.3.1...please note that I've already made fix for the price search bug in version 3.3.1:

First you need to sign up for a Google API key at:
http://www.google.com/apis/

Then, you need to download the excellent google search class and supporting files from the original author (written in French). You can find them here:

ftp://ftp2.xhtml.net/xhtml/php/google.zip

Then extract the files you downloaded and upload everything except index.php into your /customer/ folder.

So you should have copied the following extracted files from the google.zip file to the /customer/ folder:
SOAP_Google.php
PEAR.php
The entire SOAP directory

Then you should replace your customer/search.php page with the following code:
Code:
<? /*****************************************************************************\ +-----------------------------------------------------------------------------+ | X-Cart | | Copyright (c) 2001-2002 Ruslan R. Fazliev. All rights reserved. | +-----------------------------------------------------------------------------+ | The Ruslan R. Fazliev forbids, under any circumstances, the unauthorized | | reproduction of software or use of illegally obtained software. Making | | illegal copies of software is prohibited. Individuals who violate copyright | | law and software licensing agreements may be subject to criminal or civil | | action by the owner of the copyright. | | | | 1. It is illegal to copy a software, and install that single program for | | simultaneous use on multiple machines. | | | | 2. Unauthorized copies of software may not be used in any way. This applies | | even though you yourself may not have made the illegal copy. | | | | 3. Purchase of the appropriate number of copies of a software is necessary | | for maintaining legal status. | | | | DISCLAIMER | | | | THIS SOFTWARE IS PROVIDED BY Ruslan R. Fazliev ``AS IS'' AND ANY | | EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | | DISCLAIMED. IN NO EVENT SHALL Ruslan R. Fazliev OR ITS | | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | | OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | | | | The Initial Developer of the Original Code is Ruslan R. Fazliev. | | Portions created by Ruslan R. Fazliev are Copyright (C) 2001-2002 | | Ruslan R. Fazliev. All Rights Reserved. | +-----------------------------------------------------------------------------+ \*****************************************************************************/ # # $Id: search.php,v 1.35 2002/11/14 07:58:18 zorg Exp $ # require "../smarty.php"; require "../config.php"; @include "./https.php"; require "./auth.php"; require "../include/categories.php"; $tmp=strstr($QUERY_STRING, "PHPSESSID="); if (!empty($tmp)) $QUERY_STRING=""; if(!empty($QUERY_STRING)) { # # Permorfm SQL search query # if ( isset($_GET['q'])){ //BEGIN GOOGLE SEARCH CODE require_once 'SOAP_Google.php'; // Configure the following to make this work on your site $nr = 10; // Number of results per page $smarty->assign("nr",$nr); $clef = 'insert_your_google_key_here'; // your Google API key provided by Google $site_url = 'www.realestate-school.com'; //replace this with your site's url // End of configuration section if (strlen($q) > 0) { $google = new SOAP_Google($clef); $result = $google->search(array( 'query' => $q.' site:'.$site_url, 'start' => $s, 'maxResults' => $nr, 'lr' => 'lang_en', 'filter' => true )); if (false !== $result) { // echo " Found about {$result['estimatedTotalResultsCount']} documents in {$result['searchTime']} seconds.</p>"; // echo " Displaying results {$result['startIndex']} through {$result['endIndex']}.</p>"; $res = $result['resultElements']; if( count($res) < $nr ){ $is_last_page=true; }else{ $is_last_page=false; } $i=$result['startIndex']; if (is_array($res)){ foreach ($res as $site) { $google_links[]= " {$i}. {$site['title']} {$site['snippet']}</p>"; $i++; } }else{ $suggestion=$google->getSpellingSuggestion($q); if(!empty($suggestion)){ $google_links[]="No results found. <font size='2'>Did you mean: <a href='search.php?q=".$suggestion."'>".$suggestion."?</font>"; $gootle_links[]="Click here to search again."; }else{ $google_links[]="No results found matching your search term. Click here to search again."; } } //echo "<pre>"; //print_r($google_links); //echo "</pre>"; $smarty->assign("google_links",$google_links); $smarty->assign("main","search"); $smarty->assign("google_result",$result); //echo "<div align='center'>"; if ($result['startIndex'] >= $nr) { $t = $result['startIndex'] - ($nr+1); $google_nav="<a href='search.php?q="; $google_nav.=urlencode($q); $google_nav.="&amp;s=0"; $google_nav.="'>first</a> | "; } if ($result['startIndex'] >= $nr) { $t = $result['startIndex'] - ($nr+1); $google_nav.="<a href='search.php?q="; $google_nav.=urlencode($q); $google_nav.="&amp;s="; $google_nav.=$t; $google_nav.="'>previous</a> | "; } //generate page links 1 | 2 | 3 | 4 | etc..... if ($result['estimatedTotalResultsCount'] > $nr ){ $number_pages = $result['estimatedTotalResultsCount'] / $nr; if($result['estimatedTotalResultsCount'] % $nr != 0){ $number_pages++; } for($i=0;$i<$number_pages;$i++){ //$t = $result['endIndex'] ;//+ 1; $t=$i*$nr; $google_nav.="<a href='search.php?q="; $google_nav.=urlencode($q); $google_nav.="&amp;s="; $google_nav.=$t; $google_nav.="'>"; //echo "i is $i and s is $s"; if($i * $nr == $s){ $google_nav.=""; }else{ $google_nav.="<u>"; } $google_nav.=$i+1; if($i * $nr == $s){ $google_nav.=""; }else{ $google_nav.="</u>"; } $google_nav.="</a> | "; } } //if (($result['estimatedTotalResultsCount'] > $result['endIndex']) || !$result['estimateIsExact'] || $result['startIndex'] >= $nr ){ // $google_nav.=" Navigation | "; //} if ((($result['estimatedTotalResultsCount'] > $result['endIndex']) || !$result['estimateIsExact']) && $is_last_page!=true ){ $t = $result['endIndex'] ;//+ 1; $google_nav.="<a href='search.php?q="; $google_nav.=urlencode($q); $google_nav.="&amp;s="; $google_nav.=$t; $google_nav.="'>next</a> | "; } if ((($result['estimatedTotalResultsCount'] > $result['endIndex']) || !$result['estimateIsExact']) && $is_last_page!=true){ $t = $result['endIndex'] ;//+ 1; $google_nav.="<a href='search.php?q="; $google_nav.=urlencode($q); $google_nav.="&amp;s="; $google_nav.=$result['estimatedTotalResultsCount']-1; $google_nav.="'>last</a>"; } } //echo "</div>"; //echo "<hr>"; //echo "<pre>"; //print_r($result); //echo "</pre> "; // print_r($result); } else { echo " There was a problem with the search request.</p>"; } $smarty->assign("google_nav",$google_nav); //END GOOGLE SEARCH CODE }else{ $price_condition = $price_search_1?" AND $sql_tbl[pricing].price>='$price_search_1'":""; $price_condition .= $price_search_2?" AND $sql_tbl[pricing].price<='$price_search_2'":""; $sort_by_price = $price_condition?" ORDER BY price":""; $price_substring = $price_search_1?"&price_search_1=".urlencode($price_search_1):""; $price_substring .= $price_search_2?"&price_search_2=".urlencode($price_search_2):""; $search_category = addslashes(array_pop(func_query_first("select category from $sql_tbl[categories] where categoryid='$in_category'"))); //$search_query = "($sql_tbl[products].product like '%$substring%' or $sql_tbl[products].descr like '%$substring%') and $sql_tbl[categories].category like '$search_category%' and $sql_tbl[products].forsale='Y' $price_condition ".$sort_by_price; $search_query = "($sql_tbl[products].product like '%$substring%' or $sql_tbl[products].descr like '%$substring%') and $sql_tbl[categories].category like '$search_category%' and $sql_tbl[products].forsale='Y' $price_condition "; if ($current_area == "C") { $membership_condition = " AND ($sql_tbl[categories].membership='". $user_account['membership']."' OR $sql_tbl[categories].membership='') "; } else { $membership_condition = ""; } $search_query_count = "select count(*) from $sql_tbl[products], $sql_tbl[pricing], $sql_tbl[categories] where $sql_tbl[pricing].productid=$sql_tbl[products].productid and $sql_tbl[pricing].quantity=1 and $sql_tbl[products].categoryid=$sql_tbl[categories].categoryid $membership_condition and ($sql_tbl[pricing].membership='". $user_account['membership']."' or $sql_tbl[pricing].membership='') and ($sql_tbl[products].product like '%$substring%' or $sql_tbl[products].descr like '%$substring%') and $sql_tbl[categories].category like '$search_category%' and $sql_tbl[products].forsale='Y' $price_condition "; $total_products_in_search = array_pop(func_query_first($search_query_count)); # # Navigation code # $objects_per_page = $config["General"]["products_per_page"]; $total_nav_pages = ceil($total_products_in_search/$config["General"]["products_per_page"])+1; require "../include/navigation.php"; //$smarty->assign("products",func_search_products($search_query, $user_account['membership'],$first_page,$total_products_in_search)); $smarty->assign("products",func_search_products($search_query, $user_account['membership'],$first_page,$total_products_in_search,$sort_by_price)); $smarty->assign("navigation_script","search.php?substring=".urlencode($substring)."&in_category=$in_category".$price_substring); $HTTP_GET_VARS["substring"] = stripslashes($HTTP_GET_VARS["substring"]); $smarty->assign("main","search"); } } else { $smarty->assign("main","advanced_search"); } $smarty->display("customer/home.tpl"); ?>

Then you should replace your /skindirectory/customer/main/search_result.tpl with the following code:
Code:
{* $Id: search_result.tpl,v 1.10 2002/10/11 12:43:43 ambal Exp $ *} { include file="location.tpl" last_location=$lng.lbl_search_results} {include file="customer/main/navigation.tpl"} {capture name=dialog} {include file="customer/main/products.tpl"} {/capture} {if $smarty.capture.dialog ne ""} {include file="dialog.tpl" title=$lng.lbl_search_results content=$smarty.capture.dialog extra="width=100%"} {elseif $google_links ne ""} {capture name=dialog2} {section name=google_link loop=$google_links} {$google_links[google_link]} {/section} {if $google_nav ne ""} {$google_nav} {/if} {/capture} {if $smarty.capture.dialog2 ne ""} {include file="dialog.tpl" title=$lng.lbl_search_results content=$smarty.capture.dialog2 extra="width=100%"} {/if} {else} {include file="dialog.tpl" title=$lng.lbl_search_results content="No matching products found" extra="width=100%"} {/if} {include file="customer/main/navigation.tpl"}

Oh yeah, and replace the skindirectory/customer/main/advanced_search.tpl with the following code:
Code:
{* $Id: advanced_search.tpl,v 1.3 2002/10/08 12:01:17 alfiya Exp $ *} {include file="location.tpl" last_location=$lng.lbl_advanced_search} {capture name=google_search} <form action="search.php" method="get" name="googlesearch" id="googlesearch"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td height="21">Search this whole site using Google: <input name="hl" type="hidden" value="en" id="hl"> <input name="lr" type="hidden" id="lr"> <input name="ie" type="hidden" value="UTF-8" id="ie"> <input name="oe" type="hidden" value="UTF-8" id="oe"> <input name="as_qdr" type="hidden" value="all" id="as_qdr"></td> </tr> <tr> <td><input name="q" type="text" id="q"> {include file="buttons/search.tpl"} </td> </tr> </table> </form> {/capture} {include file="dialog.tpl" title="Google Search" content=$smarty.capture.google_search extra="width=100%"} {capture name=adv_search} <table border=0> <form action="search.php" name="productsearchbyprice_form"> <tr> <td>{$lng.lbl_product_title}</td> <td> <input type="text" name="substring" size="30" value="{$smarty.get.substring}"> </td> </tr> <tr><td>{$lng.lbl_price}, {$config.General.currency_symbol}</td> <td><input type="text" name="price_search_1" size="6" value="{$smarty.get.price_search_1|escape}"> - <input type="text" name="price_search_2" size="6" value="{$smarty.get.price_search_2|escape}"></td></tr> <tr><td>{$lng.lbl_category}</td> <td> <select name="in_category"> <option value="">All</option> {section name=cat_num loop=$categories} <option value="{ $categories[cat_num].categoryid}" {if $smarty.get.in_category eq $categories[cat_num].categoryid or $cat eq $categories[cat_num].categoryid}selected{/if}>{$categories[cat_num].category|escape}</option> {/section} </select> </td> </tr> <tr><td></td></tr> <tr><th>{include file="buttons/search.tpl"}</th></tr> </form> </table> {/capture} {include file="dialog.tpl" title=$lng.lbl_advanced_search content=$smarty.capture.adv_search extra="width=100%"} </p>

Now look at the customer/search.php page and fill in your key where it says:
Code:
$clef = 'insert_your_google_key_here'; $site_url = 'www.realestate-school.com';

Replace the parameters with your google key and your url, save and upload the file.

You're done. You now have a google search embedded in your site that will search only your site. It will also offer spelling suggestions whenever possible for misspelled queries that don't return any results. If you wish to change the format of the navigation links at the bottom of the results, just modify the search.php page, as I've not bothered to use $smarty logic for the nav link generation. It's just way easier to do it with php and then spit it out into the template once the links are formatted. Feel free to do whatever you want with this code. Someone else did all the hard work here, I just put it together with x-cart.
__________________
www.brettbrewer.com
Getting back into x-cart dev after a long hiatus. Modded lots of x-carts from version 3.1.x to 4.1.x. Developer of ImageScaler mod, Pre-login per user coupon mod, Wordpress feed mod, DigitalSubscriptions mod, Phonetic bulk download keys addon for DownloadExpander mod, Serial Number Generator for ESD products, Custom CMS/LMS integrations, external products mod, and more.
Reply With Quote