Snatch Posted August 16, 2007 Share Posted August 16, 2007 Good evening gents! I'm using the following code to count the number of products a user has in their basket. As it is, it works but only counts each new product added to the basket. It doesn't recognise if a user changes the quantity of a product. Eg. You have 2 products in your basket 1 x Mars 1 x Mars You have 1 product in your basket 2 x Mars Basket.php You currently have <?php $sessid = session_id(); //display number of products in cart $query = "SELECT * from baskettemp WHERE sess = '$sessid'"; $results = mysql_query($query) or die (mysql_query()); $rows = mysql_num_rows($results); if ( $rows < "1" ) { echo "no products in your basket"; } elseif( $rows == "1") { echo "$rows product in your basket"; } else { echo "$rows products in your basket"; } ?> Baskettemp db contains the fields: hidden sess prodnum and quan. I tried creating a new variable called $amountrows = $rows + quan; but this doesn't work. Any thoughts/suggestions?? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/65279-counting/ Share on other sites More sharing options...
mdnghtblue Posted August 16, 2007 Share Posted August 16, 2007 What does your UPDATE query look like? Quote Link to comment https://forums.phpfreaks.com/topic/65279-counting/#findComment-325999 Share on other sites More sharing options...
Snatch Posted August 16, 2007 Author Share Posted August 16, 2007 add.php for adding a product to the basket <?php session_id(); session_start(); include "connection.php"; $qty =$_POST['qty']; $prodnum = $_POST['prodnum']; $sess =session_id(); $query = "INSERT INTO baskettemp (sess, quan, prodnum) VALUES ('$sess','$qty','$prodnum')"; $results = mysql_query($query) or die(mysql_error()); include("basket.php"); ?> change.php for changing the quantity of a product in the basket <?php session_id(); session_start(); include "connection.php"; $qty =$_POST['qty']; $hidden = $_POST['hidden']; $sess = $_POST['sessid']; $query = "UPDATE baskettemp SET quan = '$qty' WHERE hidden = '$hidden'"; $results = mysql_query($query) or die(mysql_error()); include("basket.php"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/65279-counting/#findComment-326003 Share on other sites More sharing options...
Snatch Posted August 16, 2007 Author Share Posted August 16, 2007 Sorry to bump this but, just wondering if any one has any thoughts before I turn in for the night? Quote Link to comment https://forums.phpfreaks.com/topic/65279-counting/#findComment-326195 Share on other sites More sharing options...
lemmin Posted August 16, 2007 Share Posted August 16, 2007 Since the quantity modifies the total number you need to use that field when calculating it. You are only finding the total number of unique products. Maybe you should do something like: $query = "SELECT SUM(quan) as total from baskettemp WHERE sess = '$sessid'"; $row = mysql_fetch_array($query); echo "You have " . $row['total'] . " products in your basket."; Quote Link to comment https://forums.phpfreaks.com/topic/65279-counting/#findComment-326212 Share on other sites More sharing options...
MadTechie Posted August 16, 2007 Share Posted August 16, 2007 have you tried $query = "SELECT *, sum(quan) as Qty from baskettemp WHERE sess = '$sessid'"; EDIT: lemmin is a more complete reply! Quote Link to comment https://forums.phpfreaks.com/topic/65279-counting/#findComment-326213 Share on other sites More sharing options...
Snatch Posted August 17, 2007 Author Share Posted August 17, 2007 I've tried playing around with those ideas but now get this error message - Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\website\basket.php on line 158 You have products in your basket. Any more suggestions? <?php $sessid = session_id(); //display number of products in cart $query = "SELECT SUM (quan) as total from baskettemp WHERE sess = '$sessid'"; $rowx = mysql_fetch_array($query); echo "You have " . $rowx['total'] . "products in your basket."; ?> <br /><br /> <table border="1" align="center" cellpadding="5"> <tr> <td>Quantity</td> <td>Type</td> <td>Name</td> <td>Price Each</td> <td>Extended Price</td> <td></td> <tr> <?php $query = "SELECT * from baskettemp WHERE sess = '$sessid'"; $results = mysql_query($query) or die (mysql_query()); while ($row = mysql_fetch_array($results)) { extract ($row); $prod = "SELECT * FROM products WHERE id = '$prodnum'"; $prod2 = mysql_query($prod); $prod3 = mysql_fetch_array($prod2); extract ($prod3); echo "<td><form method = 'POST' action='change.php'> <input type='hidden' name='prodnum' value='$prodnum'> <input type='hidden' name='sessid' value='$sessid'> <input type='hidden' name='hidden' value='$hidden'> <input type='text' name='qty' size='2' value='$quan'>"; echo "</td>"; echo "<td>"; echo $type; echo "</td>"; echo "<td>"; echo $name; echo "</td></a>"; echo "<td align='right'>"; echo $price; echo "</td>"; echo "<td align='right'>"; //get extended price $extprice = $price * $quan; echo number_format($extprice, 2); echo "</td>"; echo "<td>"; echo "<input type='submit' name='Submit' value='Change Qty'> </form></td>"; echo "<td>"; echo "<form method = 'POST' action='delete.php'> <input type='hidden' name='prodnum' value='$prodnum'> <input type='hidden' name='qty' value='$quan'> <input type='hidden' name='hidden' value='$hidden'> <input type='hidden' name='sessid' value='$sessid'>"; echo "<input type='submit' name='Submit' value='Delete Item'> </form></td>"; echo "</tr>"; //add extended price to total $total = $extprice + $total; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/65279-counting/#findComment-326509 Share on other sites More sharing options...
lemmin Posted August 22, 2007 Share Posted August 22, 2007 Change: $query = "SELECT SUM (quan) as total from baskettemp WHERE sess = '$sessid'"; to: $query = mysql_query("SELECT SUM (quan) as total from baskettemp WHERE sess = '$sessid'"); Quote Link to comment https://forums.phpfreaks.com/topic/65279-counting/#findComment-331383 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.