All pastes #970384 Raw Edit

Something

public text v1 · immutable
·published 2008-04-04 00:41 UTC
rendered paste body
<?php
define('PUN_ROOT', './');
require PUN_ROOT.'include/common.php';
require PUN_ROOT.'lang/'.$pun_user['language'].'/cash.php';

if ($pun_user['is_guest'])
	message($lang_common['No view']);

if (isset($_POST['form_sent']))
{
	$items = isset($_POST['item']) && is_array($_POST['item']) ? $_POST['item'] : array();
	$deduct_cash = 0;
	$deduct_quantity = array();
	$sql = array();

	foreach ($items as $item_id => $to_buy)
	{
		if ($to_buy > 0)
		{
			$result = $db->query('SELECT item_name, item_quantity, item_cost FROM '.$db->prefix.'items WHERE item_id = '.$item_id) or error('Unable to fetch item information', __FILE__, __LINE__, $db->error());

			if ($db->num_rows($result) > 0)
			{
				$item_info = $db->fetch_assoc($result);

				if ($to_buy > $item_info['item_quantity'])
					message('You can not buy '.$to_buy.' '.pun_htmlspecialchars($item_info['item_name']).'(s): there is/are only '.$item_info['item_quantity'].' left!');

				if (($to_buy * $item_info['item_cost']) > $pun_user['cm_cash'])
					message('You don\'t have enough '.pun_htmlspecialchars($pun_config['cm_cur_name']).' to buy '.$to_buy.' '.pun_htmlspecialchars($item_info['item_name']).'.');
			
				$sql[] = '('.$item_id.', '.$pun_user['id'].', '.$to_buy.')';

				$deduct_cash += $to_buy * $item_info['item_cost'];
			}
			else
			{
				message($lang_common['Bad request']);
			}
		}
	}

	// If we're here, all of the items are valid to buy
	foreach ($items as $item_id => $to_buy)
	{
		$db->query('UPDATE '.$db->prefix.'items set item_quantity = item_quantity - '.$to_buy.' WHERE item_id = '.$item_id) or error('Unable to deduct item from store', __FILE__, __LINE__, $db->error());
	}

	$db->query('UPDATE '.$db->prefix.'users SET cm_cash = cm_cash - '.$deduct_cash.' WHERE id = '.$pun_user['id']) or error('Unable to deduct cash', __FILE__, __LINE__, $db->error());

	$db->query('INSERT INTO '.$db->prefix.'inventory (item_id, user_id, quantity) VALUES '.implode(',', $sql).' ON DUPLICATE KEY UPDATE quantity = quantity + VALUES(quantity)') or error('Unable to add items to inventory', __FILE__, __LINE__, $db->error());

	redirect($_SERVER['REQUEST_URI'], 'Items purchased. Redirecting &hellip;');
}

$page_title = pun_htmlspecialchars($pun_config['o_board_title'].' - Shop');
define('PUN_ALLOW_INDEX', 1);
require PUN_ROOT.'header.php';

?>
<div class="block">
	<div class="box">
    	<div class="inbox">
<p><strong>If you are looking to buy user account changes, then please go to the <a href="shop.php">Shop</a> which will sell these to you. This section is for Chibis, Edible Items, and Misc Items.</strong></p>
		</div>
	</div>
</div>
		<div class="blocktable">
			<h2><span>Buy some items</span></h2>
			<div class="box">
					<div class="inbox">
						<form method="post" action="<?php echo $_SERVER['REQUEST_URI'] ?>">
						<input type="hidden" name="form_sent" value="1" />
							<div class="inform">
								<table cellspacing="0">
									<thead>
										<tr>
                                        	<th class="tc2" scope="col">Image</th>
                                           	<th class="tcl" scope="col">Item Name</th>
                                           	<th class="tcl" scope="col">Description</th>
											<th class="tc2" scope="col">Price</th>
											<th class="tc3" scope="col">Rarity</th>
											<th class="tc3" scope="col">Quantity Remaining</th>
											<th class="tcr" scope="col">Number to Buy</th>
										</tr>
									</thead>
									<tbody>
<?php

// Display all the categories and forums
$result = $db->query('SELECT * FROM '.$db->prefix.'items') or error('Unable to fetch item list', __FILE__, __LINE__, $db->error());

while ($cur_item = $db->fetch_assoc($result))
{

?>
										<tr>
                                        	<td class="tc2"><img src="img/items/<?php echo pun_htmlspecialchars($cur_item['item_image']) ?>" alt="<?php echo pun_htmlspecialchars($cur_item['item_name']) ?>"/>
											<td class="tcl"><?php echo pun_htmlspecialchars($cur_item['item_name']) ?></td>
											<td class="tcl"><?php echo pun_htmlspecialchars($cur_item['description']) ?></td>
											<td class="tc2"><?php echo $cur_item['item_cost'] ?> <?php echo pun_htmlspecialchars($pun_config['cm_cur_name']) ?></td>
											<td class="tc3"><?php echo $cur_item['item_rarity'] ?></td>
											<td class="tc3"><?php echo $cur_item['item_quantity'] ?></td>
											<td class="tcr"><input type="text" name="item[<?php echo $cur_item['item_id'] ?>]" value="0" /></td>
										</tr>
<?php

}

?>
									</tbody>
								</table>
							</div>
							<p class="submitend"><input type="submit" name="buy" value="Buy selected items" /></p>
						</form>
					</div>
			</div>
		</div>
<?php

require PUN_ROOT.'footer.php';