Advertisement
giraffeinatutu

cart 8.22 11.30p

Aug 22nd, 2014
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.44 KB | None | 0 0
  1. <?php
  2. session_start();
  3.     $con =  mysqli_connect("localhost", "cairil_php", "phpsql");
  4.  
  5.         // Check if there were any issues connecting to the database
  6.         if ( mysqli_connect_errno() != 0)
  7.         {
  8.            die("An error occurred trying to establish a connection: " .mysqli_connect_error());
  9.         }
  10.  
  11.         // Select the  database to work with.
  12.         $db  =  mysqli_select_db ($con , "cairil_php");
  13.  
  14. ?>
  15. <html>
  16. <head>
  17. <?php
  18.  
  19.     $pid = $_GET["id"];  //the product id from the URL
  20.     $action = $_GET["action"]; //the action from the URL
  21.  
  22.  
  23.     switch($action) {   //decide what to do
  24.    
  25.         case "add":
  26.             $_SESSION['cart'][$pid]++;
  27.         break;
  28.        
  29.         case "remove":
  30.             $_SESSION['cart'][$pid]--;
  31.             if($_SESSION['cart'][$pid] == 0) unset($_SESSION['cart'][$pid]);
  32.         break;
  33.        
  34.         case "empty":
  35.             unset($_SESSION['cart']);
  36.         break;
  37.     }
  38.  
  39. ?>
  40. </head>
  41. <title>CR_cart.php</title>
  42. <body>
  43. <center>
  44. <h1>Welcome <?php echo(($_SESSION['fname']) . " " . ($_SESSION['lname'])) ?></h1>
  45. <br>
  46. <h3>Product List</h3>
  47. <br><br>
  48.  
  49. <?php  
  50.  
  51.     if( isset( $_SESSION['cart'])) {
  52.                            
  53.         echo "<table border=\"2\" align=\"center\">";
  54.         echo "<tbody>";
  55.         echo "<tr bgcolor=\"#FE5970\">";
  56.         echo "<th align=\"center\">Product ID</th>";
  57.         echo "<th align=\"center\">Product Name</th>";
  58.         echo "<th align=\"center\">Quantity</th>";
  59.         echo "<th align=\"center\">Remove</th></tr>";
  60.        
  61.             //iterate through the cart, the $product_id is the key and $quantity is the value
  62.             foreach($_SESSION['cart'] as $pid => $qty) {   
  63.                
  64.                 echo $pid;
  65.                 echo $qty;
  66.                 echo ("<br>line 66<br>");
  67.                
  68.                 $sql = "SELECT 'ProductID', 'ProductName' FROM 'Product' WHERE 'ProductID' = $pid";
  69.                
  70.                 echo $sql;
  71.                 echo ("<br>line 71<br>");
  72.                    
  73.                 if ($result = mysqli_query($con, $sql))
  74.                 {
  75.                
  76.                 // Return the number of rows in result set
  77.                 $rowcount=mysqli_num_rows($result);
  78.                
  79.                 echo $rowcount;
  80.                 if( $rowcount > 0) {
  81.                
  82.                     list($pid, $pname) = mysql_fetch_row($result); 
  83.                
  84.                     echo("<tr><td>");
  85.                     echo $pid;
  86.                     echo("</td><td>");
  87.                     echo $name;
  88.                     echo("</td><td>"); 
  89.                     echo $qty;
  90.                     echo("</td><td>"); 
  91.                     echo "<a href=\"$_SERVER[PHP_SELF]?action=remove&id=$pid\"><img src=\"/images/b-remove.png\" alt=\"Remove\"></a>";
  92.                     echo("</td></tr>");
  93.                 }
  94.                 }
  95.                 else
  96.                 {
  97.                 echo mysql_error();
  98.                 }
  99.                 }
  100.            
  101.            
  102.         echo "</table>";
  103.        
  104.         echo "<br><br><table>";
  105.         echo "<tr><td colspan=\"4\" align=\"right\"><a href=\"$_SERVER[PHP_SELF]?action=empty\"><img src=\"/images/b-empty.png\" alt=\"Cart\" onclick=\"return confirm('Are you sure?');\"></a></td>";
  106.         echo "</tr>";
  107.         echo "</table>";       
  108.        
  109.    
  110.     }else{
  111.         //otherwise tell the user they have no items in their cart
  112.         echo "Cart Empty.";
  113.        
  114.     }
  115.    
  116.  
  117. ?>
  118.  
  119. <br /><br />
  120. <table>
  121. <tr>
  122. <td><a href=CR_products.php><img title"view products" src="/images/b-products.png" alt="View Product" /></a>
  123.     <a href=CR_cart.php><img title"view cart" src="/images/b-shopping.png" alt="View Cart" /></a>
  124.     <a href=CR_order.php><img title"view order" src="/images/b-orders.png" alt="View Orders"/></a></td></tr>
  125.     <tr></tr>    <tr></tr>
  126.     <tr><td align="center"><a href=CR_welcome.php><img title="home" src="/images/home_button.png" alt="home" width="150" height="50"/></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=CR_logout.php><img title"logout" src="/images/exit_button.jpg" alt="Logout" /></a></td></tr>
  127. </table>    
  128. </center>
  129. </body>
  130. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement