Hey,
So I continue to learn(when I CBA). I Was jusr practising on the cmarket.php, as was wondering what doesn't need to be there, or what's a better option. Here it is:
<?
require_once(DIRNAME(__FILE__). '/globals.php');
$_GET['action'] = (isset($_GET['action'])) && (is_string($_GET['action'])) ? (strtolower(trim($_GET['action']))) : FALSE;
$_GET['ID'] = (abs((int)$_GET['ID']));
echo '<h3>Crystal Market</h3>';
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 '[url="cmarket.php?action=add"]Add a Listing[/url]
<table width = "75%" `cellspacing` = "1" class = "table">
<tr style="background:gray">
<th>Adder</th>
<th>Qty</th>
<th>Price each</th>
<th>Price total</th>
<th>Links</th>
</tr>
';
$Cmarket_info = ($db->fetch_single($db->query("SELECT cm.*, u.*
FROM crystalmarket cm
LEFT JOIN users u
ON u.userid = cm.cmADDER
ORDER BY cmPRICE/cmQTY ASC
")));
while ( $user_adder = ($db->fetch_row($Cmarket_info)) )
{
if ( (number_format($user_adder['cmADDER'])) == ($_GET['ID']) )
{
$link = '<a href = "cmarket.php?action=remove&ID = '.number_format($user_adder['cmID'].'">Remove?</a>';
}
else
{
$link = '<a href = "cmarket.php?action=buy&ID = '.number_format($user_adder['cmID']).'">Buy</a>';
}
$each = (number_format($user_adder(['cmPRICE']))) / (number_format($user_adder(['cmQTY'])));
echo '\n
<tr>
<td>[url="viewuser.php?u='.number_format($user_adder['userid']).'"]'.$user_adder['username'].'[/url]
['.number_format($user_adder['userid']).']
</td>
<td>'.number_format($user_adder['cmQTY']).'</td>
<td>($each)</td>
<td>$'.number_format($user_adder['cmPRICE']).'</td>
<td>[$link]</td>
</tr>
';
}
echo '</table>';
}
function crystal_remove()
{
global $db,$ir,$c,$userid,$h;
$Cmarket_info = ($db->fetch_single($db->query("SELECT *
FROM `crystalmarket`
WHERE `cmID` = ".($_GET['ID'])."
AND `cmADDER` = ".($_GET['ID'])."
")));
if ( (!$db->num_rows($Cmarket_info)) )
{
echo 'Error, either these crystals do not exist, or you are not the owner.
[url="index.php"]Home[/url]
';
$h->endpage();
exit;
}
$user_adder = ( ($db->fetch_row($Cmarket_info)) );
$db->query("UPDATE `users`
SET `crystals` = `crystals` + ".number_format($user_adder['cmQTY'])."
WHERE `userid` = ".($_GET['ID'])."
");
$db->query("DELETE FROM `crystalmarket`
WHERE `cmID` = ".($_GET['ID'])."
");
echo 'Crystals removed from market!
[url="home.php"]Home[/url]
';
$h->endpage();
exit;
}
function crystal_buy()
{
global $db,$ir,$c,$userid,$h;
$Cmarket_info = ($db->fetch_single($db->query("SELECT *
FROM crystalmarket cm
WHERE `cmID` = ".($_GET['ID'])."
")));
if ( (!$db->num_rows($Cmarket_info)) )
{
echo 'Error, either these crystals do not exist, or they have already been bought.
[url="home.php"]Home[/url]
';
$h->endpage();
exit;
}
$user_adder = ( ($db->fetch_row($Cmarket_info)) );
if ( (number_format($user_adder['cmPRICE']) > ($ir['money'])) )
{
echo 'Error, you do not have the funds to buy these crystals.
[url="home.php"]Home[/url]
';
$h->endpage();
exit;
}
$db->query("UPDATE `users`
SET `crystals` = `crystals` + ".number_format($user_adder['cmQTY'])."
WHERE `userid` = ".($_GET['ID'])."
");
$db->query("DELETE FROM `crystalmarket`
WHERE `cmID` = ".($_GET['ID'])."
");
$db->query("UPDATE `users`
SET `money` = `money` - ".number_format($user_adder['cmPRICE'])."
WHERE `userid` = ".($_GET['ID']."
");
$db->query("UPDATE `users`
SET `money` = `money` + ".number_forumat($user_adder['cmPRICE'])."
WHERE `userid` = ".number_forumat($user_adder['cmADDER'])."
");
event_add ($user_adder['cmADDER'] ,
'<a href = "viewuser.php?u=".($_GET['ID'])."">'.($ir['username']).'</a>
bought your '.number_format($user_adder['cmQTY']).' crystals from the market
for $'.number_format($user_adder['cmPRICE']).'.',$c);
echo 'You bought the '.number_format($user_adder['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']) > (number_format($ir['crystals'])) )
{
echo 'You are trying to add more crystals to the market than you have.';
$h->endpage();
exit;
}
$tp = ( ($_POST['amnt']*$_POST['price']) );
$db->query("INSERT INTO `crystalmarket`
VALUES ( '',".($_POST['amnt'])." , ".($_GET['ID'])." , ($tp) )
");
$db->query("UPDATE `users`
SET `crystals` = `crystals` - ".($_POST['amnt'])."
WHERE `userid` = ".($_GET['ID'])."
");
echo 'Crystals added to market!
[url="cmarket.php"]Back[/url]
';
$h->endpage()
exit;
}
else
{
echo '[b]Adding a listing.[/b]
You have [b]'.number_format($ir['crystals']).'[/b] crystal(s) that you can add to the market.
<form action = "cmarket.php?action=add" method = "post">
<table width = "50%" border = "2" >
<tr>
<td>Crystals:</td>
<td><input type = "text" name = "amnt" value = '.($ir['crystals']).' />
</td>
</tr>
<tr>
<td>Price Each:</td>
<td><input type = "text" name = "price" value = "200" />
</td>
</tr>
<tr>
<td colspan = "2" align = "center">
<input type = "submit" value = "Add To Market" />
</tr>
</table>
</form>
';
}
}
$h->endpage();
?>
Nothing Major, but I could do with some help like every body. :)