Yo Yo Cronwerks, Here is a little Crystal Market i decided to recode a little bit, enjoy!
cmarket.php:
<?php
include_once(DIRNAME(__FILE__) . '/globals.php');
echo "<h3>Crystal Market</h3><h4>Welcome to the Crystal Market, Here you can either buy Crystals or sell your own!</h4><br />";
$_GET['ID'] = abs(@intval($_GET['ID']));
switch ($_GET['action']) {
case "buy": crystal_buy(); break;
case "remove": crystal_remove(); break;
case "add": crystal_add(); break;
default: cmarket_index(); break;
}
function cmarket_index()
{
global $db, $ir, $c, $userid, $h;
echo "> <a href='cmarket.php?action=add'>Add Crystals</a><br /><br />
<table width='90%' cellspacing='1' class='table'><tr><th>Name</th><th>Crystals</th><th>Cost Each</th><th>Total Price</th><th>Links</th></tr>";
$q = $db->query("SELECT cm.*, u.* FROM crystalmarket cm LEFT JOIN users u ON u.userid=cm.cmADDER ORDER BY cmPRICE/cmQTY ASC");
while ($r = $db->fetch_row($q)) {
if ($r['cmADDER'] == $userid) {
$link = "<a href='cmarket.php?action=remove&ID={$r['cmID']}'>Remove</a>";
} else {
$link = "<a href='cmarket.php?action=buy&ID={$r['cmID']}'>Buy</a>";
}
$each = (int) $r['cmPRICE'] / $r['cmQTY'];
echo "<tr><td><a href='viewuser.php?u={$r['userid']}'>{$r['username']}</a> [{$r['userid']}]</td><td>{$r['cmQTY']}</td><td> \$" . number_format($each) . "</td><td>\$" . number_format($r['cmPRICE']) . "</td><td>[$link]</td></tr>";
}
echo "</table>";
}
function crystal_remove()
{
global $db, $ir, $c, $userid, $h;
$q = $db->query("SELECT * FROM crystalmarket WHERE cmID='{$_GET['ID']}' AND cmADDER=$userid");
if (!$db->num_rows($q)) {
echo "<b>Error:</b> These Crystals do not exist or you do not own them!<br /><br /> > <a href='cmarket.php'>Back</a>";
$h->endpage();
exit;
}
$r = $db->fetch_row($q);
$db->query("UPDATE users SET crystals=crystals+{$r['cmQTY']} where userid=$userid");
$db->query("DELETE FROM crystalmarket WHERE cmID='{$_GET['ID']}'");
echo "<b>Success:</b> You have removed your Crystals from the market.<br /><br /> > <a href='cmarket.php'>Back</a>";
$h->endpage();
exit;
}
function crystal_buy()
{
global $db, $ir, $c, $userid, $h;
$q = $db->query("SELECT * FROM crystalmarket cm WHERE cmID='{$_GET['ID']}'");
if (!$db->num_rows($q)) {
echo "<b>Error:</b> These Crystals do not exist or they have already been bought!<br /><br /> > <a href='cmarket.php'>Back</a>";
$h->endpage();
exit;
}
$r = $db->fetch_row($q);
if ($r['cmPRICE'] > $ir['money']) {
echo "<b>Error:</b> You do not have enough money to buy them Crystals!<br /><br /> > <a href='cmarket.php'>Back</a>";
$h->endpage();
exit;
}
$db->query("UPDATE users SET crystals=crystals+{$r['cmQTY']} where userid=$userid");
$db->query("DELETE FROM crystalmarket WHERE cmID='{$_GET['ID']}'");
$db->query("UPDATE users SET money=money-{$r['cmPRICE']} where userid=$userid");
$db->query("UPDATE users SET money=money+{$r['cmPRICE']} where userid={$r['cmADDER']}");
event_add($r['cmADDER'], "<a href='viewuser.php?u=$userid'>{$ir['username']}</a> bought your {$r['cmQTY']} Crystals from the market for \$" . number_format($r['cmPRICE']) . ".", $c);
echo "You bought {$r['cmQTY']} Crystals from the market for \$" . number_format($r['cmPRICE']) . ".";
}
function crystal_add()
{
global $db, $ir, $c, $userid, $h;
$_POST['amnt'] = abs((int) $_POST['amnt']);
$_POST['price'] = abs((int) $_POST['price']);
if ($_POST['amnt']) {
if ($_POST['amnt'] > $ir['crystals']) {
echo "<b>Error:</b> You do not have that many Crystals!<br /><br /> > <a href='cmarket.php'>Back</a>";
$h->endpage();
exit;
}
$tp = $_POST['amnt'] * $_POST['price'];
$db->query("INSERT INTO crystalmarket VALUES('',{$_POST['amnt']},$userid,$tp)");
$db->query("UPDATE users SET crystals=crystals-{$_POST['amnt']} WHERE userid=$userid");
echo "<b>Success:</b> You have added your Crystals to the market!<br /><br /> > <a href='cmarket.php'>Back</a>";
$h->endpage();
exit;
} else {
echo "You have <b>{$ir['crystals']}</b> Crystals<br /><br /><form action='cmarket.php?action=add' method='post'><table width='60%' cellspacing='1' class='table'><tr>
<th>Crystals</th><th>Cost Each</th></tr>
<td style='text-align:center;'><input type='text' name='amnt' value='{$ir['crystals']}' /></td>
<td style='text-align:center;'><input type='text' name='price' value='100'/></td></tr><tr>
<td colspan='2' align='center'><input type='submit' value='Add To Market' /></tr></table></form>";
}
echo "<br /><hr />> <a href='cmarket.php'>Back</a>";
}
$h->endpage();
?>
That's it enjoy, Any problems let me know!