Forgot Password / Register
Site Statistics
Total Members: 520
Total Tutorials: 242
Newsest User: 8884244477
Todays Unique Hits: 133
0 Users 7 Guests Online

Advanced shop.

Run this in phpmyadmin
Code

CREATE TABLE `shop_items` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(225) NOT NULL,
`price` varchar(225) NOT NULL,
`image` varchar(225) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM;

-- --------------------------------------------------------

CREATE TABLE `user_shop_items` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(225) NOT NULL,
`image` varchar(225) NOT NULL,
`owner` varchar(225) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM;


Save this as shop.php
PHP Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
session_start
();
include 
"config.php";
if(
$logged[username])
{
switch(
$_GET[page])
{
default:
echo (
"Welcome to the shop.<br>");
$fetch mysql_query("SELECT * FROM `shop_items`");
$rows mysql_num_rows($fetch);
if (
$rows == 0)
{
echo (
"No shop items. Sorry.");
}
else
{
while (
$shop mysql_fetch_array($fetch))
{
echo (
"<img src='$shop[image]'> | $shop[name] | $shop[price] - <a href='?page=verify&id=$shop[id]'>Purchase</a><br>");
}
}
break;

case 
'verify':
$id strip_tags(htmlspecialchars($_GET[id]));
$fetch mysql_query("SELECT * FROM `shop_items` WHERE `id` = '$id'");
$shop mysql_fetch_array($fetch);
echo (
"Are you sure you want to purchase $shop[name]?<br> <a href='?page=purchase&id=$id'>Yes</a> - <a href='shop.php'>No</a>");
break;

case 
'purchase':
$id strip_tags(htmlspecialchars($_GET[id]));
$fetch mysql_query("SELECT * FROM `shop_items` WHERE `id` = '$id'");
$shop mysql_fetch_array($fetch);

if (
$logged[points] < $shop[price])
{
echo (
"You don't have enough points. <a href='shop.php'>Back</a>");
}
else
{
$fetch mysql_query("SELECT * FROM `members` WHERE `username` = '$username'");
$user mysql_fetch_array($fetch);

$points1 $logged[points]-$shop[price];

$update1 mysql_query("UPDATE `members` SET `points` = '$points1' WHERE `username` = '$logged[username]'");
$update2 mysql_query("INSERT INTO `user_shop_items` (`owner`, `image`, `name`) VALUES ('$logged[username]', '$shop[image]', '$shop[name]')");
echo (
"Thank you for purchasing $shop[name]. Please come again. <a href='shop.php'>Back</a>");
}
break;
}
}
?>

This code arrays all the records in the database and then lets you pick which one to buy.
Mysql to update the database inserting the item you want to buy into user_furni_shop_items.
Example.
Code

$update2 = mysql_query("INSERT INTO `user_shop_items` (`owner`, `image`, `name`) VALUES ('$logged[username]', '$shop[image]', '$shop[name]')");


And save this as shop_admin.php
PHP Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
session_start
();
include 
"config.php";
if(
$logged[username] && $logged[userlevel] == 6)
{
switch(
$_GET[page])
{
default:
echo (
"Welcome to the shop admin. <a href='?page=add'>Add a shop item</a><br><br>");
$fetch mysql_query("SELECT * FROM `shop_items`");
$rows mysql_num_rows($fetch);
if (
$rows == 0)
{
echo (
"No shop items. Sorry. <a href='?page=add'>Add a shop item</a>");
}
else
{
while (
$shop mysql_fetch_array($fetch))
{
echo (
"<img src='$shop[image]'> | $shop[name] | $shop[price] - <a href='?page=edit&id=$shop[id]'>Edit</a> // <a href='?page=verifydelete&id=$shop[id]'>Delete</a><br>");
}
}
break;

case 
'add':
echo (
"<form method='post' action='?page=additem'>
Name: <input type='text' name='name'><br>
Price: <input type='text' name='price'><br>
Image: <input type='text' name='image'><br>
<input type='submit' value='Add' name='submit'>
</form>
"
);
break;

case 
'additem':
$image strip_tags(htmlspecialchars($_POST[image]));
$name strip_tags(htmlspecialchars($_POST[name]));
$price strip_tags(htmlspecialchars($_POST[price]));
$add mysql_query("INSERT INTO `shop_items` (`price`, `image`, `name`) VALUES ('$price', '$image', '$name')");
echo (
"$name has been added to the shop. thank you. <a href='shop_admin.php'>Back</a>");
break;

case 
'edit':
$id strip_tags(htmlspecialchars($_GET[id]));
$fetch mysql_query("SELECT * FROM `shop_items` WHERE `id` = '$id'");
$shop mysql_fetch_array($fetch);
echo (
"<form method='post' action='?page=edititem&id=$id'>
Name: <input type='text' value='
$shop[name]' name='name'><br>
Price: <input type='text' value='
$shop[price]' name='price'><br>
Image: <input type='text' value='
$shop[image]' name='image'><br>
<input type='submit' value='Edit' name='submit'>
</form>
"
);
break;

case 
'edititem':
$id strip_tags(htmlspecialchars($_GET[id]));
$image strip_tags(htmlspecialchars($_POST[image]));
$name strip_tags(htmlspecialchars($_POST[name]));
$price strip_tags(htmlspecialchars($_POST[price]));
$edit mysql_query("UPDATE `shop_items` SET `image` = '$image', `name` = '$name', `price` = '$price' WHERE `id` = '$id'");
echo (
"$shop[name] has been edited thank you. <a href='shop_admin.php'>Back</a>");
break;

case 
'verifydelete':
$id strip_tags(htmlspecialchars($_GET[id]));
$fetch mysql_query("SELECT * FROM `shop_items` WHERE `id` = '$id'");
$shop mysql_fetch_array($fetch);
echo (
"Are you sure you want to delete $shop[name]?<br> <a href='?page=delete&id=$id'>Yes</a> - <a href='shop.php'>No</a>");
break;

case 
'delete':
$id strip_tags(htmlspecialchars($_GET[id]));
$delete mysql_query("DELETE FROM `shop_items` WHERE `id` = '$id'");
echo (
"$shop[name] has been deleted thank you. <a href='shop_admin.php'>Back</a>");
break;
}
}
?>

This code lets the admin Add, Edit and Delete the shop items.
Add:
Code

$fetch = mysql_query("INSERT INTO `shop_items` (`price`, `image`, `name`) VALUES ('$price', '$image', '$name')");


Edit:
Code

$edit = mysql_query("UPDATE `shop_items` SET `image` = '$image', `name` = '$name', `price` = '$price' WHERE `id` = '$id'");


And Delete:
Code

$delete = mysql_query("DELETE FROM `shop_items` WHERE `id` = '$id'");


Thats it have fun.
And go green. Live earth how mad is it going to be. :)
SkillMaster
Views:
3873
Rating:
Posted on Thursday 12th February 2009 at 10:28 PM
Adam981
Adam981
lol, alright
Posted on Wednesday 11th February 2009 at 08:22 PM
jambomb
jambomb
sorted aaages ago lol
Posted on Wednesday 11th February 2009 at 03:58 PM
Adam981
Adam981
@jam just like many things on tutorials, 99% of the time if u get a blank page its because you havnt added anything for it to dispay yet.
Posted on Saturday 26th April 2008 at 10:48 PM
jambomb
jambomb
Hey!, when i try and open up shop.php i get a blank page ?
Posted on Monday 21st April 2008 at 09:25 AM
test
test
such as cause i realy want this for my site
Posted on Sunday 20th April 2008 at 05:19 PM
new2old
new2old
Erm.. i guess so, would need a lot of editing though
Posted on Thursday 10th April 2008 at 10:37 PM
test
test
can u make this instead of points to actual money and transfer to paypal
Posted on Monday 24th March 2008 at 01:05 PM
Dalez
Dalez
Yeah can someone make an inventory.
Posted on Friday 1st February 2008 at 01:07 AM
MrArmstrong
MrArmstrong
When is somone going to make an inventory cause i despirtly want one
Posted on Saturday 19th January 2008 at 06:54 PM
MrArmstrong
MrArmstrong
Can somone make an inventory for this script?