php - Disable "Add to Cart" button if the user already purchased that item from the Database -
i have been working on issue past couple days , it's been driving me nuts, thought bring question @ hand here. please note still learning php :) practice project.
i creating mockup registration page users of registration page can signup, purchase items, , see others purchased item (this intentional). current issue want disable user purchasing item purchased disabling "add cart" button if item user exists in database already. basically, buyer can purchase 1 item database , when try purchase 1 going store, "add cart" button disabled them.
note: when made (about 2 months ago) using mysql learning mysqli started (very slowly) converting it. database file has both connections mysql , mysqli it, allowed me utilize both @ same time learned , went along.
<?php session_start(); // starts session carry variables include_once('this sql file goes. removed it'); //connects page database /* verifies user logged in, if not redirect them index.php */ if($loggedin != 1) { header("location: index.php"); } /* category feature. allows page sorted sort column */ if (isset($_post['searchcatbtn'])){ } include("header.php") /* includes header */ ?> <!doctype html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>shopping cart</title> <link href="style/style.css" rel="stylesheet" type="text/css"> </head> <body> <!-- creates products within div provided style sheet--> <div id="products-wrapper"> <h1>products</h1> <div class="products"> <?php //current url of page. cart_update.php redirects url $current_url = base64_encode($url="http://".$_server['http_host'].$_server['request_uri']); $qcategories = mysql_query("select distinct sort events order sort asc;");//get data users table sorts list alphabetically username $results = $mysqli->query("select * events order eventid");// sort = '$sort' $remaining = $mysqli->query("select * orders"); $obj_r = $remaining->fetch_object(); // obj_r means objects remaining $category = $obj->sort; if ($results) { //fetch results set object , output html while($obj = $results->fetch_object()) { echo '<div class="product">'; echo '<form method="post" action="cart_update.php">'; echo '<div class="product-thumb"><img src="images/'.$obj->image.'"></div>'; echo '<div class="product-content"><h4>'.$obj->name.'</h4>'; echo '<div class="product-desc">'.$obj->description.'</div>'; echo '<div class="product-info">'; echo 'price: '.$currency.$obj->price.' | '; echo 'items left: '.$obj->max.' | '; echo '<input type="hidden" style="width:20px;height:15px;" type="text" name="product_qty" value="1" size="3" />'; // old code display qty box: qty: <input style="width:20px;height:15px;" type="text" name="product_qty" value="1" size="3" />'; echo '<button class="add_to_cart">add cart</button>'; echo '</div></div>'; echo '<input type="hidden" name="product_code" value="'.$obj->name.'" />'; echo '<input type="hidden" name="type" value="add" />'; echo '<input type="hidden" name="return_url" value="'.$current_url.'" />'; echo '</form>'; echo '</div>'; } } ?> </div> <!-- creates cart within div provided style sheet--> <div class="shopping-cart"> <h2>your shopping cart</h2> <?php if(isset($_session["products"])) { $total = 0; //sets default vaule of total echo '<ol>'; foreach ($_session["products"] $cart_itm) /* calls session , references cart_itm. creates items in cart per item. if empty, shows empty cart*/ { echo '<li class="cart-itm">'; echo '<span class="remove-itm"><a href="cart_update.php?removep='.$cart_itm["code"].'&return_url='.$current_url.'">×</a></span>'; echo '<h3>'.$cart_itm["name"].'</h3>'; echo '<div class="p-code">product code : '.$cart_itm["code"].'</div>'; //safe delete if want echo '<div class="p-qty">qty : '.$cart_itm["qty"].'</div>'; echo '<div class="p-price">price :'.$currency.$cart_itm["price"].'</div>'; echo '</li>'; $subtotal = ($cart_itm["price"]*$cart_itm["qty"]); $total = ($total + $subtotal); } echo '</ol>'; echo '<span class="check-out-txt"><strong>total : '.$currency.$total.'</strong> <a href="view_cart.php">check-out!</a></span>'; /* total amount of cart */ echo '<span class="empty-cart"><a href="cart_update.php?emptycart=1&return_url='.$current_url.'">empty cart</a></span>'; } else { echo 'your cart empty'; } ?> <br><br> <!-- creates categories. allows page sorted category --> <h2>categories</h2> <form name="searchcat" action="store.php" method="post"> <div align="center"> <select name="mydropdown"> <?php while($row = mysql_fetch_array($qcategories))//loops through $query row row { echo '<option value="' . $row['sort'] . '">' . $row['sort'] . '</option>'; //prints categories looping though array } ?> <input type="submit" name="searchcatbtn" class="btn btn-success" value="submit edits" /> </form> </select> </div> </div> </body> </html>
Comments
Post a Comment