<?php
 
 ###########################################################################
 #   OsCommerce WAP 1.1                                                    #
 #   This file is part of the wap tweak for OsCommerce (c) copyright 2004  #
 #   by Tom Nitzschner (tom@toms-home.com)                                 #
 #   http://www.toms-home.com                                              #
 #                                                                         #
 #   As always, make a backup before messing with anything. All code       #
 #   release by me is considered sample code only. It may be fully         #
 #   functual, but you use it at your own risk, if you break it,           #
 #   you get to fix it too. No waranty is given or implied.                #
 #                                                                         #
 #   All my work is released under the GNU GENERAL PUBLIC LICENSE.         #
 #   Please see the README for more information.                           #
 ###########################################################################

# User changable item below
# You can set the products listed per page with the setting below.
# I would not reccomend setting it too high, as that can cause
# timeouts while a WAP device reads the data and some devices have
# limits on the amount they can display

$increment = 5;

#change this to 1 if you wish to show the quantity of stock remaining

$show_quantity = "0";

# End user changable settings. 
# Do NOT change anything below unless you really know what you are doing.


header( 'content-Type: text/vnd.wap.wml');

// Print the file text header
echo  '<?xml version="1.0" ?><!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">';
?>

<wml>
<template>
<do type="prev" label="Back"><prev/></do>
<do type="accept" label="Main"><go href="wapindex.php"/></do>
</template>

<card id="result" title="Search Results">

<?php
require("includes/wap_top.php");
#
# Configure the items per page as per setting in the user section above.
#
$prod_increment = $increment;
$next_page = $increment * $page;
global $sub_id;
$i = 1;
#
# Query the database for the seach string.


#
$mp_ar=split(' ',trim($word));

foreach($mp_ar as $value){
	$tmpqry[]="pd.products_name like '%$value%' or pd.products_description like '%$value%'";
}

$resultqry=join(" or ",$tmpqry);



$product_query_available = tep_db_query("SELECT pd.products_id as products_id, pd.products_name as products_name, p.products_price as products_price,p.products_quantity as products_quantity,p.products_tax_class_id as products_tax_class_id from " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_PRODUCTS . " p where (".$resultqry.") and (p.products_id = pd.products_id)");


$item_total = tep_db_num_rows($product_query_available);
#
# Loop for all available items that match the string and fill the $prod array with the data.
#
while ($product = tep_db_fetch_array($product_query_available)) {
	$prod[$i]['products_id'] = $product['products_id'];
	$prod[$i]['products_name'] = $product['products_name'];
	$prod[$i]['products_price'] =  tep_round($product['products_price'], 2);
	$prod[$i]['products_quantity'] = $product['products_quantity'];
	$i++;
}
#
# Work out which page we will display.
#
$k=($increment * $page)+1;
if ($increment > ($item_total - ($increment * $page))) $increment = ($item_total - ($increment * $page));
$j=$increment + $next_page;
#
# Return search results while looping, limit the items per page as set above.
#
echo ("<p>Search Results:</p>\n");
while ($k <= $j) {
   	$prod_id = $prod[$k]['products_id'];
   	$prod_name = $prod[$k]['products_name'];
   	$prod_price = $prod[$k]['products_price'];
	$prod_quantity = $prod[$k]['products_quantity'];
	if ($show_quantity == "1") {
		echo ("<p><a href=\"wapitem.php?item_id=".$prod_id."\">" . tep_clean_wap_text($prod_name) . "</a> (Price: " . $prod_price . ")(Stock: ".$prod_quantity.")</p>\n");
	} else {
		echo ("<p><a href=\"wapitem.php?item_id=".$prod_id."\">" . tep_clean_wap_text($prod_name) . "</a> (Price: " . $prod_price . ")</p>\n");
	}
 	$k++;
 }
#
# Show No items if we didn't have a hit on the search.
#
if ($item_total == "0") echo ("<p>No Items Found</p>\n");
#
# Show the previous/next page links as required.
#
echo ("<p> </p>");
if ($k > ($increment+1)) {
	echo "<p><a href=\"wapresult.php?page=".($page-1)."&word=".urlencode($word)."\">Previous Products</a></p>\n";
}
if ($k < ($item_total)) {
	echo "<p><a href=\"wapresult.php?page=".($page+1)."&word=".urlencode($word)."\">More Products</a></p>\n";
}
?>
</card>
</wml>
