Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- session_start();
- $con = mysqli_connect("localhost", "cairil_php", "phpsql");
- // Check if there were any issues connecting to the database
- if ( mysqli_connect_errno() != 0)
- {
- die("An error occurred trying to establish a connection: " .mysqli_connect_error());
- }
- // Select the database to work with.
- $db = mysqli_select_db ($con , "cairil_php");
- ?>
- <html>
- <head>
- <?php
- $pid = $_GET[id]; //the product id from the URL
- $action = $_GET[action]; //the action from the URL
- switch($action) { //decide what to do
- case "add":
- $_SESSION['cart'][$pid]++;
- break;
- case "remove":
- $_SESSION['cart'][$pid]--;
- if($_SESSION['cart'][$pid] == 0) unset($_SESSION['cart'][$pid]);
- break;
- case "empty":
- unset($_SESSION['cart']);
- break;
- }
- ?>
- </head>
- <title>CR_cart.php</title>
- <body>
- <center>
- <h1>Welcome <?php echo(($_SESSION['fname']) . " " . ($_SESSION['lname'])) ?></h1>
- <br>
- <h3>Product List</h3>
- <br><br>
- <?php
- if( isset( $_SESSION['cart'])) {
- echo "<table>"; //format the cart using a HTML table
- //iterate through the cart, the $product_id is the key and $quantity is the value
- foreach($_SESSION['cart'] as $pid => $qty) {
- $sql = ("SELECT `ProductID`, `ProductName` FROM 'Products' WHERE 'ProductID' = '$pid'");
- if ($result=mysqli_query($con,$sql))
- {
- // Return the number of rows in result set
- $rowcount=mysqli_num_rows($result);
- if( $rowcount > 0) {
- list($pid, $pname) = mysql_fetch_row($result);
- echo "<tr>";
- echo "<td align=\"center\">$name</td>";
- echo "<td align=\"center\">$qty <a href=\"$_SERVER[PHP_SELF]?action=remove&id=$pid\">X</a></td>";
- echo "</tr>";
- }}
- }
- echo "<tr>";
- echo "<td colspan=\"3\" 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>";
- echo "</tr>";
- echo "</table>";
- }else{
- //otherwise tell the user they have no items in their cart
- echo "Cart Empty.";
- }
- ?>
- <br /><br />
- <table>
- <tr>
- <td><a href=CR_products.php><img title"view products" src="/images/b-products.png" alt="View Product" /></a>
- <a href=CR_cart.php><img title"view cart" src="/images/b-shopping.png" alt="View Cart" /></a>
- <a href=CR_order.php><img title"view order" src="/images/b-orders.png" alt="View Orders"/></a></td></tr>
- <tr></tr> <tr></tr>
- <tr><td align="center"><a href=CR_welcome.php><img title="home" src="/images/home_button.png" alt="home" width="150" height="50"/></a> <a href=CR_logout.php><img title"logout" src="/images/exit_button.jpg" alt="Logout" /></a></td></tr>
- </table>
- </center>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement