Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

for inside a for help please.

Status
Not open for further replies.

hostagency

Technical User
Feb 12, 2003
9
US
Hi

I've got a php shopping cart and customers can add discounted bonus buys products to their cart when they buy a full priced product. I have a bonusbuys table created that associates a main product_id with a bonus buy product_id. My problem is that if the customer removes the main product from their cart I need to check the cart contents for associated bonus buys and remove them also. My current working code for removing a product from the cart is this:

case 'update_product' : for ($i=0, $n=sizeof($HTTP_POST_VARS['products_id']); $i<$n; $i++) {
if (in_array($HTTP_POST_VARS['products_id'][$i], (is_array($HTTP_POST_VARS['cart_delete']) ? $HTTP_POST_VARS['cart_delete'] : array()))) {
$cart->remove($HTTP_POST_VARS['products_id'][$i]);
}

And I need to do something like this but it only deletes one product and one bonus buy, multiple product deletes and bonus buy deletes fail:

case 'update_product' : for ($i=0, $n=sizeof($HTTP_POST_VARS['products_id']); $i<$n; $i++) {
if (in_array($HTTP_POST_VARS['products_id'][$i], (is_array($HTTP_POST_VARS['cart_delete']) ? $HTTP_POST_VARS['cart_delete'] : array()))) {
$check_for_bonusbuys_query = tep_db_query(&quot;select b.bonusbuys_id from bonusbuys b where b.products_id = '&quot; . $HTTP_POST_VARS['products_id'][$i] . &quot;'&quot;);
$cart->remove($HTTP_POST_VARS['products_id'][$i]);
$check_for_bonusbuys = tep_db_fetch_array($check_for_bonusbuys_query);
if ($check_for_bonusbuys['bonusbuys_id']) {
$check_products = $cart->get_products();
for ($i=0, $n=sizeof($check_products); $i<$n; $i++) {
if ($check_products[$i]['id'] == $check_for_bonusbuys['bonusbuys_id']) {
$cart->remove($check_for_bonusbuys['bonusbuys_id']);
}
}
}

Any advice would be most appreciated, I must have something the wrong way round or just wrote some very very bad code.....I'm pretty new to php.

Thanks for your time.

Del

 
Sure, I appreciate that, the main thing I'm struggling with is getting it to repeat the 'for' statement area, I runs once and stops.

Del
 
Still don't know.

I do know your if statement will always run, since the internal trinary condition always returns something that is non-zero.

I also notice, when I reformat your code, that your opening and closing braces do not match up.

Want the best answers? Ask the best questions: TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top