7 Replies - 4017 Views - Last Post: 15 March 2013 - 11:33 AM Rate Topic: -----

#1 hajra_ch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 31-August 12

shopping cart issue

Posted 15 March 2013 - 05:21 AM

i have created a shopping cart and want to store the cart items in an array so that once the order is confirm i can store it in an order table.

can anyone help me?
thanks in advance


cart.php


<?php
session_start ();
//session_destroy ();

$page = 'index.php';

include 'db.php';
/*
if ( !isset( $_SESSION['cart'] ) ) {  
    $_SESSION['cart'] = array();  
}  

*/
//add
if (isset ($_GET['add']))
{
    $stock = mysql_query('SELECT itemID, stock FROM items WHERE itemID ='.mysql_real_escape_string((int)$_GET['add']));
    while ($stock_row =mysql_fetch_assoc($stock))
    {
        if ($stock_row ['stock']!=$_SESSION ['cart_'.(int)$_GET ['add']])
        {
            $_SESSION ['cart_'.(int)$_GET['add']]+='1';
        }
    }
    header ('Location: ' .$page);
   
}

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

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


function items () 
{
    $get = mysql_query ('SELECT itemID, name, description, price FROM items WHERE stock > 0 ORDER by itemID DESC');
            if (mysql_num_rows ($get)== 0)
                {
                echo "Item is out of stock";
                }
                else 
                    {
                    while ($get_row = mysql_fetch_assoc($get))
                    {
                        echo '<h1>' .$get_row ['name'].'</h1>';
                        echo '<p>' .$get_row ['description'].'<br/>&pound' .number_format($get_row ['price'], 2).' <a href="cart.php?add='.$get_row['itemID'].'">Add to basket</a> </p>';
                    }
                    }
}

/*
function order()
{
    $num =0;
    foreach ($_SESSION as $name => $value)
    {
        if ($value !=0)
        {
            if (substr($name, 0, 5)=='cart_')
            {
                $itemID = substr ($name, 5, strlen($name)-5);
                $get = mysql_query ('SELECT itemID, name, price FROM items WHERE stock > 0 ORDER by itemID DESC');
                while ($get_row = mysql_fetch_assoc ($get))
                {
                    $num++;
                   
                }
            }
         }
         
     }    
}
*/




function cart()
{
    //$_SESSION['cart'] = true;
    foreach ($_SESSION as $name => $value)
    {
        if ($value > 0)
        {
            if (substr($name, 0, 5)=='cart_')
            {
                $itemID = substr($name, 5,  (strlen($name)-5)); //removes cart_ on display
                $get =mysql_query ('SELECT itemID, name, price FROM items WHERE itemID='.mysql_real_escape_string((int)$itemID));
              
                while ($get_row = mysql_fetch_assoc ($get))
                {
                    $sub = $get_row ['price']* $value;
                    echo $get_row ['name']. 'x' .$value. '@ &pound;' .number_format($get_row['price'], 2). '=&pound;' .number_format($sub, 2). '<a href="cart.php?remove='.$itemID.'">[-]</a> <a href="cart.php?add='.$itemID.'">[+]</a> <a href="cart.php?delete='.$itemID.'">[Delete]</a><br/>';
                
                     
                }
            }
            $total =+ $sub;
            
        }
       
    }
    if ($total==0)
    {
        echo "Your cart is empty";
    }
    else 
    {
        echo 'Your total is &pound; ' .number_format($total, 2);
        //checkout button
    }
}

function orders()
{
    $num=0;
    foreach ($_SESSION as $name => $value)
    {
        if ($value > 0)
        {
            if (substr($name, 0, 5)=='cart_')
            {
                $itemID = substr($name, 5,  (strlen($name)-5)); //removes cart_ on display
                
                $get =mysql_query ('SELECT itemID, name, price FROM items WHERE itemID='.mysql_real_escape_string((int)$itemID));
              
                while ($get_row = mysql_fetch_assoc ($get))
                {
                    $sub = $get_row ['price']* $value;
                    echo $get_row ['name']. 'x' .$value. '@ &pound;' .number_format($get_row['price'], 2). '=&pound;' .number_format($sub, 2);
                
                  
                    
                    echo'<input type="hidden" name="item_num_'.$num.'" value="'.$itemID.'">';
                    echo'<input type="hidden" name="item_nam_'.$num.'" value="'.$get_row['name'].'">';
                    echo'<input type="hidden" name="amount'.$num.'" value="'.$get_row['price'].'">';
                    echo'<input type="hidden" name="item_quantity'.$num.'" value="'.$value.'">';
                    
                    $order[$itemID]= $value;
                    
                  while(list($key, $value)= each($order)){
                      
                      echo"$key; $value";
                  }
                    
                   
            $num++;
        }
                         
                      
                    
                    
                }
                 
            }
            
        }
       
    }
    if ($total==0)
    {
        echo "Your cart is empty";
    }
    else 
    {
        echo 'Your total is &pound; ' .number_format($total, 2);
        //checkout button
    }
}
?>




Is This A Good Question/Topic? 0
  • +

Replies To: shopping cart issue

#2 Anthonidas  Icon User is offline

  • D.I.C Head

Reputation: 30
  • View blog
  • Posts: 218
  • Joined: 25-April 11

Re: shopping cart issue

Posted 15 March 2013 - 05:37 AM

Why do you want to store it in an array? You already entered everything in your database, didn't you? So just use your entries for the order table.

In advance, I recommend to use PDO's instead of this mysql_crap!
Was This Post Helpful? 0
  • +
  • -

#3 hajra_ch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 31-August 12

Re: shopping cart issue

Posted 15 March 2013 - 05:48 AM

i havnt entered anything in the database order table thats what im trying to do.

i want to store the itemId and the quantity they have selected for one order

This post has been edited by Dormilich: 16 March 2013 - 02:53 AM

Was This Post Helpful? 0
  • +
  • -

#4 Anthonidas  Icon User is offline

  • D.I.C Head

Reputation: 30
  • View blog
  • Posts: 218
  • Joined: 25-April 11

Re: shopping cart issue

Posted 15 March 2013 - 05:56 AM

ok... so... how is the user going to add the items to his cart? are there checkboxes near each item? or are there just textboxes where you entere the quantities? how is this going to work? without more details it is impossible to help you...
Was This Post Helpful? 0
  • +
  • -

#5 hajra_ch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 31-August 12

Re: shopping cart issue

Posted 15 March 2013 - 06:00 AM

In the main index page is an add to cart button everyrime dat is pressed item is added into the cart...the customer is able to add and remove items aswell...

If you look at the fuction cart in the abovr code that is how to empty the cart add more quantity or remove the quantity is done

This post has been edited by Dormilich: 16 March 2013 - 02:52 AM

Was This Post Helpful? 0
  • +
  • -

#6 Anthonidas  Icon User is offline

  • D.I.C Head

Reputation: 30
  • View blog
  • Posts: 218
  • Joined: 25-April 11

Re: shopping cart issue

Posted 15 March 2013 - 06:14 AM

you could define an array that is filled out by your add-method...

$myArray = new array();

//add
if (isset ($_GET['add']))
{
	$stock = mysql_query('SELECT itemID, stock FROM items WHERE itemID ='.mysql_real_escape_string((int)$_GET['add']));
	while ($stock_row =mysql_fetch_assoc($stock))
	{
		if ($stock_row ['stock']!=$_SESSION ['cart_'.(int)$_GET ['add']])
		{
			$_SESSION ['cart_'.(int)$_GET['add']]+='1';
		}
	}
	// ----------- perhaps here... ----------
	$myArray[$_GET['add']]++;
	// ----- $myArray[ItemID] increased -----
	header ('Location: ' .$page);
}

Was This Post Helpful? 0
  • +
  • -

#7 hajra_ch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 31-August 12

Re: shopping cart issue

Posted 15 March 2013 - 11:27 AM

thanks

This post has been edited by Dormilich: 16 March 2013 - 02:53 AM

Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2895
  • View blog
  • Posts: 10,027
  • Joined: 08-August 08

Re: shopping cart issue

Posted 15 March 2013 - 11:33 AM

First, NEVER use MySQL functions when creating a shopping cart. Always use prepared statements.

Second, NEVER have the user submit pricing information. That should be determined by the server based on the item ids and the quantities selected.

Third, I'd have a class that defines the user. This would contain their information: name, address, phone, and anything else you might need from them. It would also contain an array of objects describing the items they have selected. That would be a different class.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1