Location: PHPKode > projects > Crown of Evanion > final/shops.php
<?php
/*
*   This file is part of 'Crown of Evanion'.
*
*    'Crown of Evanion' is free software; you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation; either version 2 of the License, or
*    (at your option) any later version.
*
*    'Crown of Evanion' is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with 'Crown of Evanion'; if not, write to the Free Software
*    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
$title = "Shops";
include("include.php");

if(!$Username) {
	header("location: login.php");
	die;
}

# Removes entries with no items left in them
mysql_query("DELETE FROM stocks WHERE quan = '0'");

$errors = array(
'1' => "<h5>No such shop.</h5>",
'2' => "<h5>That item is out of stock.</h5>",
'3' => "<h5>You can't afford that.</h5>",
);

if(!$page) {
	$select = mysql_query("SELECT * FROM shops");
	echo "<div align=\"center\"><p class=\"drag\">";
	while($shops = mysql_fetch_array($select)) {
		echo "<a href=$PHP_SELF?page=shop&id=$shops[id]>$shops[name]</a> | ";
	}
			
	echo "<a href=trader.php>Trader</a></p>
	$errors[$error]</div>";
}

if($page == "shop" && $id) {
	$id = $_REQUEST['id'];
	$select = mysql_query("SELECT * FROM shops WHERE id = '$id'");
	$num = mysql_num_rows($select);
	
	if(!$num) {
		header("location: $PHP_SELF?error=1");
		include("footer.php");
		die;
	}
	$shop = mysql_fetch_array($select);

	echo "<div align=\"center\">
	<h2>$shop[name]</h2>
	<p>$shop[des]</p>
	";

	$select = mysql_query("SELECT * FROM stocks WHERE shop = '$id'");
	$num = mysql_num_rows($select);
	if($num == 0) {
		echo "<h5>Sorry, all sold out!</h5>";
	} else {
		while ($stocks = mysql_fetch_array($select)) {
			$sel = mysql_query("SELECT * FROM items WHERE id = '$stocks[itemid]'");
			$item = mysql_fetch_array($sel);
			
			echo "<div class=\"item\">
			<a href=\"$PHP_SELF?page=buy&id=$stocks[id]\">
			<img src=\"$item[image]\" alt=\"$item[name]\" width=\"90\" height=\"90\" border=\"0\">
			</a>
			<p class=\"name\">
			<a href=\"$PHP_SELF?page=buy&id=$stocks[id]\">
			$item[name]
			Quantity: $stocks[quan]
			Price: $item[price]
			</a>
			</p>
			</div>";			
		}
		echo "</div>";
	}			
}

if($page == "buy") {
	$id = $_REQUEST['id'];
	$select = mysql_query("SELECT * FROM stocks WHERE id = '$id'");
	while ($stock = mysql_fetch_array($select)) {
		if($stock[quan] <= 0) {
			header("location: $PHP_SELF?error=2");
			include("footer.php");
			die;
		}
		
		$select = mysql_query("SELECT * FROM items WHERE id = '$stock[itemid]'");
		$item = mysql_fetch_array($select);
	
		if($item[price] > $Money) {
			header("location: $PHP_SELF?error=3");
			include("footer.php");
			die;
		}	
		
		mysql_query("UPDATE users SET money = money-$item[price] WHERE id = '$UserID'");
		mysql_query("UPDATE funds SET amount = amount+$item[price] WHERE fund = 'shops'");
		mysql_query("INSERT INTO useritems (itemid,owner) VALUES ('$item[id]','$UserID')");
		mysql_query("UPDATE stocks SET quan = quan-1 WHERE id = '$stock[id]'");
		echo "<div align=\"center\"><strong>You've purchased a $item[name]!</div>";
		
	}
}
include("footer.php");
?>
Return current item: Crown of Evanion