Need some help...array maybe?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Need some help...array maybe?

Post by Burrito »

I have built a shopping cart for work here and the way it works is, every time they add somethign to the cart, it creates a new row on the database with the item, quantity, amount etc.

If they go ahead and purchase everything in the cart, I want to add a new row to my "buys" table and include all of the row ids from the cart. I'd like to do it in one field on the same row in the buys table, but not sure how I can swing it if they have muliple rows in the cart table. I'm assuming I can just do some sort of array and include all of the row ids from the cart table into one single field on the buys table, but this is where I need help

Burrito=arrayMoron...among many other moron types.

any suggestions you can offer will be greatly appreciated.

Burr
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

if pressed into sticking all the elements into a single field, I try to use a TEXT or BLOB type. Because the row id's have specific widths (in bytes), I compact them down to binary forms for each row number, so each row only uses 4 bytes of space.

An alternate, which I suggest doing is using a secondary table that attaches the item id's to the order id. Each row is 1 order id, and 1 item id. This allows far more items to be attached to a single order, as well as simplifiying retrival of item information, inventory information, and specific order information where you can retrieve the specific items in one query.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

sorry I didn't make the cart more clear.

my cart consists of two tables...one that has all of the items with their prices, item description, item title etc...

the other one is my "cart" table which when they have selected an item from the front end (which is driven by the items table), gets a new row with that item id and the quantity of that item and a few other small bits of info (not all of the info from the items table, but enough to get by).

where I need help is on my buys table (third table). After they've made the purchase I want to enter a new row in this third new table with all of the cart table's ids in one field on the new row. I guess I could just comma separate them, but thought there might be some fancier way of doing it.

lemme know,

Burr
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I was suggesting creating a fourth table, to attach the cart contents into, so you wouldn't have to create a custom storage, nor have possible difficulty in retrieving back the actual item information..

I'd like to suggest you read this pdf: http://www.oreilly.com/catalog/javadtab ... r/ch02.pdf

it should explain the benefits and reasoning behind my suggestion far better than I can. :)
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

While I agree with Feyd, there's another (Albeit crappy) option - throw id's into the field delimited by something.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

thx for the suggestion feyd...here's what I ended up doing which is working peachy...so far, but apparently crappy :):

Code: Select all

$getitem = mysql_query("select * from 21_cart where visid = 45")
		or trigger_error(mysql_error,E_USER_ERROR);
	$numrows = mysql_num_rows($getitem);
	$count = 1;
	$list = "";
	while($gtitem = mysql_fetch_assoc($getitem)){
	if($count !== $numrows){
	$list .= $gtitemї'id'] .","; 
	} else {
	$list .= $gtitemї'id'];
	}
	$count++;
	}
//have my comma separated list now...create array
	$listar = explode(",",$list);
then my body will look something like this:

Code: Select all

<html>
<head>
	<title>Untitled</title>
</head>

<body><table width="100%" border="1">
<?foreach($listar as $cid)&#123;
	$getitem = mysql_query("select c.quantity, c.price, i.itemdesc from 21_cart c, 21_cartitem i where c.id = ".$cid." and i.id = c.itemid")
	or trigger_error(mysql_error,E_USER_ERROR);
	if($gtitem = mysql_fetch_assoc($getitem))&#123;
	?>
	<tr>
	<td>Quant:<?=$gtitem&#1111;'quantity'];?></td><td>Price:<?=$gtitem&#1111;'price'];?></td><td>Desc:<?=$gtitem&#1111;'itemdesc'];?></td>
	</tr>
<?	&#125; // end if for resulst set
&#125; // end for each loop
?>
</table>

</body>
</html>
If I get some more time, I'll look into a different "uncrappy" solution.

Burr
Post Reply