<?php
session_start();
include ('private/mysql.php');
include ('tmp/head.php');

switch($sd){
default:

    echo "[<a href='shopweapon.php'>Оружие</a>] [<a href='shoparmor.php'>Доспехи</a>] [<a href='shopelixir.php'>Эликсиры</a>] [Бижутерия]<br><hr>";

    if($_GET['tip']==''){$t="ring";}
    if($_GET['tip']=='ear'){$t="ear";}
    
    $tip = mysql_real_escape_string(htmlspecialchars($t));
    
    if($tip=='ring'){echo "Кольца<br><a href='shopjewelry.php?tip=ear'>Серьги</a><hr>";}
    if($tip=='ear'){echo "<a href='shopjewelry.php'>Кольца</a><br>Серьги<hr>";}

	$num = 10; 
	$page = (int)mysql_real_escape_string(htmlspecialchars(strip_tags($_GET['page']))); 
	$result = mysql_query("SELECT * FROM `shop` WHERE `tip`='$tip'") or die(mysql_error()); 
	$posts = mysql_num_rows($result); 
	$total = intval(($posts - 1) / $num) + 1; 
	$page = intval($page); 
	if(empty($page) or $page < 0) $page = 1; 
    if($page > $total) $page = $total; 
	$start = $page * $num - $num; 
 

    $q = mysql_query("SELECT * FROM `shop` WHERE `tip`='$tip' ORDER BY `level` ASC, `silver` ASC LIMIT $start, $num");
    
    While($w = mysql_fetch_assoc($q)){
        
        
        if($w['level'] > $user['level']){$buys = "[<font color=grey>Купить</font>]";}else{$buys = "<a style='color:gold' href='shopjewelry.php?sd=buy&id=$w[id]'>[Купить <small><img src='img/silver.png'> $w[silver]</small>]</a>";}
        
        echo "<span style='float:left'><img src='/img/shmot/$w[img]' width='50'></span>&nbsp;$w[name] $w[level] ур.<br>&nbsp;Интеллект: $w[int]<br>
        &nbsp;$buys<hr>";
        
    }
	if ($page != 1) $pervpage = '<a href=?t='.$tip.'&page=1>««</a> 
                               <a href=?t='.$tip.'&page='. ($page - 1) .'>«</a> '; 
	if ($page != $total) $nextpage = ' <a href=?t='.$tip.'&page='. ($page + 1) .'>»</a> 
                                   <a href=?t='.$tip.'&page=' .$total. '>»»</a>'; 

	if($page - 2 > 0) $page2left = ' <a href=?t='.$tip.'&page='. ($page - 2) .'>'. ($page - 2) .'</a> | '; 
	if($page - 1 > 0) $page1left = '<a href=?t='.$tip.'&page='. ($page - 1) .'>'. ($page - 1) .'</a> | '; 
	if($page + 2 <= $total) $page2right = ' | <a href=?t='.$tip.'&page='. ($page + 2) .'>'. ($page + 2) .'</a>'; 
	if($page + 1 <= $total) $page1right = ' | <a href=?t='.$tip.'&page='. ($page + 1) .'>'. ($page + 1) .'</a>'; 

	echo $pervpage.$page2left.$page1left.'<b>'.$page.'</b>'.$page1right.$page2right.$nextpage; 

break;

///////////////////////

case 'buy':

$q = mysql_query("SELECT * FROM `shop` WHERE `id`='$id'");
$e = mysql_num_rows($q);
$w = mysql_fetch_assoc($q);
if($e == '0'){
    header('Location: shopjewelry.php');
    exit;
}

if($user['level'] < $w['level']){
    header('Location: shopjewelry.php');
    exit;
}

if($user['almaz'] < $w['almaz']){
    echo "У вас недостаточно <img src='img/almaz.png'> алмазов для покупки.<br><a href='shopjewelry.php'>Назад</a>";
    include ('tmp/foot.php');
    exit;
}
if($user['silver'] < $w['silver']){
    echo "У вас недостаточно <img src='img/silver.png'> серебра для покупки.<br><a href='shopjewelry.php'>Назад</a>";
    include ('tmp/foot.php');
    exit;
}




mysql_query("INSERT INTO `item` SET 
`user`='$uid',
`name`='$w[name]',
`tip`='$w[tip]',
`level`='$w[level]',
`almaz`='$w[almaz]',
`silver`='$w[silver]',
`img`='$w[img]',
`hp`='$w[hp]',
`mp`='$w[mp]',
`min_u`='$w[min_u]',
`max_u`='$w[max_u]',
`int`='$w[int]',
`status`='not'");

mysql_query("UPDATE `users` SET `almaz`=`almaz`-'$w[almaz]',`silver`=`silver`-'$w[silver]' WHERE `id`='$uid'");

echo "Вы купили <b>$w[name]</b><br><a href='shopjewelry.php'>Вернуться</a>";


break;

///////////////////////




}
include ('tmp/foot.php');
?>