View Full Version : Array WOES!

03-11-2010, 11:02 PM
Ok so first off here is my function:

function showCart() {
global $db, $denom;
$cart = $_SESSION['cart'];
$cart_price = $_SESSION['cart_price'];
if ($cart) {
$items = explode(',',$cart);
$items2 = explode(',',$cart_price);
$contents = array();
foreach ($items as $item) {
$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
$output = '<form action="cart.php?action=update" method="post" id="cart">';
$output .= '<table>';
foreach ($contents as $id=>$qty) {
$qry_fc = "SELECT * FROM functions AS fc, eventos AS ev, prices AS pc WHERE fc.fc_id=$id AND ev.ev_id=fc.fc_ev_id AND pc.pr_ev_id=fc.fc_ev_id LIMIT 1";
$result_fc = mysql_query ($qry_fc);;
$row_fc = mysql_fetch_array ($result_fc);
$output .= '<tr>';
$output .= '<td><a href="cart.php?action=delete&id='.$id.'"><img src="images/remove.png" border="0" width="16"></a></td>';
$output .= '<td><a href="event.php?id='.$ev_id.'" class="cartLINK">'.$ev_name.' - '.$fc_dtime.'</a></td>';
$output .= '<td>'.$denom.' '.$pr_price.'</td>';
$output .= '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
$output .= '<td>'.$denom.' '.($pr_price * $qty).'</td>';
$total += $pr_price * $qty;
$output .= '</tr>';
$output .= '</table>';
$output .= '<p>Grand total: <strong>'.$denom.' '.$total.'</strong></p>';
$output .= '<div><button type="submit">Update cart</button></div>';
$output .= '</form>';
$output .= '<a href="#" onclick="get(\'alert\', \'emptyCart\', \'\', \'get(\\\'shoppingcart\\\', \\\'showCart\\\', \\\'\\\')\')"><img src="images/empty.png" border="0"></a>';
} else {
$output = '<p>La canasta esta vacia. Busque Eventos <a href="index.php" class="cartLINK">Aqui</a></p>';
return $output;


Works just fine. Now the problem. You will noticed two things first off that are never used. cart_price and items2. These are new additions. What I need to do is this. Both those are now arrays as well. For each item in array one there is another number item in array two, that co-insides to array1. In other words let's spell it out:

Array1 = 1,2,1,5,1
Array2 = 1,1,2,1,4

So array one means that there are 3 quantities of item number 1. This works fine. Normally this would be this way, three of item number one. But the second array is a price list IDs. That connect to each one. There's not really 3 of item # 1, there is 3 different items # 1, same product, but different prices, hence it is considered a new item in the cart. So some how I need to merge these two arrays into one and make it work that way the way it currently does but just with this addition. I just made myself dizzie :confused:

Any ideas?

03-12-2010, 05:39 PM
I don't know if it's possible, as I didn't look closely at your code, but you should look into a design where your array can be uniquely keyed.

03-13-2010, 05:18 PM
Well I did it totally different than up there finally. I got messing with it and decided to do it this way.

What I did was have an array inside an array now. Now I pass all this info to the add cart function in this format:


Then when the add cart get's it, it adds it to the end of the session cart variable with a ",". Then when I want to read it out into the cart for showing I double explode. First I explode the main session variable "," into separate items for the cart. Then I foreach it and for each product, or line in the cart, I explode again, this time the ";" and this time I do the query of the DB using the two fields, id1 and id2 to find the correct row in each of the three tables that I am querying.

Then the final array item is the qty which I read back to make my price.

Yes it might not be the right way to handle this I know. BUT it works and works well. So for right now I'm happy.

Thanks for the advice.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum