Jump to content

[SOLVED] exploding multiple arrays


aebstract

Recommended Posts

Okay I have this code inside functions.php:

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="index.php?page=cart&action=update" method="post" id="cart">';
	foreach ($contents as $id=>$qty) {



		$sql = 'SELECT * FROM p_products WHERE id = '.$id;
		$result = $db->query($sql);
		$row = $result->fetch();
		extract($row);
		$output[] = '<table width="590" style="border-top: 1px solid #000;">';
		$output[] = '<tr>';
		$output[] = '<td width="100"><img src="products/' .$partnumber. '&#45;cart.jpg" class="jkimagelarge" title="products/' .$partnumber. '.jpg" /></td>';
		$output[] = '<td width="350">'.$partname.' <br /> '.$partnumber.' <br /> $'.$price.' - size -  '.$size.'</td>';
		$output[] = '<td width="60"><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
		$output[] = '<td width="80">$'.($price * $qty).'</td>';
		$total += $price * $qty;
		$output[] = '</tr>';
		$output[] = '</table>';
	}
	$output[] = '<p align="right">Grand total: $'.$total.'</p>';
	$output[] = '<div><button name="update" type="submit">Update cart</button></div>';
	$output[] = '</form>';
} else {
	$output[] = '<p>You shopping cart is empty.</p>';
}
return join('',$output);
}

 

Which is all there really is to the cart. If you want it, there is this, inside cart.php:

 



$cart = $_SESSION['cart'];
$action = $_GET['action'];
switch ($action) {
case 'add':
	if ($cart) {
		$cart .= ','.$_GET['id'];
		header("Location: index.php?page=cart");
		exit;
	} else {
		$cart = $_GET['id'];
		header("Location: index.php?page=cart");
		exit;		}
	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;

Just so noone asks for it later for any reason. Now when you add an item to your cart, it is set in the session variable as the id from the database. So if you add several items, the variable maybe be. 4,4,5,6,2,4. For my own reasons, I need to be able to add an item to that session, such as: 3,4,5,2,5;1.5,2,2,3. Right now this will work, and what it does is try to fetch 5;1.5 as the id. I want it to use 5 as the id and 1.5 as $size. So if a ; exists within the $id variable it should explode it again in to $id and $size.

Link to comment
Share on other sites

Okay here is where I put it in, which may be where my problem is:


function showCart() {
global $db;
$cart = $_SESSION['cart'];
if ($cart) {
	$items = explode(',',$cart);
	list($item,$size) = explode(';',$item);
	$contents = array();
	foreach ($items as $item) {
		$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
	}

	$output[] = '<form action="index.php?page=cart&action=update" method="post" id="cart">';
	foreach ($contents as $id=>$qty) {



		$sql = 'SELECT * FROM p_products WHERE id = '.$id;
		$result = $db->query($sql);
		$row = $result->fetch();
		extract($row);
		$output[] = '<table width="590" style="border-top: 1px solid #000;">';
		$output[] = '<tr>';
		$output[] = '<td width="100"><img src="products/' .$partnumber. '-cart.jpg" class="jkimagelarge" title="products/' .$partnumber. '.jpg" /></td>';
		$output[] = '<td width="350">'.$partname.' <br /> '.$partnumber.' <br /> $'.$price.' - size -  '.$size.'</td>';
		$output[] = '<td width="60"><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
		$output[] = '<td width="80">$'.($price * $qty).'</td>';
		$total += $price * $qty;
		$output[] = '</tr>';
		$output[] = '</table>';
	}
	$output[] = '<p align="right">Grand total: $'.$total.'</p>';
	$output[] = '<div><button name="update" type="submit">Update cart</button></div>';
	$output[] = '</form>';
} else {
	$output[] = '<p>You shopping cart is empty.</p>';
}
return join('',$output);
}


 

I also have this neat function, just for testing:

 

function showCart2() {
$cart = $_SESSION['cart'];

$var = explode(',',$cart);
$cnt = count($var);

for ($i=0; $i<$cnt; $i++) {
    list($id, $size) = explode(";", $var[$i]);

if (isset($size)) {
   echo "ID is $id and Size is $size <br />";
} else {
   echo "ID is $id and there is no size! <br />";
}

}
}

 

I echo that function out just to make sure everything is working. With that second function I get these results:

ID is 8 and there is no size!

ID is 1 and there is no size!

ID is 10 and Size is 1.5

 

However, with my main one that I am trying to get working, I end up having the first two products show correctly and my third item shows as the second item again.

 

 

BPE-2009-2517

$24.63 - size - $24.63

 

BPE-2008-2505

$15.64 - size - $15.64

 

BPE-2008-2505

$15.64 - size - $15.64

 

The first two items are correct, but the third item should not be BPE-2008-2505 (that's the second item, 1).

Link to comment
Share on other sites

try it here:

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="index.php?page=cart&action=update" method="post" id="cart">';
      foreach ($contents as $id=>$qty) {

        list($id,$size) = explode(';',$id);


         $sql = 'SELECT * FROM p_products WHERE id = '.$id;
         $result = $db->query($sql);
         $row = $result->fetch();
         extract($row);
         $output[] = '<table width="590" style="border-top: 1px solid #000;">';
         $output[] = '<tr>';
         $output[] = '<td width="100"><img src="products/' .$partnumber. '-cart.jpg" class="jkimagelarge" title="products/' .$partnumber. '.jpg" /></td>';
         $output[] = '<td width="350">'.$partname.' <br /> '.$partnumber.' <br /> $'.$price.' - size -  '.$size.'</td>';
         $output[] = '<td width="60"><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
         $output[] = '<td width="80">$'.($price * $qty).'</td>';
         $total += $price * $qty;
         $output[] = '</tr>';
         $output[] = '</table>';
      }
      $output[] = '<p align="right">Grand total: $'.$total.'</p>';
      $output[] = '<div><button name="update" type="submit">Update cart</button></div>';
      $output[] = '</form>';
   } else {
      $output[] = '<p>You shopping cart is empty.</p>';
   }
   return join('',$output);
}


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.