Jump to content

Recommended Posts

Hi,

I m trying to export a cart script into paypal, for paypal to understand that ive sent multiple items i need to give each product in the array a number starting from one going up in numberical order,

iv tried to use a for loop with an increment counthing variable for($counter=1;$counter>0;$coutner++) but cant get it to work with this existing code; whats the easiest way to do this?! i know how to get all of the variables and stuff into the form i need to send to paypal so i would just like to know how to output the number from a variable where it says in teh code below...

 

i want the final output to be something like this

                       

1 ProductA Quantity: 20

2 ProductB Quantity: 15

3 ProductC Quantity: 6

 

this is the point at which i output the cart before proceeding to the checkout,

 

function showCart() {
    global $db;
    $cart = $_SESSION['cart'];
    if ($cart) {
        $items = explode(',',$cart);
        $contents = array();
        
        
        foreach ($items as $item) 
            {
            $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
            }        
        
        $output[] = '<form action="cart.php?action=update" method="post" id="cart">';
        $output[] = '<table>';
        
        foreach ($contents as $id=>$qty) 
            {
            $sql = 'SELECT * FROM syn_vinyl_product WHERE prod_id = '.$id;
            $result = $db->query($sql);
            $row = $result->fetch();
            extract($row);
            
            $output[] = '<tr>';
            $output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
            $output[] = '<td>'.$prod_title.' by '.$prod_desc.'</td>';
            $output[] = '<td>£'.$price.'</td>';
            $output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
            $output[] = '<td>£'.($price * $qty).'</td>';
            $output[] = '<td>//////////    OUTPUTT THE NUMBER HERE  /////////////</td>';
            
            $total = '';
            $total += $price * $qty;
            
            $output[] = '</tr>';
            }
            
        $output[] = '</table>';
        $output[] = '<p>Grand total: <strong>£'.$total.'</strong></p>';
        $output[] = '<div><button type="submit">Update cart</button></div>';
        $output[] = '</form>';
    }  

 

THANKS ALOT!!!

Link to comment
https://forums.phpfreaks.com/topic/157425-cart-to-paypal/
Share on other sites

the cart code

 

<?php
##########
### saved from http://v3.thewatchmakerproject.com/journal/276/building-a-simple-php-shopping-cart
##########


// Include MySQL class
require_once('mysql.class.php');
// Include database connection
require_once('global.inc.php');
// Include functions
require_once('functions.inc.php');
// Start the session
session_start();
// Process actions
$cart = $_SESSION['cart'];
$action ='';
if (isset($_GET['action']))
{
$action = $_GET['action'];
};
switch ($action) {
case 'add':
	if ($cart) {
		$cart .= ','.$_GET['id'];
	} else {
		$cart = $_GET['id'];
	}
	break;
case 'delete':
	if ($cart) {
		$items = explode(',',$cart);
		$newcart = '';
		foreach ($items as $item) {
			if ($_GET['id'] != $item) {
				if ($newcart != '') {
					$newcart .= ','.$item;
				} else {
					$newcart = $item;
				}
			}
		}
		$cart = $newcart;
	}
	break;
case 'update':
if ($cart) {
	$newcart = '';
	foreach ($_POST as $key=>$value) {
		if (stristr($key,'qty')) {
			$id = str_replace('qty','',$key);
			$items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart);
			$newcart = '';
			foreach ($items as $item) {
				if ($id != $item) {
					if ($newcart != '') {
						$newcart .= ','.$item;
					} else {
						$newcart = $item;
					}
				}
			}
			for ($i=1;$i<=$value;$i++) {
				if ($newcart != '') {
					$newcart .= ','.$id;
				} else {
					$newcart = $id;
				}
			}
		}
	}
}
$cart = $newcart;
break;
}
$_SESSION['cart'] = $cart;
?>

Link to comment
https://forums.phpfreaks.com/topic/157425-cart-to-paypal/#findComment-829882
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.