Advanced shop.


Run this in phpmyadmin
PHP Code
  1. CREATE TABLE `shop_items` (
  2. `id` int(11) NOT NULL auto_increment,
  3. `name` varchar(225) NOT NULL,
  4. `price` varchar(225) NOT NULL,
  5. `image` varchar(225) NOT NULL,
  6. PRIMARY KEY (`id`)
  7. ) ENGINE=MyISAM;
  8.  
  9. -- --------------------------------------------------------
  10.  
  11. CREATE TABLE `user_shop_items` (
  12. `id` int(11) NOT NULL auto_increment,
  13. `name` varchar(225) NOT NULL,
  14. `image` varchar(225) NOT NULL,
  15. `owner` varchar(225) NOT NULL,
  16. PRIMARY KEY (`id`)
  17. ) ENGINE=MyISAM;


Save this as shop.php
PHP Code
  1. <?php
  2. session_start();
  3. include "config.php";
  4. if($logged[username])
  5. {
  6. switch($_GET[page])
  7. {
  8. default:
  9. echo ("Welcome to the shop.<br>");
  10. $fetch = mysql_query("SELECT * FROM `shop_items`");
  11. $rows = mysql_num_rows($fetch);
  12. if ($rows == 0)
  13. {
  14. echo ("No shop items. Sorry.");
  15. }
  16. else
  17. {
  18. while ($shop = mysql_fetch_array($fetch))
  19. {
  20. echo ("<img src='$shop[image]'> | $shop[name] | $shop[price] - <a href='?page=verify&id=$shop[id]'>Purchase</a><br>");
  21. }
  22. }
  23. break;
  24.  
  25. case 'verify':
  26. $id = strip_tags(htmlspecialchars($_GET[id]));
  27. $fetch = mysql_query("SELECT * FROM `shop_items` WHERE `id` = '$id'");
  28. $shop = mysql_fetch_array($fetch);
  29. 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>");
  30. break;
  31.  
  32. case 'purchase':
  33. $id = strip_tags(htmlspecialchars($_GET[id]));
  34. $fetch = mysql_query("SELECT * FROM `shop_items` WHERE `id` = '$id'");
  35. $shop = mysql_fetch_array($fetch);
  36.  
  37. if ($logged[points] < $shop[price])
  38. {
  39. echo ("You don't have enough points. <a href='shop.php'>Back</a>");
  40. }
  41. else
  42. {
  43. $fetch = mysql_query("SELECT * FROM `members` WHERE `username` = '$username'");
  44. $user = mysql_fetch_array($fetch);
  45.  
  46. $points1 = $logged[points]-$shop[price];
  47.  
  48. $update1 = mysql_query("UPDATE `members` SET `points` = '$points1' WHERE `username` = '$logged[username]'");
  49. $update2 = mysql_query("INSERT INTO `user_shop_items` (`owner`, `image`, `name`) VALUES ('$logged[username]', '$shop[image]', '$shop[name]')");
  50. echo ("Thank you for purchasing $shop[name]. Please come again. <a href='shop.php'>Back</a>");
  51. }
  52. break;
  53. }
  54. }
  55. ?>

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.
PHP Code
  1. $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. <?php
  2. session_start();
  3. include "config.php";
  4. if($logged[username] && $logged[userlevel] == 6)
  5. {
  6. switch($_GET[page])
  7. {
  8. default:
  9. echo ("Welcome to the shop admin. <a href='?page=add'>Add a shop item</a><br><br>");
  10. $fetch = mysql_query("SELECT * FROM `shop_items`");
  11. $rows = mysql_num_rows($fetch);
  12. if ($rows == 0)
  13. {
  14. echo ("No shop items. Sorry. <a href='?page=add'>Add a shop item</a>");
  15. }
  16. else
  17. {
  18. while ($shop = mysql_fetch_array($fetch))
  19. {
  20. 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>");
  21. }
  22. }
  23. break;
  24.  
  25. case 'add':
  26. echo ("<form method='post' action='?page=additem'>
  27. Name: <input type='text' name='name'><br>
  28. Price: <input type='text' name='price'><br>
  29. Image: <input type='text' name='image'><br>
  30. <input type='submit' value='Add' name='submit'>
  31. </form>
  32. ");
  33. break;
  34.  
  35. case 'additem':
  36. $image = strip_tags(htmlspecialchars($_POST[image]));
  37. $name = strip_tags(htmlspecialchars($_POST[name]));
  38. $price = strip_tags(htmlspecialchars($_POST[price]));
  39. $add = mysql_query("INSERT INTO `shop_items` (`price`, `image`, `name`) VALUES ('$price', '$image', '$name')");
  40. echo ("$name has been added to the shop. thank you. <a href='shop_admin.php'>Back</a>");
  41. break;
  42.  
  43. case 'edit':
  44. $id = strip_tags(htmlspecialchars($_GET[id]));
  45. $fetch = mysql_query("SELECT * FROM `shop_items` WHERE `id` = '$id'");
  46. $shop = mysql_fetch_array($fetch);
  47. echo ("<form method='post' action='?page=edititem&id=$id'>
  48. Name: <input type='text' value='$shop[name]' name='name'><br>
  49. Price: <input type='text' value='$shop[price]' name='price'><br>
  50. Image: <input type='text' value='$shop[image]' name='image'><br>
  51. <input type='submit' value='Edit' name='submit'>
  52. </form>
  53. ");
  54. break;
  55.  
  56. case 'edititem':
  57. $id = strip_tags(htmlspecialchars($_GET[id]));
  58. $image = strip_tags(htmlspecialchars($_POST[image]));
  59. $name = strip_tags(htmlspecialchars($_POST[name]));
  60. $price = strip_tags(htmlspecialchars($_POST[price]));
  61. $edit = mysql_query("UPDATE `shop_items` SET `image` = '$image', `name` = '$name', `price` = '$price' WHERE `id` = '$id'");
  62. echo ("$shop[name] has been edited thank you. <a href='shop_admin.php'>Back</a>");
  63. break;
  64.  
  65. case 'verifydelete':
  66. $id = strip_tags(htmlspecialchars($_GET[id]));
  67. $fetch = mysql_query("SELECT * FROM `shop_items` WHERE `id` = '$id'");
  68. $shop = mysql_fetch_array($fetch);
  69. 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>");
  70. break;
  71.  
  72. case 'delete':
  73. $id = strip_tags(htmlspecialchars($_GET[id]));
  74. $delete = mysql_query("DELETE FROM `shop_items` WHERE `id` = '$id'");
  75. echo ("$shop[name] has been deleted thank you. <a href='shop_admin.php'>Back</a>");
  76. break;
  77. }
  78. }
  79. ?>

This code lets the admin Add, Edit and Delete the shop items.
Add:
PHP Code
  1. $fetch = mysql_query("INSERT INTO `shop_items` (`price`, `image`, `name`) VALUES ('$price', '$image', '$name')");


Edit:
PHP Code
  1. $edit = mysql_query("UPDATE `shop_items` SET `image` = '$image', `name` = '$name', `price` = '$price' WHERE `id` = '$id'");


And Delete:
PHP Code
  1. $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's Avatar
Views:
4,193
Rating:
Posted on Thursday 12th February 2009 at 10:28 PM
Adam981
Adam981's Avatar
lol, alright
Posted on Wednesday 11th February 2009 at 08:22 PM
jambomb
jambomb's Avatar
sorted aaages ago lol
Posted on Wednesday 11th February 2009 at 03:58 PM
Adam981
Adam981's Avatar
@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's Avatar
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's Avatar
such as cause i realy want this for my site
Posted on Sunday 20th April 2008 at 05:19 PM
new2old
new2old's Avatar
Erm.. i guess so, would need a lot of editing though
Posted on Thursday 10th April 2008 at 10:37 PM
test
test's Avatar
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's Avatar
Yeah can someone make an inventory.
Posted on Friday 1st February 2008 at 01:07 AM
MrArmstrong
MrArmstrong's Avatar
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's Avatar
Can somone make an inventory for this script?