<?php
include ("ad_header.php");
?>
<HTML><HEAD><TITLE>Поиск товара</TITLE>
<META http-equiv=Content-Type content="text/html; charset=windows-1251">
</HEAD>
<BODY text=#000000 vLink=#00468c aLink=#ff6633 link=#0053a2 bgColor=#ffffff>
<script language="JavaScript">
function sr() {
if (pt.value!="") {
location.href=pt.value;
}
}
</script>
<table bgcolor=#D3D3D3><form action='ad_goods.php' method=post>
<tr><th><font size=2>Значение</th><th><font size=2>Место поиска</th><th><font size=2>Включаем?</th></tr>
<tr bgcolor=white><td><input type=text name='name' size=45></td><td><center>Название</td><td><center><input type=checkbox name=find_name></td></tr>
<tr bgcolor=white><td>
<select name=category>
<?php
$query="select * from subcategories";
$res=mysql_query($query);
while ($myrow=mysql_fetch_assoc($res)) {
$name_of_category=$myrow["name"];
$name_of_category=stripslashes($name_of_category);
$id=$myrow["first"];
echo "<option value=$id>$name_of_category";
}
?>
</select>
</td><td><center>Категория</td><td><center><input type=checkbox name=find_category></td></tr>
<tr bgcolor=white><td>
<select name=type>
<option value=one>Уникальный
<option value=mult>Несколько одинаковых
<option value=unlim>Неограниченное количество
</select>
</td><td><center>Тип аукциона</td><td><center><input type=checkbox name=find_type></td></tr>
<tr bgcolor=white><td> от <input type=text name='minprice' size=4> до <input type=text name='maxprice' size=4></td><td><center>Цена</td><td><center><input type=checkbox name=find_price></td></tr>
<tr bgcolor=white><td> Аукцион закончится в течении <input type=text name='end' size=4> часов</td><td><center>Дата окончания</td><td><center><input type=checkbox name=find_srok></td></tr>
<tr bgcolor=white><td colspan=3><center><input type=hidden name=action value=search><input type=submit value='Найти'></td></tr>
</table>
</form>

<?php
if (isset($_POST['action']) && $_POST['action']=="search") {
$magic=get_magic_quotes_gpc();
$where="";
if (isset($_POST['find_name'])) {
$name=$_POST['name'];
$name=str_replace ('"',"'",$name);
if ($magic!=1) {
$name=mysql_escape_string($name);
}
if ($where=="") {
$where="name like ('%$name%')";
}
else {
$where=$where." and name like ('%$name%')";
}
}

if (isset($_POST['find_category'])) {
$category=$_POST['category'];
$category=str_replace ('"',"'",$category);
if ($magic!=1) {
$category=mysql_escape_string($category);
}
if ($where=="") {
$where="parent='$category'";
}
else {
$where=$where." and parent='$category'";
}
}

if (isset($_POST['find_type'])) {
$type=$_POST['type'];
$type=str_replace ('"',"'",$type);
if ($magic!=1) {
$type=mysql_escape_string($type);
}
if ($where=="") {
$where="type='$type'";
}
else {
$where=$where." and type='$type'";
}
}


if (isset($_POST['find_price'])) {
$maxprice=$_POST['maxprice'];
$maxprice=str_replace ('"',"'",$maxprice);
$minprice=$_POST['minprice'];
$minprice=str_replace ('"',"'",$minprice);
if ($magic!=1) {
$maxprice=mysql_escape_string($maxprice);
$minprice=mysql_escape_string($minprice);
}
if ($where=="") {
$where="price>='$minprice' and price<='$maxprice'";
}
else {
$where=$where." and price>='$minprice' and price<='$maxprice'";
}
}

if (isset($_POST['find_srok'])) {
$nowtime=time();
$end=$_POST['end'];
$end=str_replace ('"',"'",$end);
if ($magic!=1) {
$end=mysql_escape_string($end);
}
$end=$end*3600;
if ($where=="") {
$where="(srok-$nowtime)<=$end";
}
else {
$where=$where." and (srok-$nowtime)<=$end";
}
}
if (isset($_POST['find_name']) || isset($_POST['find_category']) || isset($_POST['find_type']) || isset($_POST['find_price']) || isset($_POST['find_srok'])) {
$search_query="select first,name from goods where ($where)";
$res_search=mysql_query($search_query);
$num=mysql_num_rows($res_search);
echo "Результаты поиска ($num):<br><br>";
echo "<table bgcolor=#D3D3D3>";
while ($srow=mysql_fetch_assoc($res_search)) {
$name_of_good=$srow["name"];
$name_of_good=stripslashes($name_of_good);
$id=$srow["first"];
echo "<form action='ad_lot.php' method=post>";
echo "<tr bgcolor=white><td><center>$name_of_good</center></td><td><input type=hidden name=com value='$id'><input type=submit value='Подробнее'></td></tr>";
echo "</form>";
}
echo "</table>";
}

}
?>
</BODY></HTML>
<?php
mysql_close ($link);
?>