greetings . . .
I'm trying to compare 2 multidimensional arrays. The first is comprised of items being passed from a form. The second is comprised of items already in the Db.
What I want to do is step through both arrays and create a new array from the passed values and database values.
If the item exists in both places (passed & Db) I want to add the quantities. If the value is new (ie passed from the form) I want to add the values to the array.
help . . .
//-------------------------------------------------------------------------------
// DETERMINE PRODUCTS ALREADY IN CART
//-------------------------------------------------------------------------------
$cart_items_qry = mysql_query("SELECT cart.product_id,cart.date,cart.quantity,product.type,product.title,product.description,product.price FROM product,cart WHERE (cart.cart_id='$cart_id' AND cart.product_id=product.product_id)", $db) or die("Error8: ".mysql_error());
$key_q = 0;
while (list($existing_product_id,$existing_date,$existing_quantity,$existing_type,$existing_title,$existing_description,$price) = mysql_fetch_row($cart_items_qry))
{
$existing_product_bundle[$key_q][0] = $existing_product_id;
$existing_product_bundle[$key_q][1] = $existing_date;
$existing_product_bundle[$key_q][2] = $existing_quantity;
$key_q++;
}
while(list($key3, $value3) = each($existing_product_bundle))
{
//print("Existing: $key3: value3[0]=$value3[0], value3[1]=$value3[1], value3[2]=$value3[2]<br>");
}
reset($existing_product_bundle);
//-------------------------------------------------------------------------------
// DETERMINE PRODUCTS BEING PASSED
//-------------------------------------------------------------------------------
// If tickets
//echo "$product_id";
//echo "$product_date";
//echo "$qty";
if (($product_type=="tickets") && ($product_date))
{
// put form data into multidimensional array
while(list($key4, $value4) = each($product_id))
{
$passed_product_bundle[$key4][0] = $value4;
}
while(list($key5, $value5) = each($product_date))
{
$passed_product_bundle[$key5][1] = $value5;
}
while(list($key6, $value6) = each($qty))
{
$passed_product_bundle[$key6][2] = $value6;
}
while(list($key7, $value7) = each($passed_product_bundle))
{
//print("New: $key7: value7[4]=$value7[4], value7[5]=$value7[5], value7[6]=$value7[6]<br>");
}
reset($passed_product_bundle);
//-------------------------------------------------------------------------------
// COMPARE PASSED WITH EXISTING
//-------------------------------------------------------------------------------
// first check for matches
$cnt_cmp = "0";
while(list($key8, $value8) = each($existing_product_bundle)) {
$new_product_id = $value8[0];
$new_product_date = $value8[1];
$new_qty = $value8[2];
echo "Existing Product IDs: $new_product_id<br>\n";
$cnt_cmp2 = "0";
while(list($key9, $value9) = each($passed_product_bundle)) {
$passed_product_id = $value9[0];
$passed_product_date = $value9[1];
$passed_qty = $value9[2];
echo "Passed Product IDs: $passed_product_id<br>\n";
// check if product IDs match and then unleash a torrent of comparisons
if ($value8[0] == $value9[0]) {
// add qty
$new_product_id = $value8[0];
$new_product_date = $value8[1];
$new_qty = $value8[2] + $value9[2];
echo "Matching Product IDs: $new_product_id<BR>\n";
//echo "Passed Product Bundle: $passed_product_bundle[$key9]<br>\n";
unset($passed_product_bundle[$key9]);
}
echo "cnt_cmp2: $cnt_cmp2<br>\n";
$cnt_cmp2++;
}
// Luigi makes a newa arraya
$new_product_bundle[$cnt_cmp][0] = $new_product_id;
$new_product_bundle[$cnt_cmp][1] = $new_product_date;
$new_product_bundle[$cnt_cmp][2] = $new_qty;
echo "cnt_cmp: $cnt_cmp<br>\n";
$cnt_cmp++;
}
// then take left over non-matches and add them to new array
while(list($key10, $value10) = each($passed_product_bundle)) {
if ($value10[2] > 0) {
$new_product_bundle[$cnt_cmp][0] = $value10[0];
$new_product_bundle[$cnt_cmp][1] = $value10[1];
$new_product_bundle[$cnt_cmp][2] = $value10[2];
}
}
while(list($key11, $value11) = each($new_product_bundle)) {
//echo "new_product_bundle(product id) = $value11[0]<BR>\n";
//echo "new_product_bundle(product date) = $value11[1]<BR>\n";
//echo "new_product_bundle(product qty) = $value11[2]<BR>\n";
}
}