Create your own Shop Version 1
Copy Right Demonic Shop v1. There will be about 1-5 versions made currently.
First run these queries
Things being created:
ACP:
Add/Delete Items
Shop:
Buy Items/View items
Part 2
Show Items in profile
Total Items
Send Items/Use them
Note::(The code has comments so read comments)
CODE
CREATE TABLE `inventury` (
`id` INT( 255 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`itemname` VARCHAR( 255 ) NOT NULL ,
`username` VARCHAR( 255 ) NOT NULL
) ENGINE = MYISAM ;
CREATE TABLE `items` (
`id` INT( 255 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`itemname` VARCHAR( 255 ) NOT NULL ,
`price` VARCHAR( 255 ) NOT NULL ,
`image` LONGTEXT NOT NULL
) ENGINE = MYISAM ;
ALTER TABLE `users` ADD `shoppoints` INT( 255 ) NOT NULL DEFAULT '0';
Edits you will make are:
Admin CP: to add items go to admin.php?additem=add and to delete go to admin.php?additem=delete
find(on line 4):
CODE
if($logged[username] && $logged[level] ==5)
{
add below:
CODE
$additems = $_GET[additem];
//starts to ourput our admin.php?additem page
if($additems == "add")
{
//define our forum values/varibles
$item = $_POST[item];
$image = $_POST[image];
$price = $_POST[price];
$addit = $_POST[addme];
$item = htmlspecialchars($item);
$image = htmlspecialchars($image);
$price = htmlspecialchars($price);
$addit = $addit;
//end varibles
if(isset($addit)){//if you press submit it will add the items to database
mysql_query("INSERT INTO items(itemname,price,image) VALUES('$item','$price','$image') ")
or die(mysql_error());
echo ("Thank you, you added the item : <b> $item </b> ");
}else{//else show the page form
echo ("
<form method='post'>
<input type='text' value='itemhere' name='item'>
<input type='text' value='image link here' name='image'>
<input type='text' value='item price here' name='price'>
<input type='submit' value='Add Item' name='addme' />
</form>
");
}
}
if($additems == "delete")
{
$deleteit = $_POST[del];//gets the submit button varible
$zitem = $_POST[itemz];//gets all forum id's
if(isset($deleteit)){//if submitted
mysql_query("DELETE FROM items WHERE id='$zitem' ") or die(mysql_error());
//then itll delete item
echo ("You successfully deleted the item: $zitem ");
}else{//else
$titems = mysql_query("SELECT * FROM items");//select all items from SQL
echo ("<form method='post'><select name='itemz'>");//out out it in a <select> input
while($showemz = mysql_fetch_array($titems)){
echo "<option value='$showemz[id]'>$showemz[itemname]</option>";//id values
}
echo ("</select><input type='submit' name='del' value='Delete Item'></form>");
//end select
}
}
buy.php
this file is used to buy users it shows a check form and your ok.
CODE
<?php
/*
*Include Config file
*/
//////////////////////////////////////////////////////////
include "config.php";
//////////////////////////////////////////////////////////
if($logged[username])
{//check if user is logged in
/*
*Start You varibles
*/
$item = $_GET[item];
$buy = $_POST[buy];
$cancel = $_POST[cancel];
/*
*lets be safe and make the items varible safe so we wont be
*getting hacked in any way what this will do is
*it will prevent sql injection and best way to be safe in all means
*/
$items = mysql_real_escape_string($items);
//moreinformation can be found here:
//http://us2.php.net/mysql_real_escape_string
/*
*End Your varibles
*/
/*
*if we clicked cancel bring us to the index
*/
if(isset($cancel)){
echo ('<META HTTP-EQUIV="Refresh" CONTENT="3; URL=http://www.link.com">');
}
/*
*end cancel check
*/
if(isset($buy)){//if the but button was clicked then...
$user = mysql_query("SELECT * FROM users WHERE username='$logged[username]' ")
or die(mysql_error());
//get users info from database
$suser = mysql_fetch_array($user);
//outputs our information into a varible witch is now reconized as a array
/*
*above we just selected users information so we can subtract money from there shoppoints
*/
/*
*select item information from database
*/
$itemi = mysql_query("SELECT * FROM items WHERE itemname='$items' ")
or die(mysql_error());
//get items info from database
$sitemi = mysql_fetch_array($itemi);
//outputs our information into a varible witch is now reconized as a array
if($suser[shoppoints] == 0 || $suser[shoppoints] < $sitemi[price]){//money check if
/*
*Ok above is telling us if our user has no points OR if the user does have any points do somthen
*well that somthen is going to echo an error below
*/
echo "Sorry, you either have no money or you dont have enough money to by this item.";
//error message
}//end money check if
else{//else finish the script to execute.
$usedmoney = $suser[shoppoints]-$sitemi[price];
//this line is simply easy (eg...chicken cost 20 points you have 50
//so it would be 50-20 and it updates your total and your account would now be 30 points)
//if everything goes well update and give us a message
echo "Thank you for purchasing $item from our shop";
mysql_query("UPDATE users SET shoppoints='$usedmoney' WHERE username='$logged[username]' ")
or die(mysql_error());//updates your total points from the above comment with the example
mysql_query("INSERT INTO `inventury` (itemname,username) VALUES('$item','$logged[username]') ")
or die(mysql_error());//insert into the inventury so we can view your items later
}//end else
}//end if
else{//if not submitted show buy buttons
echo ("
<form method='post'>
<input type='submit' name='cancel' value='Cancel Purchase' />
<input type='submit' name='buy' value='Continue Purchase' />
</form>
");
}
}else{//if user isnt logged in them tell them to log in.
echo "Please login view <a href='login.php'>here</a>";
}//end else
?>
Shop.php
this code will show the list of items on a page and will show a simple HTML form (im not great in HTML so bare with me :P)
CODE
<?php
//Copy Right Demonic of TechTuts. :O
include "config.php";
?>
<html>
<head>
<style type='text/css'>
.shopbody{
border: 1px solid black;
}
.shopbody2{
border: 1px solid black;
border-top:0px;
}
</style>
<title>Shop</title>
</head>
<body>
<!--Start Shop HTML-->
<table class="shopbody" colspan="3" width="100%">
<tr>
<td style="border-right: 1px solid #000000" width="25%">
Shop Item
</td>
<td style="border-right: 1px solid #000000" width="25%">
Item Price
</td>
<td style="border-right: 1px solid #000000" width="25%">
Buy Item
</td>
<td width="25%">
Image
</td>
</tr>
</table>
<?php
//If user is logged in show the item and its price and the buy button.
if($logged){//if statement 1
$shop = mysql_query("SELECT * FROM `items`");
//select all items from database
while($items = mysql_fetch_array($shop)){
//show all items using a while statement (whilestatement1)
echo ('
<table class="shopbody2" colspan="3" width="100%">
<tr>
<td style="border-right: 1px solid #000000" width="25%">
'.$items[itemname].'
</td>
<td style="border-right: 1px solid #000000" width="25%">
'.$items[price].'
</td>
<td style="border-right: 1px solid #000000" width="25%">
<a href="buy.php?item='.$items[itemname].'">Buy '.$items[itemname].'</a>
</td>
<td width="25%">
'.$items[image].'
</td>
</tr>
</table>
');
}//end Whilestatement1
}//end if statement1
else{//else1
echo "Sorry, guest cant use shop";
//show error message if your not logged in
}//end else1
?>
<!--End Shop HTML-->
</body>
</html>
Sunday May 27, 2007 | http://www.totaldream.org/article/20-create_your_own_shop_version_1.html