Location: PHPKode > projects > Dominant Creature RPG > class/shop_class.php
<?php



require_once ("data_class.php");



class Shop extends Data {





	function Init() {



		$this->GetUserData();



	}



	function NewItem($id, $qty, $price) {





		$cost = $price * $qty;

		//echo $price, $qty;



		$gold_left = $this->creature[gold] - $cost;



		if ($cost > $this->creature[gold]) {

			$status = "You can't afford to buy this and spend $cost gold. You only have ".$this->creature[gold]." gold. <br /><a href='?p=shop'>Go back.</a>";

		} else {

			$status = "You bought $qty items for $cost gold. Go back to <a href='?p=shop'>shop</a> or choose menu item on left.";

		

		$q1 = mysql_query("INSERT into creature_inventory VALUES ('', '".$this->creature[user_id]."', '$id', '$qty', '0')");

		$q2 = mysql_query("UPDATE player_creatures SET gold = '$gold_left'");

		

		}

		echo "<table width='560' align='left' border='0'>";

		echo "<tr><td colspan='3' style='border-bottom: 1px solid #ccc;'>Welcome to the shop. You can purchase various items here:</td></tr>";

		echo "<tr><td colspan='3'>$status</td></tr>";

		echo "</table>";



	}



	function ExistingItem($id, $qty, $price) {



		$cost = $price * $qty;



		$gold_left = $this->creature[gold] - $cost;

		

		if ($cost > $this->creature[gold]) {

			$status = "You can't afford to buy this and spend $cost gold. <a href='?p=shop'>Go back.</a>";

		} else {

			$status = "You bought $qty items for $cost gold (existing). Go back to <a href='?p=shop'>shop</a> or choose menu item on left.";



		$q1 = mysql_query("UPDATE creature_inventory SET qty = qty + $qty WHERE item = '$id' AND creature = '".$this->creature[user_id]."' ");

		$q2 = mysql_query("UPDATE player_creatures SET gold = '$gold_left'");



		}

	

		echo "<table width='560' align='left' border='0'>";

		echo "<tr><td colspan='3' style='border-bottom: 1px solid #ccc;'>Welcome to the shop. You can purchase various items here:</td></tr>";

		echo "<tr><td colspan='3'>$status</td></tr>";

		echo "</table>";



	}



	function Buy() {



		$this->Init();



		$ar_items_qty = $_POST['it'];

		$ar_items_id = $_POST['iid'];

		$ar_items_price = $_POST['iip'];

		



		//echo $ar_items_id[$i] . "," . $ar_items_qty[$i];





		$t = mysql_query("SELECT id FROM creature_inventory WHERE item = '".$ar_items_id."' AND creature = ".$this->creature[user_id]."");



		

		if (empty($ar_items_qty) OR preg_match("/[^0-9]+/", $ar_items_qty)) {

			

			echo "<table width='560' align='left' border='0'>";

			echo "<tr><td>Obviously, something has gone wrong! Go <a href='?p=shop'>back</a> and try to repeat action if you think it should work, or try to do something else.</td></tr>";

			echo "</table>";

			

		}

		

		else

		

		if (mysql_num_rows($t) > 0 && $ar_items_qty != "") {

			//echo "Item already exsists, and this one was added to stock.<br />";

				

			$this->ExistingItem($ar_items_id, $ar_items_qty, $ar_items_price);

				

		}



		else





		if (mysql_num_rows($t) == 0 && $ar_items_qty != "") {

			//echo "No record of such items found in inventory and we'll add it.<br />";

				

			$this->NewItem($ar_items_id, $ar_items_qty, $ar_items_price);

		}





		else {

				

			echo "<table width='560' align='left' border='0'>";

			echo "<tr><td>Obviously, something has gone wrong! Go back and try to repeat action if you think it should work, or try to do something else.</td></tr>";

			echo "</table>";

				

		}







	}





	function Intro() {



		$this->Init();



		if ( isset($_POST['buy_this']) ) {

			$this->Buy();

		}



		else {



			$l_min = $this->creature[level] - 5;

			if ($l_min <= 1) {$l_min = 1;}

			$l_max = $this->creature[level] + 5;





			$si = mysql_query("

	SELECT *

		FROM

	shop_items

	WHERE id > 1

	AND

	level >= $l_min

	AND

	level <= $l_max

	");	



			echo "<table width='560' align='left' border='0'>";

			echo "<tr><td style='border-bottom: 1px solid #ccc;'>Welcome to the shop. You can purchase various items here:</td></tr>";



			$i = 1;

			$max = mysql_num_rows($si);

			while ($i <= $max) {



				$item[$i] = mysql_fetch_assoc($si);



				echo "<tr>

	<td><form action='?p=shop' method='post'><table align='center' width='500' border='0'><tr><td>

	<tr><td style='border-bottom: 1px solid #ccc;'><b>Item</b></td><td style='border-bottom: 1px solid #ccc;'><b>Quantity</b></td>

	<td style='border-bottom: 1px solid #ccc;'><b>Price</b></td>

	<td style='border-bottom: 1px solid #ccc;'><b>Buy?</b></td></tr>

	<td width='200'><b title='header=[Item info] body=[".$item[$i][description]."]'>".$item[$i][iname]."</b></td>

	<td width='100'><input type='hidden' value='".$item[$i][id]."' name='iid' />

	<input type='hidden' value='".$item[$i][price]."' name='iip' />

	<input type='text' style='width:50px;' maxlength='3' name='it' /></td>

	<td width='100'>".$item[$i][price]." gold

	</td>

	<td width='100'><input type='submit' name='buy_this' value='Buy' />

	</td></tr></table></form>

	</td></tr>";



				$i++;

			}



			echo "</table>";



		}



	}



}

?>
Return current item: Dominant Creature RPG