Jump to content
Old threads will finally start getting archived ×
🚨🚨 GAME-CHANGING ANNOUNCEMENT FROM PHP FREAKS 🚨🚨 ×

Recommended Posts

Is anyone experiencing in coding php shopping cart? I am having a really difficult time in one little part and if I can solve this

problem then I will be able to code a php shopping cart with no problem. I am having a hard time changing quantities on all

the saves products in the when you have more than 1 product in the shopping cart.

 

http://www.robslounge.com/robs_shoppingcart/

 

I am really frustrated, I have also referenced to many other tutorials but did not find them helpful.

 

 

I posted this before too and nobody replied  ??? The page should be working now

 

<?php require_once('Connections/shoppingcart.php'); ?>
<? session_start();
require("functions.php");

if(isset($_POST['Submit'])){
$id = $_POST['id'];
$quantity = $_POST['quantity'];

                
        mysql_query("UPDATE cart SET quantity = '$quantity' WHERE id = '$id'");

}



?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$maxRows_view_cart = 10;
$pageNum_view_cart = 0;
if (isset($_GET['pageNum_view_cart'])) {
  $pageNum_view_cart = $_GET['pageNum_view_cart'];
}
$startRow_view_cart = $pageNum_view_cart * $maxRows_view_cart;

mysql_select_db($database_shoppingcart, $shoppingcart);
$query_view_cart = "SELECT * FROM cart WHERE session_id = '$session_id'";
$query_limit_view_cart = sprintf("%s LIMIT %d, %d", $query_view_cart, $startRow_view_cart, $maxRows_view_cart);
$view_cart = mysql_query($query_limit_view_cart, $shoppingcart) or die(mysql_error());
$row_view_cart = mysql_fetch_assoc($view_cart);

if (isset($_GET['totalRows_view_cart'])) {
  $totalRows_view_cart = $_GET['totalRows_view_cart'];
} else {
  $all_view_cart = mysql_query($query_view_cart);
  $totalRows_view_cart = mysql_num_rows($all_view_cart);
}
$totalPages_view_cart = ceil($totalRows_view_cart/$maxRows_view_cart)-1;

mysql_select_db($database_shoppingcart, $shoppingcart);
$query_totalcost = "SELECT SUM(price) FROM cart WHERE session_id='$session_id'";
$totalcost = mysql_query($query_totalcost, $shoppingcart) or die(mysql_error());
$row_totalcost = mysql_fetch_assoc($totalcost);
$totalRows_totalcost = mysql_num_rows($totalcost);
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>
<table width="990" height="654" border="1" align="center" cellspacing="3">
  <tr>
    <td width="135" valign="top"> </td>
    <td width="636" valign="top"><br />
      <form id="form1" name="form1" method="post" action="">
        <p> </p>        
<?
# If the shopping cart in the current session is not empty
if($row_view_cart['id']!=""){ 
?>
                <table width="631" border="1">
          <tr>
            <td width="120"> </td>
            <td width="324"><br />
            </td>
            <td width="95"><label>quantity</label></td>
            <td width="64">Price</td>
          </tr>
          <?php do { ?>
          <tr>
         
              <td><img name="" src="products/<?php echo $row_view_cart['image']; ?>" width="120" height="120" alt="" /></td>
              <td><a href="productdetails.php?productid=<?php echo $row_view_cart['product_id']; ?>"><?php echo $row_view_cart['product_name']; ?></a><br />
                Price: $<?php echo $row_view_cart['storeprice']; ?></td>
              <td><select name="quantity" id="quantity">
                  <option value="1" <?php if (!(strcmp(1, $row_view_cart['quantity']))) {echo "selected=\"selected\"";} ?>>1</option>
                  <option value="2" <?php if (!(strcmp(2, $row_view_cart['quantity']))) {echo "selected=\"selected\"";} ?>>2</option>
                  <option value="3" <?php if (!(strcmp(3, $row_view_cart['quantity']))) {echo "selected=\"selected\"";} ?>>3</option>
                  <option value="4" <?php if (!(strcmp(4, $row_view_cart['quantity']))) {echo "selected=\"selected\"";} ?>>4</option>
                  <option value="5" <?php if (!(strcmp(5, $row_view_cart['quantity']))) {echo "selected=\"selected\"";} ?>>5</option>
                  <option value="6" <?php if (!(strcmp(6, $row_view_cart['quantity']))) {echo "selected=\"selected\"";} ?>>6</option>
                                  </select>
                  <input name="id" type="hidden" id="id" value="<?php echo $row_view_cart['id']; ?>" /></td>
              <td>$<?php echo  sprintf("%01.2f", $row_view_cart['price']); ?></td>
          
</tr>
<? echo "$row_view_cart[id],";?>
    <?php } while ($row_view_cart = mysql_fetch_assoc($view_cart)); ?>
        </table>
        <br />
        <table width="100%" border="1">
                           <tr>
                             <td><div align="right">Total: $<?php 
                             
                              echo sprintf("%01.2f", $row_totalcost['SUM(price)']);
                            ?></div></td>
                           </tr>
                         </table>
                         <br />
          <br />
          <label>
          <div align="right">
            <input type="submit" name="Submit" id="Submit" value="Submit" />
          </div>
          </label>
          <?
          # If it's been found that the current session does not have any products in the shopping cart. 
          } else {
          
          echo "<p align=center>Shopping cart is currently empty!</p>";
          }
          
          ?>
       
          <p align="center"><a href="index.php">Main menu</a></p>
        <p> </p>
        <p> </p>
        <p></p>
        <p> </p>
        <p> </p>
        <p> </p>
      </form>
      <p align="center"> </p></td>
    <td width="197" valign="top"> </td>
  </tr>
</table>
</body>
</html>
<?php
mysql_free_result($view_cart);

mysql_free_result($totalcost);
?> 

Look cooldude832

 

I dont know how to program it so it can update all the quantities accordingly thats why I am asking on here?! Thats why you will see

 mysql_query("UPDATE cart SET quantity = '$quantity' WHERE id = '$id'");

 

I have read many tutorials but it didnt help..

Well the "id" is the id of what?

The user's cart the item or what

 

I think that is your problem is you are using where id and you don't know what id is

 

 

When you add the first item in the shopping cart, it can update the quantity with no problem.

When you add the second item, it can still update the first item you add to the shopping cart but it wont update the quantity

on the second item. Thats the problem that Im having... that is all?!

Ok I managed to change the quantities of the shopping cart thankfully!!  :)

 

The only problem Im having is displaying the subtotal correctly. If you go on the webapage and let's say you add 3 products to the shopping cart

for instance it does display the subtotal (I was doing some testing and it is showing this under the table that displays the total) but it also

shows the other calculated subtotals and I dont want to display that.

 

www.robslounge.com/robs_shoppingcart

 

<?php require_once('Connections/shoppingcart.php'); ?>
<? session_start();
require("functions.php");

if(isset($_POST['Submit'])){

// Select Database
mysql_select_db($database_shoppingcart, $shoppingcart);
$query_UpdateQuantity = "SELECT * FROM cart";
$UpdateQuantity = mysql_query($query_UpdateQuantity, $shoppingcart) or die(mysql_error());
$row_UpdateQuantity = mysql_fetch_assoc($UpdateQuantity);
$totalRows_UpdateQuantity = mysql_num_rows($UpdateQuantity);

// Add Variables
$id = $_POST['id'];
$quantity = $_POST['quantity'];
$storeprice = $_POST['storeprice'];

// Update quantity
$c = array_combine($id, $quantity);

foreach ($c as $itemid => $quant) {


// Going through a loop, updating it by each ID
mysql_query("UPDATE cart SET quantity = '$quant' WHERE id = '$itemid'") or die (mysql_error());

// Delete the product from the database if the user sets the quantity to "0"
mysql_query("DELETE FROM cart WHERE quantity='0' AND session_id='$session_id'") or die (mysql_error());


}

}


?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$maxRows_view_cart = 10;
$pageNum_view_cart = 0;
if (isset($_GET['pageNum_view_cart'])) {
  $pageNum_view_cart = $_GET['pageNum_view_cart'];
}
$startRow_view_cart = $pageNum_view_cart * $maxRows_view_cart;

mysql_select_db($database_shoppingcart, $shoppingcart);
$query_view_cart = "SELECT * FROM cart WHERE session_id = '$session_id'";
$query_limit_view_cart = sprintf("%s LIMIT %d, %d", $query_view_cart, $startRow_view_cart, $maxRows_view_cart);
$view_cart = mysql_query($query_limit_view_cart, $shoppingcart) or die(mysql_error());
$row_view_cart = mysql_fetch_assoc($view_cart);

if (isset($_GET['totalRows_view_cart'])) {
  $totalRows_view_cart = $_GET['totalRows_view_cart'];
} else {
  $all_view_cart = mysql_query($query_view_cart);
  $totalRows_view_cart = mysql_num_rows($all_view_cart);
}
$totalPages_view_cart = ceil($totalRows_view_cart/$maxRows_view_cart)-1;

mysql_select_db($database_shoppingcart, $shoppingcart);
$query_totalcost = "SELECT SUM(storeprice) FROM cart WHERE session_id='$session_id'";
$totalcost = mysql_query($query_totalcost, $shoppingcart) or die(mysql_error());
$row_totalcost = mysql_fetch_assoc($totalcost);
$totalRows_totalcost = mysql_num_rows($totalcost);

mysql_select_db($database_shoppingcart, $shoppingcart);
$query_calculatetotal = "SELECT * FROM cart WHERE session_id = '$session_id'";
$calculatetotal = mysql_query($query_calculatetotal, $shoppingcart) or die(mysql_error());
$row_calculatetotal = mysql_fetch_assoc($calculatetotal);
$totalRows_calculatetotal = mysql_num_rows($calculatetotal);


?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>
<table width="990" height="654" border="1" align="center" cellspacing="3">
  <tr>
    <td width="135" valign="top"> </td>
    <td width="636" valign="top"><br />
      <form id="form1" name="form1" method="post" action="">
        <p> </p>        
<?
# If the shopping cart in the current session is not empty
if($row_view_cart['id']!=""){ 
?>
                <table width="631" border="1">
          <tr>
            <td width="120"> </td>
            <td width="324"><br />            </td>
            <td width="95"><label>quantity</label></td>
            <td width="64">Price</td>
          </tr>
          
          <?php do { ?>
          <tr>
         
              <td><img name="" src="products/<?php echo $row_view_cart['image']; ?>" width="120" height="120" alt="" /></td>
              <td><a href="productdetails.php?productid=<?php echo $row_view_cart['product_id']; ?>"><?php echo $row_view_cart['product_name']; ?></a><br />
                Price: $<?php echo $row_view_cart['storeprice']; ?></td>
              <td><select name="quantity[]" id="quantity[]">
                  <option value="0" <?php if (!(strcmp(0, $row_view_cart['quantity']))) {echo "selected=\"selected\"";} ?>>0</option>
                  <option value="1" <?php if (!(strcmp(1, $row_view_cart['quantity']))) {echo "selected=\"selected\"";} ?>>1</option>
                  <option value="2" <?php if (!(strcmp(2, $row_view_cart['quantity']))) {echo "selected=\"selected\"";} ?>>2</option>
                  <option value="3" <?php if (!(strcmp(3, $row_view_cart['quantity']))) {echo "selected=\"selected\"";} ?>>3</option>
                  <option value="4" <?php if (!(strcmp(4, $row_view_cart['quantity']))) {echo "selected=\"selected\"";} ?>>4</option>
                  <option value="5" <?php if (!(strcmp(5, $row_view_cart['quantity']))) {echo "selected=\"selected\"";} ?>>5</option>
                  <option value="6" <?php if (!(strcmp(6, $row_view_cart['quantity']))) {echo "selected=\"selected\"";} ?>>6</option>
                                  </select>
                  <input name="id[]" type="hidden" id="id[]" value="<?php echo $row_view_cart['id']; ?>" />
                  <input name="storeprice[]" type="hidden" id="storeprice[]" value="<?php echo $row_view_cart['storeprice']; ?>" /></td>
              <td>$<?php echo  sprintf("%01.2f", $row_view_cart['price']); ?></td>
</tr>


    <?php } while ($row_view_cart = mysql_fetch_assoc($view_cart)); ?>
        </table>
            <br />
        <table width="100%" border="1">
                           <tr>
                             <td><div align="right">Total: $<? echo $row_totalcost['SUM(storeprice)']; ?></div></td>
                           </tr>
                         </table>
           
        <?php do { ?>
          <?

$calculate += $row_calculatetotal['quantity'] * $row_calculatetotal['storeprice'];
echo $calculate;
echo "<br />";

?>
          <?php } while ($row_calculatetotal = mysql_fetch_assoc($calculatetotal)); ?>
        <br />
          <br />
          <label>
          <div align="right">
            <input type="submit" name="Submit" id="Submit" value="Submit" />
          </div>
          </label>
	  <?
	  # If it's been found that the current session does not have any products in the shopping cart. 
	  } else {
	  
	  echo "<p align=center>Shopping cart is currently empty!</p>";
	  }
	  
	  ?>
       
          <p align="center"><a href="index.php">Main menu</a></p>
        <p> </p>
        <p> </p>
        <p></p>
        <p> </p>
        <p> </p>
        <p> </p>
      </form>
      <p align="center"> </p></td>
    <td width="197" valign="top"> </td>
  </tr>
</table>
</body>
</html>
<?php
mysql_free_result($view_cart);

mysql_free_result($totalcost);

mysql_free_result($calculatetotal);

if(isset($_POST['Submit'])){
mysql_free_result($UpdateQuantity);
}
?>

 

how can I just display the calculated subtotal? Is there a way where you can calculate the price * quantity in the mysql database?

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.