Jump to content
Larry Ullman's Book Forums

kerry

Members
  • Posts

    34
  • Joined

  • Last visited

kerry's Achievements

Newbie

Newbie (1/14)

0

Reputation

  1. This is the book that I have used. In fact I have used both of Larry's book and the code is from the book. It is just that I want to edit Larry's code to add this drop down box. If I set the quantity as one as in Larry's code it comes into the cart ok. It is just using the drop down value that will not come into the cart.
  2. This is the cart script <?php session_start(); $_SESSION['cart']; $page_title = 'Add to cart'; if (isset ($_GET['pid']) && is_numeric($_GET['pid']) ) { $pid = (int) $_GET['pid']; if (isset($_SESSION['cart'][$pid])) { $_SESSION['cart'][$pid]['quantity']++; }else{ //new product require_once ('mysqli_connect.php'); $q="SELECT price, product_name, quantity_stock FROM product WHERE product_id=$pid"; $r=mysqli_query ($dbc, $q); if (mysqli_num_rows($r) == 1) { list($price, $product_name) = mysqli_fetch_array ($r, MYSQLI_NUM); $_SESSION['cart'][$pid] = array ('quantity' => $quantity_stock, 'price' => $price, 'product_name' => $product_name); }else{ echo '<div align="center">Your basket has been updated</div>'; } } }else{ //no print ID echo '<div align="center"> Your basket has been updated</div>'; } if (isset($_POST['submitted'])) { foreach ($_POST['qty'] as $k => $v) { $pid = (int) $k; $qty = (int) $v; if ($qty ==0) { // Delete. unset ($_SESSION['cart'][$pid]); } elseif ($qty >0 ) { // Change quantity. $_SESSION['cart'][$pid]['quantity'] = $qty; } } // End of FOREACH. } // End of SUBMITTED IF.
  3. It is the quantity set by the drop down list (quantity_stock) I am (i think) accessing it in the cart script by $_SESSION['cart'][$pid] = array ('quantity' => $quantity_stock); But think that is where I am having the problem. The quantity is unlimited so it does not matter about retrieveing it from the database
  4. oops, yes had forgot the closing select. Here is the code from my cart page. No quantity is in the qty box $q="SELECT price, product_name, quantity_stock FROM product WHERE product_id=$pid"; $r=mysqli_query ($dbc, $q); if (mysqli_num_rows($r) == 1) { list($price, $product_name) = mysqli_fetch_array ($r, MYSQLI_NUM); $_SESSION['cart'][$pid] = array ('quantity' => $quantity_stock, 'price' => $price, 'product_name' => $product_name); }else{ echo '<div align="center">Your basket has been updated</div>'; }
  5. I am wanting to add to the cart (on a different page) through a drop down list the quantity of 4 or 8. It displays 4 or 8, but does not display the addcart image to bring these quantities to the cart page. <?php $row = FALSE; // Assume nothing! if (isset($_GET['pid']) && is_numeric($_GET['pid']) ) { // Make sure there's a print ID! $pid = (int) $_GET['pid']; // Get the print info: require_once ('mysqli_connect.php'); $q="SELECT product_name, price, print_id, product_category, description, image_name, product_id, quantity_stock FROM product WHERE product_id= $pid"; $r = mysqli_query ($dbc, $q); if (mysqli_num_rows($r) == 1) { // Good to go! $row = mysqli_fetch_array ($r, MYSQLI_ASSOC); echo '<table border="0">'; echo "<tr><td><img id='image1' border='0' img src=\"/orders/show_image.php?image=$pid&name=" . urlencode($row['image_name']) . "\" $image[3] alt=\"{$row['product_name']}\" width=\"400\" height=\"168\"/></td> <td><p class=head_blue><b>{$row['product_name']}</b> <p class=desc><b>{$row['description']}<br/></p> <p class=desc><b>£{$row['price']}</b></p> <select name='quantity_stock'> <option value='4'>4</option> <option value='8'>8</option> <a href=\"basket.php?pid=$pid\"><img src='images/addcart.gif' width='83' height='23' border='0'/></a></td></tr></table>"; if ($row['quantity_stock'] >=1) { } else { echo '<p> NOT IN STOCK</p></td></tr></table>';}}}?>
  6. Hi Larry I have tried this, but it does not matter how many items I put in my basket it only says that there is 1 item in my cart <?php session_start(); function writeShoppingCart() { $items = 0; foreach ($_SESSION['cart'] as $item) { $items += $item['quantity']; } return '<p>You have <a href="basket.php"> '.count($items).' in your shopping cart</a></p>'; } echo writeShoppingCart();?>
  7. I have created the shopping cart as in the book: I am tring to show on each page how many items are in the cart. But, it only shows 1 item even if there are 5 items in the cart. The code I am using is: <?php session_start(); function writeShoppingCart() { $cart = $_SESSION['cart']; if (!$cart) { return '<p>You have no items in your shopping cart</p>'; } else { $items = explode(',', $cart); return '<p>You have <a href="basket.php"> '.count($items).' in your shopping cart</a></p>'; } } echo writeShoppingCart();?> I guess it is not bringing an array from the cart, but dont know how to show this in the above. A snippet from my cart is: session_start(); $_SESSION['cart']; $page_title = 'Add to cart'; if (isset ($_GET['pid']) && is_numeric($_GET['pid']) ) { $pid = (int) $_GET['pid']; if (isset($_SESSION['cart'][$pid])) { $_SESSION['cart'][$pid]['quantity']++; }else{ //new product require_once ('../mysqli_connect.php'); $q="SELECT price, product_name FROM product WHERE product_id=$pid"; $r=mysqli_query ($dbc, $q); if (mysqli_num_rows($r) == 1) { list($price, $product_name) = mysqli_fetch_array ($r, MYSQLI_NUM); $_SESSION['cart'][$pid] = array ('quantity' => 1, 'price' => $price, 'product_name' => $product_name); }else{ echo '<div align="center">Your basket has been updated</div>'; } } }else{ echo '<div align="center"> Your basket has been updated</div>'; } if (isset($_POST['submitted'])) { foreach ($_POST['qty'] as $k => $v) { $pid = (int) $k; $qty = (int) $v; if ($qty ==0) { unset ($_SESSION['cart'][$pid]); } elseif ($qty >0 ) { $_SESSION['cart'][$pid]['quantity'] = $qty; } } } if (!empty($_SESSION['cart'])) { require_once ('../mysqli_connect.php'); $q="SELECT product_id, description, price, product_name FROM product WHERE product_id IN ("; foreach ($_SESSION['cart'] as $pid => $value) {
  8. I took the code from your article (peachpit) and then used the book with the form. All works except for getting existing variables. It starts from view.php which has the product on. The product_id is sent to the next page reviews.php through the link http://www.xx/reviews.php?pid=101 And, yes this page is sent back to itself once added to the database. I therefore presume: $product_id = (int) $_GET['product_id']; // is correct as I am getting the product_id It does not matter if I put value="<?php echo $_GET['product_id']; ?>"/> or value="<?php echo $_POST['product_id']; ?>"/> Neither adds to the database.
  9. I have had a look at your book and guess this is what you mean. I must have done something wrong as it still doesn't put the product_id in the database: $product_id = (int) $_GET['product_id']; $rating = mysqli_real_escape_string($dbc, $_POST['rating']); $review = mysqli_real_escape_string($dbc, $_POST['review']); $q='INSERT INTO review (product_id, rating, review) VALUES (?,?,?)'; $stmt=mysqli_prepare($dbc,$q); mysqli_stmt_bind_param($stmt, 'iss', $product_id, $rating, $review); mysqli_stmt_execute($stmt); if (mysqli_stmt_affected_rows($stmt)==1){ echo'<p>Your review has been added </p>'; }else{ echo'<p>error</p>'; } } ?> <form action="reviews.php" method="post" accept-charset="utf-8"> <fieldset><legend>Review this product</legend> <p><label for="rating">Rating</label><input type="radio" name="rating" value="5" />5 <input type="radio" name="rating" value="4" />4 <input type="radio" name="rating" value="3" />3 <input type="radio" name="rating" value="2" />2 <input type="radio" name="rating" value="1" />1</p> <p><Label for="review">Review</Label><textarea name="review" rows="8" cols="40" value="<?php echo $_POST['review'];?>"/></textarea></p> <p><input type="submit" value="submit review" ></p> <input type="hidden" name="product_id" id="product_id" value="<?php echo $_GET['product_id']; ?>"/> </fieldset> </form>
  10. Hi Larry I have already tried that, but that still did not put the product_id in the database.
  11. Hi. Thanks for the reply. I have solved the problem, but I cannot get the product_id to insert into the database: The $pid (product_id) is carried from the page required. http://www.xx/reviews.php?pid=101 but whatever I try it does not put the product_id in the review database. <?php require_once ('../mysqli_connect.php'); if ($_SERVER['REQUEST_METHOD'] == 'POST') { $pid = $_GET ['pid']; $product_id = mysqli_real_escape_string($dbc, $_POST['product_id=$pid']); $review = mysqli_real_escape_string($dbc, $_POST['review']); $q='INSERT INTO review (product_id, review) VALUES (?,?)'; $stmt=mysqli_prepare($dbc,$q); mysqli_stmt_bind_param($stmt, 'is', $product_id, $review); mysqli_stmt_execute($stmt); if (mysqli_stmt_affected_rows($stmt)==1){ echo'<p>Your review has been added </p>'; }else{ echo'<p>error</p>'; } } ?> <form action="reviews.php" method="post" accept-charset="utf-8"> <fieldset><legend>Review this product</legend> > <p><Label for="review">Review</Label><textarea name="review" rows="8" cols="40" value="<?php echo $_POST['review'];?>"/></textarea></p> <p><input type="submit" value="submit review" ></p> <input type="hidden" name="product_id" id="product_id" value="<?php echo $_POST['product_id'];?>"/> </fieldset> </form>
  12. I am editing the script in the book (17.1 to add a print) to create a product review. I have created a new table (review) But all I get is an error message, so I am obviously missing something. Any advice on creating a producct review on the site or advice on where I have gone wrong would be appreciated <?php require_once ('../mysqli_connect.php'); if (isset($_POST['submitted'])) { $errors = array(); if (!empty($_POST['product_id'])) { $pi = trim($_POST['product_id']); } else { $errors[] = 'Please enter the product id'; } $ci = (!empty($_POST['reviewer_name'])) ? trim($_POST['reviewer_name']) : NULL; $re = (!empty($_POST['review'])) ? trim($_POST['review']) : NULL; $q="INSERT INTO review (product_id, reviewer_name, review)VALUES(?, ?, ?)"; $stmt = mysqli_prepare($dbc, $q); mysqli_stmt_bind_param($stmt, 'sss', $pi, $ci, $re); mysqli_stmt_execute($stmt); // Check the results... if (mysqli_stmt_affected_rows($stmt) == 1) { echo '<p>Your review has been added</p>'; $_POST = array(); } else { // Error! echo '<p style="font-weight: bold; color: #C00"> error.</p>'; } mysqli_stmt_close($stmt); } ?> <p> </p> <form enctype="multipart/form-data" action="addreview.php" method="post"> <fieldset><legend>Add a Review</legend> <p><b>Name:</b> <input type="text" name="product_name" size="30" maxlength="60" value="<?php if (isset($_POST['reviewer_name'])) echo htmlspecialchars($_POST['reviewer_name']); ?>" /> You can use a nickname</p> <p><b>Review:</b> <textarea name="description" cols="40" rows="5"><?php if (isset($_POST['review'])) echo $_POST['review']; ?></textarea> </p> </fieldset> <div align="center"><input type="submit" name="submit" value="Submit" /></div> <input type="hidden" name="submitted" value="TRUE" /> </form> MySQL client version: 5.0.77
  13. Hi Thanks for the reply. Its a good idea to indicate "no stock", "Out of stock - back soon" Any ideas on how I could add to the code above to show no stock when 0?
×
×
  • Create New...