Jump to content

explode array explode > query


aebstract

Recommended Posts

Having trouble with this and don't really know what to call it. I've got my session set as something like: 4,4,5;10,3,5

Which is fine, and when I run it through to my shopping cart, I get my results displayed where the number = id in database. However, for the item 5;10, it just displays the information for the item above it and doesn't do anything for it. What I'm trying to do is check to see if the "id" has a ";" in it and if it does, explode that in to 2 parts, "5" and "10" and set the 5 as the id and use the 10 as "$size". Here is what I have:

 


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) {

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


Link to comment
https://forums.phpfreaks.com/topic/142803-explode-array-explode-query/
Share on other sites

Okay I took the entire if statement out and replaced it with what you stated. Here is what I am getting:

 

ID is 8 and there is no size!

ID is 4 and Size is 1.5

 

this is with: 8,4;1.5

which is correct, but this isn't part of my query dealing with the db.

the part with the query is display this:

 

 

 

[broken img]$ - size - 1.5

 

[broken img]$ - size - 1.5

 

The broken img, is suppose to be

/products/$partnumber-cart.jpg

it grabs that partnumber from the database based on id, it is outputting

/products/-cart.jpg

Here is the code:

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(';', $cart);


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

 

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(';', $cart);


		$sql = mysql_query("SELECT * FROM p_products WHERE id = '.$id.'") or die(mysql_error());;
		echo "$sql";
		$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 type="submit">Update cart</button></div>';
	$output[] = '</form>';
} else {
	$output[] = '<p>You shopping cart is empty.</p>';
}
return join('',$output);
}

 

I added one item to my cart, without a ; just a single item to make it easy and now it's not even doing that correctly.

 

Resource id #5, however it is actually set as 8.

Well for starters, look at what I said in my last reply:

 

"Resource id #5, however it is actually set as 8. "

 

I selected a part with id 8, the session has it set as id 8, though it is reading as "Resource id #5". Also read what I said about the images, the $id is not being set correctly with this.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.