<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>View products by search string</TITLE>
<script language="JavaScript">
function newWindow(mypage,myname,w,h,features) {
  if(screen.width){
  var winl = (screen.width-w)/2;
  var wint = (screen.height-h)/2;
  }else{winl = 0;wint =0;}
  if (winl < 0) winl = 0;
  if (wint < 0) wint = 0;
  var settings = 'height=' + h + ',';
  settings += 'width=' + w + ',';
  settings += 'top=' + wint + ',';
  settings += 'left=' + winl + ',';
  settings += features;
  win = window.open(mypage,myname,settings);
  win.window.focus();
}
</script>
</HEAD>
<BODY>
<?php
/***************************************************************************
 *			searchs.php                   	                   *
 *                     	          				           *
 *   File Info:		  Handles searching                                *
 *   Copyright:       Copyright 2003 http://www.128concepts.com            *
 *						                           *
 ***************************************************************************/
include ("config.php");
include ("./tests.php");

$cont = "true";
$str = getenv("QUERY_STRING");
echo "<br><table width=530 border=1 cellspacing=0 cellpadding=0 height=100>
<tr><td width=530 bgcolor=#CCCCCC height=10>$header</td></tr>
<tr><td width=530 bgcolor=#FFFFFF height=140>";
if ($_SERVER['REQUEST_METHOD'] == "POST") 
  {
        # escape data and set variables
	$limit= addslashes($_POST["limit"]);
        $search= addslashes($_POST["search"]);
        $search = strtolower($search);
}
########### get the page and search string #############
if (strlen($str) > 1)
{
$pos = strpos($str,'=');
$poss = strpos($str,'&');
$lan = $pos + 1;
$pages = substr($str,0,$poss);
$page = substr($pages,$lan,$poss);
$lgt = strlen($str);
$sch = substr($str,$poss+1,$lgt);
$pos1 = strpos($sch,'=');
$search = substr($sch,$pos1+1,strlen($sch));
$search = str_replace("%20"," ",$search);
$pos2 = strpos($str,'+');
$pos3 = strpos($str,':');
if ($pos3 >= 1)
{
	$limit = substr($str,1,$pos3-1);
}
$npage = $page;
}


if (strlen($search) == 0)
{
	echo("<center><font face=verdana size=2 color= #666666>Opps....you did not enter a search string...<br>please re-enter to search again</font></center><center><img src = images/searching.gif border=0 height=40></center></td></tr></table>");
	$cont = "false";
}
if ($cont == "true"){

if (empty($search))
	$search = $oldsearch;
# look for product(s) using search string

$table = "products";
$conn = @mysql_connect($server,$user,$password)
	or die("Unable to connect to SQL database...");
$rs = @mysql_select_db($database,$conn) 
	or die("Unable to select the database...Please press 'Create Database' to create new database.");

// limit value for records to be shown

if ($limit == NULL)
	$limit = 5;

echo "<br><center><font face = Verdana size = 2>Select number of records you wish to view per page:<br>Currently viewing  $limit records per page.";
$form = "<form action =search.php method = POST>";
$form.= "<select name = limit><option value = 5>5 records</option><option value = 10>10 records</option><option value = 25>25 records</option><option value = 50>50 records</option></select> ";
$form.= "<input type=hidden name=search value='$search'>";
$form.= "<input type=submit value = 'View Records'></form><br></center>";
echo $form;

//$query_count = "SELECT * from $table where (pitem like '%$search%' or pdes like '%$search%')";
//$res_count = mysql_query($query_count,$conn);
$totalrows = mysql_num_rows($res_count);

if (empty($page))
$page = 1;
$totalrows = 0;
$like = "";
$limitv = $page * $limit - ($limit);
$search = strtoupper($search);
$count = count_words($search);

#### remove any spaces in search string ###

if ($count == 1) {$search = str_replace(' ', "", $search); }

$searchwords = split ( "[ ,]", $search );  
foreach ($searchwords as $word)  
{
$query = "SELECT * from $table where (pitem like '%$word%' or pdes like '%$word%') LIMIT $limitv,$limit";
if(($word!=="AND")AND($word!=="OR"))
$like.= "(pitem like '%$word%' or pdes like '%$word%')";

// strip OR or AND from end of $like

$length = strlen ($like);
$test_or = substr($like, -3);
			
if($test_or=="OR ")
{
	$new_len = $length - 3;
	$like =  substr($like, 0, $new_len);
}

$test_and = substr($like, -4);
		
if($test_and=="AND ")
{
	$new_len = $length - 4;
	$like = substr($like, 0, $new_len);
}

if($word=="AND")
{
	$next = "AND ";
}

if($word!=="AND")
{
	$next = "OR ";
}
	
$like.= $next;	
unset($next);
$result = mysql_query($query,$conn)
	or die("SQL command fail ".mysql_error());
$count_result = mysql_num_rows($result);
}

// strip OR or AND from end of $like
$like_length = strlen ($like);

$test_or = substr($like, -3);

if($test_or=="OR ")
{
	$new_len = $like_length - 3;
	$like =  substr($like, 0, $new_len);
}

$test_and = substr($like, -4);
if($test_and=="AND ")
{
	$new_len = $like_length - 4;
	$like = substr($like, 0, $new_len);
}

$query_count = "SELECT * from $table where $like";
$res_count = mysql_query($query_count,$conn);
$totalrows = mysql_num_rows($res_count);

if(empty($page)) 

$page = 1; 

$limitvalue = $page * $limit - ($limit); 

$query = "select * from $table where $like LIMIT $limitvalue, $limit "; 

// End of multiword search additions and modifications here

$result = mysql_query($query) or die("Error: " . mysql_error()); 
$count_result = mysql_num_rows($result); 

$numpages = ceil($totalrows / $limit);
echo "<center><img src=images/search-all-prods.gif border=0></center>";
echo "<center><b><font face=verdana size=2 color=#000080>Found $totalrows</b> result(s) for '$search'</font><img src=images/searching.gif height=28></center></center><br>";
$from = $limit*$page-$limit+1;
$to = $from+$count_result-1;
if ($totalrows > $limit)
{
if ($style == 1) #style 1
{
echo "<table align=\"center\" width=\"90%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
            <tr><td width=\"50%\" bgcolor=\"$backcolor\" align=\"left\">";if($numpages>1){echo"Showing item(s): $from to $to</td></table> ";} 
}
if ($style == 2) #style 2
{
echo "<table align=\"center\" width=\"60%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
            <tr><td width=\"50%\" bgcolor=\"$backcolor\" align=\"left\">";if($numpages>1){echo"Showing item(s): $from to $to</td></table> ";} 
}}
echo "<table align=\"center\" width=\"65%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
if ($totalrows > $limit)
echo "<tr><td width=\"50%\" bgcolor=\"$backcolor\" align=\"right\"><b>Go to page: </b>";
if($page != 1){ 
$pageprev = $page - 1; 
//echo("<a href=\"$PHP_SELF?page=$pageprev\"><< PREV</a>&nbsp;"); 
echo("<a href=\"$PHP_SELF?+$limit:page=$pageprev&search=$search\"><img src = ./images/back.gif  width = 20 height = 16 border=0 alt=Previous Page></a>&nbsp;"); 

} 

// display page nos if not 1 
for($i = 1; $i <= $numpages; $i++){ 
if($numpages>1){
if($i == $page) 
echo("&nbsp;".$i."&nbsp;"); 
else
echo("&nbsp;<a href=\".\search.php?+$limit:page=$i&search=$search\">$i</a>&nbsp;"); 
}}

// display next page link if there is more than one
if(($totalrows - ($limit * $page)) > 0){ 
$pagenext = $page + 1; 
//echo("<a href=\"$PHP_SELF?page=$pagenext\">NEXT >></a>"); 
echo("<a href=\"$PHP_SELF?+$limit:page=$pagenext&search=$search\"><img src = ./images/next.gif width = 20 height = 16 border=0 alt=Next Page></a>"); 

}
echo "</td><tr></table><br>";
if($totalrows!=0){
	if ($style == 1) #style 1
	{
	echo"<table align=\"center\" valign=\"top\" width=\"100%\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\">
	<tr bgcolor=\"#FFFFFF\"><td>
			<table cellpadding=\"2\" border=\"0\" cellspacing=\"1\" valign=\"top\" width=\"100%\" align=\"center\">
				<tr bgcolor=\"$backcolor\" height=\"20\">
				<td bgcolor=#CCCCCC height=\"20\" align=\"center\" width=\"30\"><b><font face = verdana size=2 color=darkred>Product ID</b></font></td>
				<td bgcolor=#CCCCCC height=\"20\" align=\"center\"><b><font face = verdana size=2 color=darkred>Title</b></font></td>
				<td bgcolor=#CCCCCC height=\"20\" align=\"center\"><b><font face = verdana size=2 color=darkred>Description</b></font></td>
				<td bgcolor=#CCCCCC height=\"20\" align=\"center\"><b><font face = verdana size=2 color=darkred>Price</b></font></td></tr>";}
	# secondary style
	if ($style == 2)
{
	$list = "<TABLE id=\"AutoNumber1\" style=\"BORDER-COLLAPSE: collapse\" borderColor=\"#cccccc\" 
height=\"85\" cellSpacing=\"0\" borderColorDark=\"#cccccc\" cellPadding=\"0\" width=\"100%\" 
borderColorLight=\"#cccccc\" border=\"1\">
  <TBODY>
  <TR><TD width=\"14%\" bgColor=\"#cccccc\" height=\"19\"><FONT style=\"FONT-SIZE: 9pt\" 
      face=\"Arial\">&nbsp; Image</FONT></TD>
    <TD width=\"40%\" bgColor=\"#cccccc\" height=\"19\"><FONT style=\"FONT-SIZE: 9pt\" 
      face=\"Arial\">Product ID, Title, Description</FONT></TD>
    <TD width=\"13%\" bgColor=\"#cccccc\" height=\"19\">
      <P align=\"center\"><FONT style=\"FONT-SIZE: 9pt\"
face=\"Arial\">Order</FONT></P></TD>
    <TD width=\"11%\" bgColor=\"#cccccc\" height=\"19\">
      <P align=\"center\"><FONT style=\"FONT-SIZE: 9pt\"
      face=\"Arial\">Information</FONT></P></TD>
    <TD width=\"45%\" bgColor=\"#cccccc\" height=\"19\">
      <P align=\"center\"><FONT style=\"FONT-SIZE: 9pt\"
    face=\"Arial\">Currency</FONT></P></TD></TR>
  <TR>";
}
	while($row = mysql_fetch_array($result))
	{ 
		$prod_id = $row["pid"];
		$pid = $prod_id;
		$item = $row["pitem"];
		$title = $row["pdes"];
		$prices1 = $row["price"]; 
		$prices = number_format($prices1,2); 


			################ code to display picture thumbnail ################

	$tables = "stocks";
	$sqlone = "SELECT * from $tables where pid = $prod_id";
	$resultx = @mysql_query($sqlone,$conn)
	or die("SQL command fail - ".mysql_error());
	$getrows = mysql_fetch_array($resultx);
	$picture = $getrows["spic"];

	if (fopen($picture,"r") == NULL)
		$picture = "./images/nopic.jpg";	// set to no picture thumbnail

	#############################################################
		if ($style == 1) #style 1
		{
		echo("<tr>");
		echo"<td bgcolor=#ACD7E3><img src=images/click.gif border=0 height=25><a href=\"shop_showitem.php?pid=$prod_id\"><u>$prod_id</u></a></td>";
		echo("<td bgcolor=#FFFFFF>$item</td>");
		echo("<td bgcolor=#BBEEED>$title</td>");
		echo("<td bgcolor=#BEDAEB>$currency$prices</td></tr>");	
		}
		# style 2 #
		if ($style == 2)
	{
		$list.="<tr><TD width=\"14%\" height=\"110\">
<IMG height=\"72\" src=\"$picture\" width=\"72\" border=\"0\">
      <P>&nbsp;</P></TD>";
		
		$list.="<TD bgcolor = #BBEEED vAlign=\"top\" width=\"40%\" height=\"136\"><FONT style=\"FONT-SIZE: 9pt\" 
      face=\"Arial\"><SPAN class=\"boldtext\">Product ID: $prod_id</SPAN><SPAN class=\"smalltext\">&nbsp;<FONT 
      color=\"#000080\"><B>Item:</B></FONT> $item&nbsp;</SPAN></FONT>
      <P><FONT style=\"FONT-SIZE: 9pt\" face=\"Arial\"><SPAN class=\"normaltext\"><SPAN 
      class=\"redtext\"><B>Description:</b> $title</SPAN></FONT></P></TD>";

              	$list.="<TD width=13% bgColor=#ffffff height=136><a href=./shop_showitem.php?pid=$prod_id><center><img src =./images/order.gif  width = 30 height = 24 border = 0></a></center></TD><TD width=11% bgColor=#e1eaf0 height=136>";

	if ($_SESSION['admin'] != NULL)
		$list.="<a href= javascript:newWindow('./image.php?$pid','acePopup',650,500,'scrollbars=yes')><center><img src =./images/moreinfo.gif width=40 height=24 border = 0></a></center></TD>";
		else
		$list.="<a href= ./images.php?$prod_id><center><img src =./images/moreinfo.gif width=40 height=24 border =0></a></center></TD>";	
            
                             if ($wcurrency == "NO")
		   $list.="<TD width=45% height=136><p align = center>Our Price $currency$prices</p></TD></TR>";

                            if ($wcurrency == "ARS")
		$list.="<TD width=45% height=136><p align = center>Our Price <img src = ./images/argntina.gif> <br>$prices</p></TD></TR>";

		if ($wcurrency == "BRL")
		$list.="<TD width=45% height=136><p align = center>Our Price <img src = ./images/brazil.gif><br> $prices</p></TD></TR>";
		
		if ($wcurrency == "BND")
		$list.="<TD width=45% height=136><p align = center>Our Price <img src = ./images/brunei.gif><br>$prices</p></TD></TR>";

		if ($wcurrency == "CNY")
		$list.="<TD width=45% height=136><p align = center>Our Price <img src = ./images/china.gif> $prices</p></TD></TR>";

		if ($wcurrency == "EGY")
		$list.="<TD width=45% height=136><p align = center>Our Price <img src = ./images/egypt.gif><br> $prices</p></TD></TR>";

		if ($wcurrency == "HKD")
		$list.="<TD width=45% height=136><p align = center>Our Price <img src = ./images/hngkng97.gif><br> $prices</p></TD></TR>";

		if ($wcurrency == "INR")
		$list.="<TD width=45% height=136><p align = center>Our Price <img src = ./images/india.gif><br>$prices</p></TD></TR>";

		if ($wcurrency == "IDR")
		$list.="<TD width=45% height=136><p align = center>Our Price <img src = ./images/indonsia.gif><br> $prices</p></TD></TR>";

		if ($wcurrency == "KWD")
		$list.="<TD width=45% height=136><p align = center>Our Price <img src = ./images/kuwait.gif><br> $prices</p></TD></TR>";

		if ($wcurrency == "MYR")
		$list.="<TD width=45% height=136><p align = center>Our Price <img src = ./images/malaysia.gif> <br>$prices</p></TD></TR>";

		if ($wcurrency == "NZD")
		$list.="<TD width=45% height=136><p align = center>Our Price <img src = ./images/newzeal.gif> <br>$prices</p></TD></TR>";

		if ($wcurrency == "NOR")
		$list.="<TD width=45% height=136><p align = center>Our Price <img src = ./images/norway.gif> <br>$prices</p></TD></TR>";

		if ($wcurrency == "PKR")
		$list.="<TD width=45% height=136><p align = center>Our Price <img src = ./images/pakistan.gif> <br>$prices</p></TD></TR>";

		if ($wcurrency == "PHP")
		$list.="<TD width=45% height=136><p align = center>Our Price <img src = ./images/phillipn.gif><br> <br>$prices</p></TD></TR>";

		if ($wcurrency == "PLN")
		$list.="<TD width=45% height=136><p align = center>Our Price <img src = ./images/poland.gif> <br>$prices</p></TD></TR>";

		if ($wcurrency == "ROL")
		$list.="<TD width=45% height=136><p align = center>Our Price <img src = ./images/romania.gif> <br>$prices</p></TD></TR>";

		if ($wcurrency == "RUR")
		$list.="<TD width=45% height=136><p align = center>Our Price <img src = ./images/russia.gif> <br>$prices</p></TD></TR>";

		if ($wcurrency == "SAR")
		$list.="<TD width=45% height=136><p align = center>Our Price <img src = ./images/saudiar.gif><br>$prices</p></TD></TR>";

		if ($wcurrency == "ZAR")
		$list.="<TD width=45% height=136><p align = center>Our Price <img src = ./images/safrica.gif> <br>$prices</p></TD></TR>";

		if ($wcurrency == "KRW")
		$list.="<TD width=45% height=136><p align = center>Our Price <img src = ./images/skorea.gif><br>$prices</p></TD></TR>";

		if ($wcurrency == "SEK")
		$list.="<TD width=45% height=136><p align = center>Our Price <img src = ./images/sweden.gif><br> $prices</p></TD></TR>";

		if ($wcurrency == "CHF")
		$list.="<TD width=45% height=136><p align = center>Our Price <img src = ./images/switzrld.gif> <br>$prices</p></TD></TR>";

		if ($wcurrency == "TWD")
		$list.="<TD width=45% height=136><p align = center>Our Price <img src = ./images/taiwan.gif> <br>$prices</p></TD></TR>";
		if ($wcurrency == "SGD")
		$list.="<TD width=45% height=136><p align = center>Our Price <img src = ./images/singapor.gif> <br>$prices</p></TD></TR>";
		if ($wcurrency == "THB")
		$list.="<TD width=45% height=136><p align = center>Our Price <img src = ./images/thailand.gif><br> $prices</p></TD></TR>";
		if ($wcurrency == "VND")
		$list.="<TD width=45% height=136><p align = center>Our Price <img src = ./images/vietnam.gif> <br>$prices</p></TD></TR>";
		if ($wcurrency == "USD")
		$list.="<TD width=45% height=136><p align = center>Our Price <img src = ./images/usa.gif> <br>$prices</p></TD></TR>";
	}

	}
	if ($style == 1)
	echo"</td></tr></table></td></tr></table><br>";
	if ($style == 2)
	{
	$list.="<TR>
    	<TD width=14% height=1></TD>
    	<TD width=40% height=1></TD>
    	<TD width=13% height=1></TD>
    	<TD width=11% height=1></TD>
    	<TD width=40% height=1></TD></TR></TBODY></TABLE></table>";
$list.= "<br><center><p align = right><A href=.\shop_viewcart.php>View Shopping Basket</a><A href=.\shop_checkout.php><IMG src=./images/cart.gif width=42 height=50 border=0 alt=Cart></A></p></center>";
	}
	if ($style == 2)
	echo($list);
	// Display links at the bottom to indicate current page and number of pages displayed
	if ($style==2)
	echo "<table border=0 width=100% align = middle><tr align=center class=subtitle><td><a href=./shop_showall.php><img src =./images/our-prods.gif border=0 height=60></a></td><td><a href = ./shop_viewcart.php><img src =./images/carts.gif height=18 border=0><font face=arial size=1 color=#0F0F0F><b>VIEW CART     </b></font></a></td><td><a href = ./shop_delcart.php><img src =./images/trashcan.gif height=18 border=0><font face=arial size=1 color=#0F0F0F><b>EMPTY CART</b></font></a></td><td><a href = ./shop_updcart.php><img src = ./images/upd-cart.gif border=0 width=27 height=27><font face=arial size=1 color=#0F0F0F><b>UPDATE CART</b></font></a></td></tr></table>";
	mysql_free_result($result);
	if ($style == 1)
	{
	echo("<br><center><strong><font color = $highlight face=verdana size=2>To view a product item, click on the link shown above and click it!</font></strong></center><br>");
	echo "<table border=0 width=100% align = middle><tr align=center class=subtitle><td><a href=./shop_showall.php><img src =./images/our-prods.gif border=0 height=60></a></td><td><a href = ./shop_viewcart.php><img src =./images/carts.gif height=18 border=0><font face=arial size=1 color=#0F0F0F><b>VIEW CART     </b></font></a></td><td><a href = ./shop_delcart.php><img src =./images/trashcan.gif height=18 border=0><font face=arial size=1 color=#0F0F0F><b>EMPTY CART</b></font></a></td><td><a href = ./shop_updcart.php><img src = ./images/upd-cart.gif border=0 width=27 height=27><font face=arial size=1 color=#0F0F0F><b>UPDATE CART</b></font></a></td></tr></table>";
	
	}
} 
if ($totalrows == 0)
{
	echo("<center><font face=verdana size=2 color=#666666>Your search string return 0 matches - please try another search string</font></center><br><br>");
	
}	
if ($totalrows > 0)
echo "<center><hr><a href=store.php><img src=images/homes.gif border=0></a><a href=javascript:history.back()><img src=images/previous-1.gif border = 0 height=20></a></center></td></tr></table>";
else
echo "<center><hr><a href=store.php><img src=images/homes.gif border=0></a><a href=javascript:history.back()><img src=images/previous-1.gif border = 0 height=20></a></center></td></tr></table>";	
}

###### function to count number of words in a string #####


function count_words($string) {

    $string = eregi_replace(" +", " ", $string);
    $string = explode(" ", $string);

    while (list(, $word) = each ($string)) {
        if (eregi("[0-9A-Za-zÀ-ÖØ-öø-ÿ]", $word)) {
            $word_count++;
        }
    }
    return($word_count);
}
?>
</body>
</htm>