Page 1 of 1
Need some help...array maybe?
Posted: Thu Jan 13, 2005 12:54 pm
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
Posted: Thu Jan 13, 2005 1:21 pm
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.
Posted: Thu Jan 13, 2005 1:33 pm
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
Posted: Thu Jan 13, 2005 1:42 pm
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.

Posted: Thu Jan 13, 2005 3:58 pm
by magicrobotmonkey
While I agree with Feyd, there's another (Albeit crappy) option - throw id's into the field delimited by something.
Posted: Thu Jan 13, 2005 4:05 pm
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){
$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)){
?>
<tr>
<td>Quant:<?=$gtitemї'quantity'];?></td><td>Price:<?=$gtitemї'price'];?></td><td>Desc:<?=$gtitemї'itemdesc'];?></td>
</tr>
<? } // end if for resulst set
} // end for each loop
?>
</table>
</body>
</html>
If I get some more time, I'll look into a different "uncrappy" solution.
Burr