Jump to content

echo array key?


ievernova

Recommended Posts

Hi,

I m trying to export a cart script to paypal as a form, 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 o i would just like to know how to output the array key number (is i?) as 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 writeShoppingCart() {
$cart = $_SESSION['cart'];
if (!$cart) {
	return '<p>You have no items in your shopping cart</p>';
} else {
	// Parse the cart session variable
	$items = explode(',',$cart);
	$s = (count($items) > 1) ? 's':'';
	return '<p>You have <a href="cart.php">'.count($items).' item'.$s.' in your shopping cart</a></p>';
}
}

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 '.$artist.'</td>';
		$output[] = '<td>£'.$price.'</td>';
		$output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
		$output[] = '<td>£'.($price * $qty).' ////////////////ITEM 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>';
} else {
	$output[] = '<p>You shopping cart is empty.</p>';
}
return join('',$output);
}

 

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

 

THANKS ALOTT!!!!!!!!!

Link to comment
Share on other sites

im already using a foreach loop to output the cart items and have tried using soemthing like that already, as far as i understand i want to put teh foreach loop you mentioned above where the current foreach loop mis that display the cart items, how do i combine two foreach loops?

Link to comment
Share on other sites

I read through it.  $items appears to be an array of item id's that the customer selected.  $contents appears to be an attempt to get rid of empty nodes (does not look right).  I would code it directly like this:

 


  $contents = array();
  $cnt = 0;
  foreach ($items as $item) {
    if (isset($item) {
      $cnt += 1;
      $contents[$cnt] = $item;
  } }

 

Link to comment
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.