Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/65279-counting/
Share on other sites

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");
?>

Link to comment
https://forums.phpfreaks.com/topic/65279-counting/#findComment-326003
Share on other sites

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.";

Link to comment
https://forums.phpfreaks.com/topic/65279-counting/#findComment-326212
Share on other sites

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;

               }
?>

Link to comment
https://forums.phpfreaks.com/topic/65279-counting/#findComment-326509
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.