Build a Shopping Cart with Php Part-3”
Total Page:16
File Type:pdf, Size:1020Kb
18 JUNE, 2015 (https(:/h/twtpwsw(:/h/.tfttawpcsite:t/be/orp.oclukos.mc.go/omwo3/ggplerro.cugoprmasm//wum/30ep/rr)bo/g1r0a5m7m14e3rs7)7957438843854/105714377957438843854/posts) (http://www.w3programmers.com/) HOME (HTTP://WWW.W3PROGRAMMERS.COM/) CATEGORIES (HTTP://WWW.W3PROGRAMMERS.COM/#) POSTED ON 11 SEPTEMBER, 2012 (HTTP://WWW.W3PROGRAMMERS.COM/BUILD-A-SHOPPING-CART- WITH-PHP-PART-3/) BY MASUD ALAM Categories (HTTP://WWW.W3PROGRAMMERS.COM/AUTHOR/MASUD1985/) Android Application Build a shopping cart Development with php part-3 (http://www.w3program mers.com/category/andr oid-apps-development/) 0 Angular JS ADMINISTRATOR PAGES (http://www.w3program mers.com/category/angu After completing shopping cart part1 lar-js-2/) (www.w3programmers.com/build-a-shopping-cart-with-php-part- 1/)and part2 (http://www.w3programmers.com/build-a-shopping- CakePHP cart-with-php-part-2/), In this tutorial we’ll learn shopping cart (http://www.w3program administration, The administration side of the shopping cart is very mers.com/category/cake simple. The primary function for the admin is to view and confirm php/) completed orders. When an order has been confirmed, the administrator has successfully sent out the product. CODEIGNITER (http://www.w3program The first step is to provide an administrator login. Create a new file mers.com/category/code called adminlogin.phpand add the following code: igniter/) 1 <?php 2 Drupal 3 session_start(); (http://www.w3program 4 5 require("config.php"); mers.com/category/drup 6 al/) 7 if(isset($_SESSION['SESS_ADMINLOGGEDIN']) == TRUE) { 8 Facebook 9 header("Location: " . $config_basedir); 10 (http://www.w3program 11 } 12 mers.com/category/face 13 if($_POST['submit']) book1/) 14 15 { 16 FAT FREE FRAMEWORK 17 $loginsql = "SELECT * FROM admin WHERE username = '" . $_POST['userBox'] . "' AND (http://www.w3program password = '" . sha1($_POST['passBox']). "'"; mers.com/category/fat- 18 free-framework/) 19 $loginres = mysql_query($loginsql) or die(mysql_error()); 20 HTML and CSS 21 $numrows = mysql_num_rows($loginres); 22 (http://www.w3program 23 if($numrows == 1) mers.com/category/html 24 25 { -and-css/) 26 27 $loginrow = mysql_fetch_assoc($loginres); 28 JAVASCRIPT 29 session_register("SESS_ADMINLOGGEDIN"); 30 (http://www.w3program 31 $_SESSION['SESS_ADMINLOGGEDIN'] = 1; mers.com/category/java 32 33 header("Location: " . $config_basedir . script/) "adminorders.php"); 34 35 } Joomla 36 (http://www.w3program 37 else 38 mers.com/category/joo 39 { 40 mla/) 41 header("Location: " . $config_basedir . "adminlogin.php?error=1"); 42 JQUERY and AJAX with 43 } PHP 44 45 } (http://www.w3program 46 mers.com/category/jque 47 else 48 ry-and-ajax-with-php/) 49 { 50 51 require("header.php"); JSON, XML and Web 52 53 echo "<h1>Admin Login</h1>"; Services 54 (http://www.w3program 55 if(@$_GET['error'] == 1) { 56 mers.com/category/json- 57 echo "<strong>Incorrect username/password! xml-and-web-services/) </strong>"; 58 59 } Laravel 60 61 ?> (http://www.w3program 62 63 <p> mers.com/category/larav 64 el/) 65 <form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="POST"> 66 Magento 67 <table> 68 (http://www.w3program 69 <tr> mers.com/category/mag 70 71 <td>Username</td> ento/) 72 73 <td><input type="textbox" name="userBox"> 74 MySQL Tutorial 75 </tr> (http://www.w3program 76 77 <tr> mers.com/category/mys 78 79 <td>Password</td> ql-tutorial/) 80 81 <td><input type="password" name="passBox"> 82 PHP 83 </tr> (http://www.w3program 84 85 <tr> mers.com/category/php/ 86 ) 87 <td></td> 88 89 <td><input type="submit" name="submit" PHP & MySQL Basics value="Log in"> (http://www.w3progr 90 91 </tr> ammers.com/categor 92 93 </table> y/php/php-mysql- 94 basics/) 95 </form> 96 97 <?php PHP & MySQL 98 Projects 99 } 100 (http://www.w3progr 101 require("footer.php"); ammers.com/categor 102 103 ?> y/php/php-mysql- projects/) Much of this code should look familiar to you. When the admin has successfully logged in, the SESS_ADMINLOGGEDIN variable is PHP Arrays, Strings created. and Numbers (http://www.w3progr Logging Out the Administrator ammers.com/categor y/php/php-arrays- To log out the administrator, create a file called strings-and- adminlogout.phpand add the following code: numbers/) 1 <?php 2 PHP Date Time and 3 session_start(); RegEx 4 5 require("config.php"); (http://www.w3program 6 7 session_unregister("SESS_ADMINLOGGEDIN"); mers.com/category/php- 8 date-time-and-regex/) 9 header("Location: " . $config_basedir); 10 11 ?> PHP Design patterns (http://www.w3program As with the normal user logout, you unregister the variable—as opposed to destroying the entire session. This prevents against the mers.com/category/php- administrator being logged out completely when logged in as both design-patterns/) an admin and a user. PHP File, Mail, Session and Cookie Managing Completed Orders (http://www.w3program mers.com/category/php- The main administrator page shows the list of completed orders. file-mail-session-and- The purpose of this page is to enable an admin to see which orders need products mailed. The admin can then create the package and cookie/) confirm the order after it has been mailed. PHP Object Oriented This page is fairly straightforward; it simply outputs data from Programming some tables. The script has two primary states: either displaying (http://www.w3program orders or confirming them. The default page displays the orders. If mers.com/category/php- you pass the page func=conf GET variable and the order number, object-oriented- the order will be confirmed. programming/) Create a new file called adminorders.php and write following code: PHP PDO and MySQLi (http://www.w3program 1 <?php mers.com/category/php- 2 3 session_start(); pdo-and-mysqli/) 4 5 require("config.php"); 6 PHP Security and 7 require("functions.php"); 8 Exceptions 9 if(isset($_SESSION['SESS_ADMINLOGGEDIN']) (http://www.w3program == FALSE) { 10 mers.com/category/php- 11 header("Location: " . $config_basedir); security-and-exceptions/) 12 13 } 14 Python 15 if(isset($_GET['func']) == TRUE) { 16 (http://www.w3program 17 if($_GET['func'] != "conf") { 18 mers.com/category/pyth 19 header("Location: " . $config_basedir); on/) 20 21 } 22 SASS and LESS 23 $validid = pf_validate_number($_GET['id'],"redirect", (http://www.w3program $config_basedir); mers.com/category/sass 24 25 $funcsql = "UPDATE orders SET status = 10 -and-less/) WHERE id = " . $_GET['id']; 26 27 mysql_query($funcsql); Standard PHP Library 28 29 header("Location: " . $config_basedir . (SPL) "adminorders.php"); (http://www.w3program 30 31 } mers.com/category/stan 32 dard-php-library-spl/) 33 else { 34 35 require("header.php"); Symfony 36 37 echo "<h1>Outstanding orders</h1>"; 38 (http://www.w3program 39 $orderssql = "SELECT * FROM orders WHERE status = 2"; mers.com/category/symf 40 ony/) 41 $ordersres = mysql_query($orderssql); 42 43 $numrows = mysql_num_rows($ordersres); Twitter Bootstrap 44 45 if($numrows == 0) (http://www.w3program 46 47 { mers.com/category/twitt 48 er-bootstrap/) 49 echo "<strong>No orders</strong>"; 50 51 } Useful PHP Functions 52 53 else and Features 54 (http://www.w3program 55 { 56 mers.com/category/usef 57 echo "<table cellspacing=10>"; 58 ul-php-functions-and- 59 while($row = mysql_fetch_assoc($ordersres)) features/) 60 61 { 62 WordPress 63 echo "<tr>"; 64 (http://www.w3program 65 echo "<td>[<a href='adminorderdetails.php? mers.com/category/basi id=" . $row['id']. "'>View</a>]</td>"; 66 c-wordpress-tutorial/) 67 echo "<td>". date("D jS F Y g.iA", strtotime($row['date'])). "</td>"; 68 YII 69 echo "<td>"; 70 (http://www.w3program 71 if($row['registered'] == 1) mers.com/category/yii/) 72 73 { 74 Zend Framework 2.x 75 echo "Registered Customer"; 76 (http://www.w3program 77 } mers.com/category/zend 78 79 else -framework-2-x/) 80 81 { 82 83 echo "Non-Registered Customer"; 84 85 } Latest Posts 86 87 echo "</td>"; 88 Dive into Python 89 echo "<td>£" . sprintf('%.2f', (http://www.w3program 90 91 $row['total']) . "</td>"; mers.com/dive-into- 92 93 echo "<td>"; python/) 94 95 if($row['payment_type'] == 1) 96 Getting Started Python 97 { (http://www.w3program 98 99 echo "PayPal"; mers.com/getting- 100 started-python/) 101 } 102 SonataAdminBundle of 103 else 104 Symfony2 105 { 106 (http://www.w3program 107 echo "Cheque"; mers.com/sonataadminb 108 109 } undle-of-symfony2/) 110 111 echo "</td>"; 112 FOSUserBundle of 113 echo "<td><a href='adminorders.php? Symfony2 func=conf&id=" . $row['id']. "'>Confirm Payment</a></td>"; (http://www.w3program 114 115 echo "</tr>"; mers.com/fosuserbundle 116 -of-symfony2/) 117 } 118 119 echo "</table>"; Magento Extension 120 121 } Development Part-2 122 (http://www.w3program 123 } 124 mers.com/magento- 125 require("footer.php"); 126 extension-development- 127 ?> part-2/) Now below we explainthe code: Magento Extension 1 <?php Development Part-1 2 (http://www.w3program 3 session_start(); 4 mers.com/magento- 5 require("config.php"); 6 extension-development- 7 require("functions.php"); part-1/) 8 9 if(isset($_SESSION['SESS_ADMINLOGGEDIN']) == FALSE) { Magento Theme 10 11 header("Location: " . $config_basedir); Development From 12 Scratch Part-3 13 } (http://www.w3program After the usual introductory code, make a check to see if the func mers.com/magento- GET variable exists: theme-development- scratch-part-3/) 1 } 2 3 if(isset($_GET['func']) == TRUE) { Magento Theme 4 5 if($_GET['func'] != "conf") { Development From 6 Scratch Part-2 7 header("Location: " . $config_basedir); 8 (http://www.w3program 9 } 10 mers.com/magento- 11 $validid = theme-development- pf_validate_number($_GET['id'],"redirect", $config_basedir); from-scratch-part-2/) 12 13 $funcsql = "UPDATE orders SET status = 10 WHERE id = " . $_GET['id']; Magento Theme 14 Development From 15 mysql_query($funcsql); 16 Scratch Part-1 17 header("Location: " . $config_basedir . (http://www.w3program "adminorders.php"); 18 mers.com/magento- 19 } theme-development- If the func GET variable exists, the page redirects when the variable from-scratch-part-1/) is set to anything other than conf; this prevents against a SQL injection attack. Next, the id GET variable is validated. The order is User registration with finally confirmed by updating the orderstable and setting the status Symfony field to 10.