Location: PHPKode > projects > MyBlog: PHP and MySQL Blog/CMS software > os/market.php
 <?php include("template/top.php"); ?>
<div id="content">
<a href="index.php">Go Home</a><br />
<a href="market.php">(Refresh)</a><br />
<h2>Kudos Marketplace</h2>
<?php

if (empty($_COOKIE['login'])) {
echo "Please login/register to use this feature!";
}
else {
require("config/mysqlconnection.inc");
$result = mysql_query("SELECT * FROM myblog_market WHERE bought = 'no' ORDER BY num DESC") or die(mysql_error());
echo "<table cellspacing=\"2\"><tr><td>Title</td><td>Seller</td><td>Price</td><td>Actions</td></tr>";
while($row = mysql_fetch_array( $result )) {
echo "<tr><td>" . $row['item'] . "</td><td>" . $row['who'] . "</td><td><a href=\"market.php?mode=buy&item=" . $row['item'] . "\">" . $row['price'] . "</a></td>";

if (isset($_COOKIE['super'])) {
echo "<td><a href=\"market.php?delete=" . $row['item'] . "\">Delete</a></td></tr>";
}

elseif ($_COOKIE['login'] == $row['who'] AND empty($_COOKIE['super'])) {
echo "<td><a href=\"market.php?delete=" . $row['item'] . "\">Delete</a></td></tr>";
}

else {
echo "</tr>";
}
}
echo "</table>";
echo "<br /><br />";
$taxman = file_get_contents("tax_man.txt");
require("config/settings.inc");

if ($market_everyone == yes) {
echo "<h3>Sell an item</h3>";
echo "<p>Tax is currently 3% payable to " . $taxman . "</p>";
echo "<form action=\"sell.php\" method=\"post\">";
echo "<input type=\"text\" name=\"item\" value=\"Item Title\" /><br />";
echo "<input type=\"text\" name=\"content\" value=\"Actual Item\" /><br />";
echo "<input type=\"text\" name=\"price\" value=\"Enter a price in kudos\" /><br />";
echo "<input type=\"hidden\" name=\"who\" value=\"" . $_COOKIE['login'] . "\" />";
echo "<input type=\"hidden\" name=\"bought\" value=\"no\" />";
echo "<input type=\"submit\" />";
echo "</form>";
}
elseif ($market_admin == yes AND isset($_COOKIE['admin'])) {
echo "<h3>Sell an item</h3>";
echo "<p>Tax is currently 3% payable to " . $taxman . "</p>";
echo "<form action=\"sell.php\" method=\"post\">";
echo "<input type=\"text\" name=\"item\" value=\"Item Title\" /><br />";
echo "<input type=\"text\" name=\"content\" value=\"Actual Item\" /><br />";
echo "<input type=\"text\" name=\"price\" value=\"Enter a price in kudos\" /><br />";
echo "<input type=\"hidden\" name=\"who\" value=\"" . $_COOKIE['login'] . "\" />";
echo "<input type=\"hidden\" name=\"bought\" value=\"no\" />";
echo "<input type=\"submit\" />";
echo "</form>";
}

elseif ($market_super == yes AND isset($_COOKIE['super'])) {
echo "<h3>Sell an item</h3>";
echo "<p>Tax is currently 3% payable to " . $taxman . "</p>";
echo "<form action=\"sell.php\" method=\"post\">";
echo "<input type=\"text\" name=\"item\" value=\"Item Title\" /><br />";
echo "<input type=\"text\" name=\"content\" value=\"Actual Item\" /><br />";
echo "<input type=\"text\" name=\"price\" value=\"Enter a price in kudos\" /><br />";
echo "<input type=\"hidden\" name=\"who\" value=\"" . $_COOKIE['login'] . "\" />";
echo "<input type=\"hidden\" name=\"bought\" value=\"no\" />";
echo "<input type=\"submit\" />";
echo "</form>";
}

else {
}
if (isset($_GET['delete'])) {
$id = $_GET['delete'];
mysql_query("DELETE FROM myblog_market WHERE item='$id'") 
or die(mysql_error());
}
if ($mode == buy) {
$item = $_GET['item'];
$user = $_COOKIE['login'];
$taxcollector = file_get_contents("tax_man.txt");

$result = mysql_query("SELECT * FROM myblog_market WHERE item = '$item'") or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
$user2 = $row['who'];
$price = $row['price'];
}

if ($user2 == $_COOKIE['login']) {
echo "You cannot buy from yourself!";
exit;
}

$result = mysql_query("SELECT * FROM myblog_users WHERE user = '$user'") or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
$ekudos3 = $row['kudos'];
}

$result = mysql_query("SELECT * FROM myblog_users WHERE user = '$taxcollector'") or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
$ekudos4 = $row['kudos'];
}

$test = $ekudos3 - $price;

if ($test < 0) {
echo "You do Not Have Enough Kudos!";
exit;
}


$result = mysql_query("SELECT * FROM myblog_users WHERE user = '$user2'") or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
$ekudos2 = $row['kudos'];
}


$result = mysql_query("UPDATE myblog_market SET bought='$user' WHERE item = '$item'") 
or die(mysql_error());

$result = mysql_query("SELECT * FROM myblog_users WHERE user = '$user'") or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
$ekudos = $row['kudos'];
}
$newk = $ekudos - $price;

$newk2 = $ekudos2 + $price - $price/97;
if ($ekudos != $ekudos4) {
$newk4 = $ekudos4 + $price/97;
}
else {
$newk4 = $newk + $price/100;
}

$result = mysql_query("UPDATE myblog_users SET kudos='$newk' WHERE user = '$user'") 
or die(mysql_error()); 

$result = mysql_query("UPDATE myblog_users SET kudos='$newk2' WHERE user = '$user2'") 
or die(mysql_error()); 

$result = mysql_query("UPDATE myblog_users SET kudos='$newk4' WHERE user = '$taxcollector'") 
or die(mysql_error()); 

echo "<br />Your kudos was: " . $ekudos . ". It is now: " . $newk . ". The sellers kudos was: " . $ekudos2 . ". It is now: " . $newk2 . ". The taxmans kudos was: " . $ekudos4 . ". It is now: " .$newk4;
}


else {
echo "Click Prices to buy";
}

$purchased = $_COOKIE['login'];
echo "<h2>My Purchased Items</h2>";
$result = mysql_query("SELECT * FROM myblog_market WHERE bought = '$purchased' ORDER BY num DESC") or die(mysql_error());
echo "<p>";
while($row = mysql_fetch_array( $result )) {
echo $row['content'] . " <br />By:" . $row['who'] . "<br /><a href=\"market.php?delete=" . $row['item'] . "\">Delete</a><br /><br />";
}
echo "</p>";
}
?>
<h2>My Items For Sale</h2>
<?php 
$user = $_COOKIE['login'];
require("config/mysqlconnection.inc");

$result = mysql_query("SELECT * FROM myblog_market WHERE who = '$user' ORDER BY num DESC") or die(mysql_error());
echo "<table cellspacing=\"2\"><tr><td>Title</td><td>Content</td><td>Price</td><td>Status</td><td>Actions</td></tr>";
while($row = mysql_fetch_array( $result )) {
echo "<tr><td>" . $row['item'] . "</td><td>" . $row['content'] . "</td><td>" . $row['price'] . "</td><td>";

if ($row['bought'] == no) 
{
echo "For Sale";
}
else {
echo "Bought By:" . $row['bought'];
}

echo "</td><td><a href=\"market.php?delete=" . $row['item'] . "\">Delete</td></tr>";
}
echo "</table>";
?>
<?php
require("config/settings.inc");
if ($fundtransfers != yes) {
echo "
<h2>Kudos Transfer</h2>
<p>Here you can transfer kudos between accounts.</p>
<form action=\"market.php\" method=\"get\">
<p>To: <select name=\"to\">";

$result = mysql_query("SELECT * FROM myblog_users") or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
if ($row['user'] != $_COOKIE['login']) {
echo "<option value=\"" . $row['user'] . "\">" . $row['user'] . "</option>";
}
else {
}
}
echo "
</select>
<br />
Amount: <input type=\"text\" name=\"amount\" /><br />
Reason for transfer: <input type=\"text\" name=\"reason\" /><br />
<input type=\"submit\" /><br /><br />
</form>";
}
?>
<?php
require("config/mysqlconnection.inc");
if (isset($_GET['amount'])) {
$old = '-';
$new = '';
$_GET['amount'] = str_replace($old, $new, $_GET['amount']);

$user = $_COOKIE['login'];
$user2 = $_GET['to'];
$result = mysql_query("SELECT * FROM myblog_users WHERE user = '$user'") or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
$ekudos1 = $row['kudos'];
}

$result = mysql_query("SELECT * FROM myblog_users WHERE user = '$user2'") or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
$ekudos2 = $row['kudos'];
}

$newk1 = $ekudos1 - $_GET['amount'];
$newk2 = $ekudos2 + $_GET['amount'];

if ($newk1 < 0) {
echo "You do not have enough kudos!";
}
else {
$result = mysql_query("UPDATE myblog_users SET kudos='$newk1' WHERE user = '$user'") 
or die(mysql_error()); 
$result = mysql_query("UPDATE myblog_users SET kudos='$newk2' WHERE user = '$user2'") 
or die(mysql_error()); 


$file2 = $user2 . "_funds.txt";
$file1h = fopen($file2, 'a') or die("can't open file");
$amount = $_GET['amount'];
$fromwho = " transfered by:";
$person = $_COOKIE['login'];
$line = "<br />";


fwrite($file1h, $amount);
fwrite($file1h, $fromwho);
fwrite($file1h, $person);
fwrite($file1h, $line);
fclose($file1h);
echo "Funds transfer succesful!";
}
}
?>
</div>
<?php include("template/footer.php"); ?>
Return current item: MyBlog: PHP and MySQL Blog/CMS software