Shopping cart

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

abbey4biz
Forum Commoner
Posts: 32
Joined: Wed Nov 23, 2011 12:25 pm

Shopping cart

Post by abbey4biz »

Hi, Friends, Please can somebody help me look into these add to cart scripts and see what i am missing.

Each time i add to cart, i get Your cart is empty.

Code: Select all

<?php
session_start();

if (isset($_GET['add'])){

$_SESSION['cart_'.(int)$_GET['add']]+='1';

}

if (isset($_GET['remove'])){
	$_SESSION['cart_'.(int)$_GET['remove']]--;
	header ('Location: m_addtocart.php');
}

if (isset($_GET['delete'])){
	$_SESSION['cart_'.(int)$_GET['delete']]='0';
	header ('Location: m_addtocart.php');
}

?>
<?php
function cart2(){

	foreach($_SESSION as $name => $value){
		if($value > 0){
			if(substr($name,0,5) == 'cart_'){
				$id = substr($name,5,(strlen($name)-5));
				
			$q = 'SELECT id, prodname, price FROM products WHERE id = $id';
			$r = @mysqli_query($dbc, $q);
			if ($r){
				while($get_row = mysql_fetch_array($r, MYSQLI_ASSOC)){
			$sub = $get_row['price']*$value;
				
			echo $get_row['prodname']. 'x'. $value . '@' . number_format($get_row['price'], 2). '='.number_format($sub, 2).' <a href="cart.php?remove='.$id.'">[-]</a> <a href="cart.php?add='.$id.'">[+]</a> <a href="cart.php?delete='.$id.'">[Delete]</a></br>';
			}
			}
		
			
		$total = $total + $sub;
			
				}
				}
				
if ($total == 0){
echo "Your Cart is empty!";
} else {
echo '<p>Total: N'.number_format($total, 2).'</p>';
echo "Checkout Button";
}
	}	}
?>
Thanks for your anticipated swift response.
x_mutatis_mutandis_x
Forum Contributor
Posts: 160
Joined: Tue Apr 17, 2012 12:57 pm

Re: Shopping cart

Post by x_mutatis_mutandis_x »

Is this script "m_addtocart.php" or "cart.php"? Also where is your "cart2()" function called?
abbey4biz
Forum Commoner
Posts: 32
Joined: Wed Nov 23, 2011 12:25 pm

Re: Shopping cart

Post by abbey4biz »

No this is not m_addtocart.php, this script is cart2.php. My cart2() function is called on m_addtocart.php

Thanks
x_mutatis_mutandis_x
Forum Contributor
Posts: 160
Joined: Tue Apr 17, 2012 12:57 pm

Re: Shopping cart

Post by x_mutatis_mutandis_x »

abbey4biz wrote:No this is not m_addtocart.php, this script is cart2.php. My cart2() function is called on m_addtocart.php

Thanks
So when user add's an item to the cart, he will request the page "cart.php?add=3" and you increment the quantity session value for that item ($_SESSION[cart_3]++), but your are not redirecting them to m_addtocart.php (which is where your total is getting calculated). Try this and let me know what you get:

Code: Select all

if (isset($_GET['add'])){
 
$_SESSION['cart_'.(int)$_GET['add']]+='1';
header('Location: m_addtocart.php'); 

}
 
abbey4biz
Forum Commoner
Posts: 32
Joined: Wed Nov 23, 2011 12:25 pm

Re: Shopping cart

Post by abbey4biz »

when i tried, i still get Your cart is empty
and also this warning at the top:

Warning: Cannot modify header information - headers already sent by (output started at /home/themarke/public_html/header.php:15) in /home/themarke/public_html/cart2.php on line 7
x_mutatis_mutandis_x
Forum Contributor
Posts: 160
Joined: Tue Apr 17, 2012 12:57 pm

Re: Shopping cart

Post by x_mutatis_mutandis_x »

abbey4biz wrote:when i tried, i still get Your cart is empty
and also this warning at the top:

Warning: Cannot modify header information - headers already sent by (output started at /home/themarke/public_html/header.php:15) in /home/themarke/public_html/cart2.php on line 7
Alright try this instead and let me know:

Code: Select all

if (isset($_GET['add'])){
  
 $_SESSION['cart_'.(int)$_GET['add']]+='1';
 cart2(); 

}
 
abbey4biz
Forum Commoner
Posts: 32
Joined: Wed Nov 23, 2011 12:25 pm

Re: Shopping cart

Post by abbey4biz »

Your cart is empty is now displayed twice.
x_mutatis_mutandis_x
Forum Contributor
Posts: 160
Joined: Tue Apr 17, 2012 12:57 pm

Re: Shopping cart

Post by x_mutatis_mutandis_x »

abbey4biz wrote:Your cart is empty is now displayed twice.
Ok, so looks like your function cart2() is being called anyways (somewhere else in your script), so revert back to your original code.
[syntax]

if (isset($_GET['add'])){

$_SESSION['cart_'.(int)$_GET['add']]+='1';

}
[/syntax]

I just noticed that you are using mysql_fetch_array instead of mysqli_fetch_array. I think that might cause a problem, because mysqli_query returns a 'mysqli_result' object, and mysql_fetch_array accepts a resource link. Change it to:
[syntax]
while ($get_row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
[/syntax]

Let me know what you get..
abbey4biz
Forum Commoner
Posts: 32
Joined: Wed Nov 23, 2011 12:25 pm

Re: Shopping cart

Post by abbey4biz »

I still get Your cart is empty
x_mutatis_mutandis_x
Forum Contributor
Posts: 160
Joined: Tue Apr 17, 2012 12:57 pm

Re: Shopping cart

Post by x_mutatis_mutandis_x »

abbey4biz wrote:I still get Your cart is empty
Well we have no other choice but to debug it :). Replace your function cart2() with this

Code: Select all


function cart2(){
 
        foreach($_SESSION as $name => $value){

                echo "Name=$name; Value=$value<br />\n"; //comment this later, this is for debugging

                 if($value > 0){
                        
                        echo  "Cart_" . substr($name, 0, 5) . "<br />\n"; //comment this later, this is for debugging
                        
                         if(substr($name,0,5) == 'cart_'){

                                 $id = substr($name,5,(strlen($name)-5));

                                 echo "ID=$id<br />\n";
                                 
                        $q = 'SELECT id, prodname, price FROM products WHERE id = $id';
                         $r = @mysqli_query($dbc, $q);
                         if ($r){
                                 while($get_row = mysqli_fetch_array($r, MYSQLI_ASSOC)){

                                 echo "Calculating Price..<br />\n"; //comment this later, this is for debugging

                         $sub = $get_row['price']*$value;
                                 
                        echo $get_row['prodname']. 'x'. $value . '@' . number_format($get_row['price'], 2). '='.number_format($sub, 2).' <a href="cart.php?remove='.$id.'">[-]</a> <a href="cart.php?add='.$id.'">[+]</a> <a href="cart.php?delete='.$id.'">[Delete]</a></br>';
                         }
                         }
                 
                        
                $total = $total + $sub;
                         
                                }
                                 }
                                 
if ($total == 0){
 echo "Your Cart is empty!";
 } else {
 echo '<p>Total: N'.number_format($total, 2).'</p>';
 echo "Checkout Button";
 }
         }       }
 

Let me know what gets printed on the screen
abbey4biz
Forum Commoner
Posts: 32
Joined: Wed Nov 23, 2011 12:25 pm

Re: Shopping cart

Post by abbey4biz »

Thanks for you effort. This was what i got:

Name=cart_1; Value=1936
Cart_cart_
ID=1
Your Cart is empty!
x_mutatis_mutandis_x
Forum Contributor
Posts: 160
Joined: Tue Apr 17, 2012 12:57 pm

Re: Shopping cart

Post by x_mutatis_mutandis_x »

abbey4biz wrote:Thanks for you effort. This was what i got:

Name=cart_1; Value=1936
Cart_cart_
ID=1
Your Cart is empty!
That indicates you have an error in your mysqli_query or its not returning any rows, because "Calculating Price.." never gets printed. Using '@' before a function will supress any errors, so remove that. Use something like:

Code: Select all

echo "DB Connection type: " . gettype($dbc); //comment this later, this is for debugging
 $r = mysqli_query($dbc, $q);

abbey4biz
Forum Commoner
Posts: 32
Joined: Wed Nov 23, 2011 12:25 pm

Re: Shopping cart

Post by abbey4biz »

ok, this is what i got now:

Name=cart_1; Value=2138
Cart_cart_
ID=1

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /home/themarke/public_html/cart2.php on line 39
Your Cart is empty!
x_mutatis_mutandis_x
Forum Contributor
Posts: 160
Joined: Tue Apr 17, 2012 12:57 pm

Re: Shopping cart

Post by x_mutatis_mutandis_x »

abbey4biz wrote:ok, this is what i got now:

Name=cart_1; Value=2138
Cart_cart_
ID=1

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /home/themarke/public_html/cart2.php on line 39
Your Cart is empty!
There you go. Your $dbc parameter is not initialized. You are not creating a connection in your cart2() function. Try this code for cart2()

Code: Select all

function cart2(){

      $dbc = mysqli_connect("host", "username", "password", "database_name");
  
         foreach($_SESSION as $name => $value){
 
                //echo "Name=$name; Value=$value<br />\n"; 
 
                 if($value > 0){
                         
                        //echo  "Cart_" . substr($name, 0, 5) . "<br />\n"; 
                         
                         if(substr($name,0,5) == 'cart_'){
 
                                 $id = substr($name,5,(strlen($name)-5));
 
                                // echo "ID=$id<br />\n";
                                  
                         $q = 'SELECT id, prodname, price FROM products WHERE id = $id';
                          $r = @mysqli_query($dbc, $q);
                          if ($r){
                                  while($get_row = mysqli_fetch_array($r, MYSQLI_ASSOC)){
 
                                 //echo "Calculating Price..<br />\n"; 
 
                         $sub = $get_row['price']*$value;
                                  
                         echo $get_row['prodname']. 'x'. $value . '@' . number_format($get_row['price'], 2). '='.number_format($sub, 2).' <a href="cart.php?remove='.$id.'">[-]</a> <a href="cart.php?add='.$id.'">[+]</a> <a href="cart.php?delete='.$id.'">[Delete]</a></br>';
                          }
                          }
                  
                         
                $total = $total + $sub;
                          
                                 }
                                  }
                                  
 if ($total == 0){
  echo "Your Cart is empty!";
  } else {
  echo '<p>Total: N'.number_format($total, 2).'</p>';
  echo "Checkout Button";
  }
          }       

mysqli_close($dbc);

}
"host" is your database hostname or ipaddress (localhost if your database server is running on the same machine as apache server)
"username" is the username you use to connect
"password" password associated with the username
"database_name" is the name of the database where your tables are stored
abbey4biz
Forum Commoner
Posts: 32
Joined: Wed Nov 23, 2011 12:25 pm

Re: Shopping cart

Post by abbey4biz »

same thing:

your cart is empty now only
Post Reply