Jump to content

Recommended Posts

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 -  $'.$price.'</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 type="submit">Update cart</button></div>';
	$output[] = '</form>';
} else {
	$output[] = '<p>You shopping cart is empty.</p>';
}
return join('',$output);
}

 

Okay, to add to the session is something like action=add&id=#. Which then this script will take the session and divide up all the items. So the array may be like 8,8,8,4,6,6,3,2,7,8. Which with this script will display having 4 of item 8, 2 of item 6, etc. I need to add a special thing to this and I about have it figured out but kinda lost. So I am going to give the option of a "size", the way I am thinking of formatting this is to add a ;# on to my numbers I have. So add&id=#;# = 8;1.5. Then in my array it would be:

8;1.5,8;1.5,8;1.2,4,6,6,3;1.4,2,7,8

Something like that, and explode the results of the first explode by ; to separate the first number from the second, then I need to set the first number as $id and would like to set the second number as $size. Can anyone point me in the right direction? I've already got it so it adds the ;# in the session, I just need to figure out how to explode and set the variable ;(

Link to comment
https://forums.phpfreaks.com/topic/142513-solved-help-with-explode-array-and-cart/
Share on other sites

<?php
$var = explode(';',$explodedVar);
$cnt = count($var);

for ($i=0; $i<$cnt; $i++) {
    list($id, $size) = explode(", ", $var[$i]);
   
   echo "ID is $id and Size is $size <br />";
}
?>

 

There is an example of how it will be done.

I switched around the , and the ; since it needs to explode by , first. Here it is:


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

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

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

   echo "ID is $id and Size is $size <br />";
}
}

It is echoing:

ID is 8 and Size is

ID is 10;1.5 and Size is

ID is 3 and Size is

 

I'm gonna continue to chug away at it and see if I can get it to separate the 10 from 1.5 and set 1.5 as the size variable and keep 10 as the id. If anyone has any input that would be great!

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.