<?


include 'connect.php';

//Только для авторизованных
if($u['id']){

if (isset($_GET['submit'])) {
$sex = $_GET['sex'];
$age1 = $_GET['age1'];
$age2 = $_GET['age2'];
$city = check(substr($_GET['city'], 0, 100));
$online = $_GET['online'];
header ("Location: search.php?act=go&sex=$sex&age1=$age1&age2=$age2&city=$city&online=$online");
}

switch ($_GET['act']) {

//Результат ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
case 'go':
//Пользователей на страницу
if($u['setting_users_list']) $count = $u['setting_users_list']; else $count = 10;

$sex = $_GET['sex'];
if($sex == 'zh' || $sex == 'm') $sexq = "AND `sex` = '".$sex."'"; else $sexq = "";

$age1 = $_GET['age1'];
if ($age1 > 0 && $age1 < 90){
$year = date('Y') - $age1 + 1;
$age1q = "AND `anketa_byear` > '0' AND `anketa_byear` < '".$year."'";
} else { $age1q = ""; }

$age2 = $_GET['age2'];
if ($age2 > 0 && $age2 < 90){
$year = date('Y') - $age2 - 1;
$age2q = "AND `anketa_byear` > '0' AND `anketa_byear` > '".$year."'";
} else { $age2q = ""; }

$city = check(substr($_GET['city'], 0, 100));
if($city) $cityq = "AND `anketa_city` = '".$city."'"; else $cityq = "";

$online = $_GET['online'];
if($online) $onlineq = "AND `online` > '".(time()-$system['online_time'])."'"; else $onlineq = "";

if($sexq || $age1q || $age2q || $cityq || $onlineq) $where = "WHERE `id` > '0'"; else $where = "";

$title = $title.' | Поиск людей';
include 'head.php';

echo '<div class="block_back">';
echo '<a href = "search.php">Новый поиск</a>';
echo '</div>';

echo '<div class="title"><img src= "img/search.gif"> <b>Поиск людей</b></div>';

$all = mysql_result(mysql_query("SELECT COUNT(*) FROM `users` $where $sexq $age1q $age2q $onlineq $cityq"), 0);
echo '<div class = "block_view">Найдено: <b>'.$all.'</b></div>';
echo '<div class="main_menu">';
if($all > 0){
   $total = intval(($all - 1) / $count) + 1;
   $page = num($_GET['page']);
   if(empty($page) or $page < 0) $page = 1; 
   if($page > $total) $page = $total;
   $start = $page * $count - $count;
   $result = mysql_query("SELECT * FROM `users` $where $sexq $age1q $age2q $onlineq $cityq ORDER BY rating DESC LIMIT $start, $count");
      while($user = mysql_fetch_assoc($result)){
	  //Вывод
	  echo '<div class="block_list">';
	  echo '<table>
      <tr>
      <td>';
	  ava($user['id'],50);
	  echo '</td>
	  <td VALIGN=top>';
	  echo ''.ico($user['sex'],$user['admin']).' <a href = "page.php?id='.$user['id'].'"><b>'.$user['login'].'</b></a> '.online($user['online']).'';
	  
	  echo '<br/>';
	  
	  //if($u['id']){
	  //if($user['id'] != $u['id']){
	  //echo '<img src = "img/mail.gif"> <a href = "mail.php?act=write&id='.$user['id'].'">Сообщение</a><br/>';
	  ////Проверяем не друг ли
      //$req = mysql_query("SELECT * FROM `friends` WHERE `user` = '".$u['id']."' and friend = '".$user['id']."' LIMIT 1");
      //if(mysql_num_rows($req))
      //echo '<img src = "img/friends.gif"> <a href = "friends.php?act=delete&id='.$user['id'].'">Убрать из друзей</a><br/>';
      //else 
	  //echo '<img src = "img/friends.gif"> <a href = "friends.php?act=adding&id='.$user['id'].'">Добавить в друзья</a><br/>';
	  //}
	  //}
	  
	  echo '<br/>';
	  echo '</td>
      </tr>
      </table>';
      echo '</div>';
	  }
	  navigation($page, $total,'search.php?act=go&sex='.$sex.'&age1='.$age1.'&age2='.$age2.'&city='.$city.'&online='.$online.'&');
}else{
echo 'Пользователей не найдено!';
}
echo '</div>';
include 'foot.php';
break;


//Поля для поиска ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
default:

$title = $title.' | Поиск';
include 'head.php';
//Подзаголовок
echo '<div class="title"><img src= "img/search.gif"> <b>Поиск людей</b></div>';
echo '<div class="block_action">';
echo '<form action="search.php" method=get>';
echo '
Пол: <br/><select name="sex">
<option value="0">- Не выбран -</option>
<option value="zh">Женский</option>
<option value="m">Мужской</option>
</select><br/>';
echo 'Возраст: <br/> От: <input type=text SIZE=2 name=age1 maxlength=2>До: <input type=text SIZE=2 name=age2 maxlength=2><br/>';
echo 'Город: <br/><input type=text name=city maxlength=50><br/>';
echo '<input name="online" type="checkbox" value="1">&nbsp;Только онлайн<br/>';
echo '
<input type=submit name="submit" class="btn" value="Поиск" class="submit white"/></form>';
echo '</div>';
include 'foot.php';
break;
}
} else {
header ("Location: aut.php");
}
?>