Re: [PHP] endless while loop
"Jason Wong" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > On Thursday 06 January 2005 05:17, Gunter Sammet wrote: > >> I am having some trouble getting the following code to work: >> >> if (is_array($bundle_attributes)) { >> >> reset($bundle_attributes); >> >> while (list($option, $value) = each($bundle_attributes)) { >> >> reset($value); >> >> while (list($option2, $value2) = each($value)) { >> >> $this->contents[$products_id]['attributes'][$option] = $value; >> >> if (tep_session_is_registered('customer_id')) tep_db_query("insert into " >> . >> TABLE_CUSTOMERS_BASKET_BUNDLE_ATTRIBUTES . " (customers_id, products_id, >> sub_product_id, products_options_id, products_options_value_id) values >> ('" >> . (int)$customer_id . "', '" . tep_db_input($products_id) . "', '" . >> $option . "', '" . (int)$option2 . "', '" . (int)$value2 . "')"); >> >> } >> >> } >> >> } >> >> } >> >> It gives me an endless loop in the second while loop. It seems like it >> doesn't move the pointer to the next value in the $option array. > > But isn't the "next value" of $option controlled by the outer while-loop? > >> When I do >> a print in that loop, I am getting the key value pair for the first >> element >> in the $option array. > > So inside the inner while-loop, $option should never change. > >> Changing it to a for loop works. Any ideas why this >> happens? > > No. But I can tell you that: > > 1) the reset() aren't necessary > 2) even better, use a foreach-loop instead of while-loop > > -- > Jason Wong -> Gremlins Associates -> www.gremlins.biz > Open Source Software Systems Integrators > * Web Design & Hosting * Internet & Intranet Applications Development * > -- > Search the list archives before you post > http://marc.theaimsgroup.com/?l=php-general > -- > New Year Resolution: Ignore top posted posts Hi Jason: Thanks for the reply. I added some of the reset in just to make sure the pointer isn't somewhere else through earlier iterations/to make sure it doesn't make a difference. Will play around with the foreach as soon as I have some time. Reading through php.net it seems like there are a few pitfalls (as there can be with while) to watch out for. Found out what the problem was after I posted. Reading through the function manual for each I found the following: Caution Because assigning an array to another variable resets the original arrays pointer, our example above would cause an endless loop had we assigned $fruit to another variable inside the loop. That's what I did. Didn't had to but it has been a copy and paste from some other code I haven't cleaned up. Good thing on it is, that I now know that I can't do this. Thanks again! Gunter -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] endless while loop
On Thursday 06 January 2005 05:17, Gunter Sammet wrote: > I am having some trouble getting the following code to work: > > if (is_array($bundle_attributes)) { > > reset($bundle_attributes); > > while (list($option, $value) = each($bundle_attributes)) { > > reset($value); > > while (list($option2, $value2) = each($value)) { > > $this->contents[$products_id]['attributes'][$option] = $value; > > if (tep_session_is_registered('customer_id')) tep_db_query("insert into " . > TABLE_CUSTOMERS_BASKET_BUNDLE_ATTRIBUTES . " (customers_id, products_id, > sub_product_id, products_options_id, products_options_value_id) values ('" > . (int)$customer_id . "', '" . tep_db_input($products_id) . "', '" . > $option . "', '" . (int)$option2 . "', '" . (int)$value2 . "')"); > > } > > } > > } > > } > > It gives me an endless loop in the second while loop. It seems like it > doesn't move the pointer to the next value in the $option array. But isn't the "next value" of $option controlled by the outer while-loop? > When I do > a print in that loop, I am getting the key value pair for the first element > in the $option array. So inside the inner while-loop, $option should never change. > Changing it to a for loop works. Any ideas why this > happens? No. But I can tell you that: 1) the reset() aren't necessary 2) even better, use a foreach-loop instead of while-loop -- Jason Wong -> Gremlins Associates -> www.gremlins.biz Open Source Software Systems Integrators * Web Design & Hosting * Internet & Intranet Applications Development * -- Search the list archives before you post http://marc.theaimsgroup.com/?l=php-general -- New Year Resolution: Ignore top posted posts -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] endless while loop
Hi all: I am having some trouble getting the following code to work: if (is_array($bundle_attributes)) { reset($bundle_attributes); while (list($option, $value) = each($bundle_attributes)) { reset($value); while (list($option2, $value2) = each($value)) { $this->contents[$products_id]['attributes'][$option] = $value; if (tep_session_is_registered('customer_id')) tep_db_query("insert into " . TABLE_CUSTOMERS_BASKET_BUNDLE_ATTRIBUTES . " (customers_id, products_id, sub_product_id, products_options_id, products_options_value_id) values ('" . (int)$customer_id . "', '" . tep_db_input($products_id) . "', '" . $option . "', '" . (int)$option2 . "', '" . (int)$value2 . "')"); } } } } It gives me an endless loop in the second while loop. It seems like it doesn't move the pointer to the next value in the $option array. When I do a print in that loop, I am getting the key value pair for the first element in the $option array. Changing it to a for loop works. Any ideas why this happens? TIA Gunter -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php