Page 1 of 1
stuck on a simple foreach
Posted: Mon Dec 20, 2010 5:24 am
by adibranch
pickle | Please use [ syntax=php ], [ syntax=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:
Posting Code in the Forums to learn how to do it too.
Hi all, wonder if any of you can help. I'm not much a of a PHP person, and after battling with this for a few hours, i thought i'd ask on here.
HEre's the code..
Code: Select all
$products_list_id = $cart->get_product_id_list() ;
$products_list_id_array = array($products_list_id);
foreach($products_list_id_array as $pidvalue) {
$check_free_shipping_query = tep_db_query("select products_free_shipping from " . TABLE_PRODUCTS . " where products_id = '" . (int)$pidvalue . "'");
$check_free_shipping = tep_db_fetch_array($check_free_shipping_query);
$check_free_shipping_array[] = $check_free_shipping['products_free_shipping'];
}
if (in_array("1", $check_free_shipping_array) && !in_array("0", $check_free_shipping_array)) {
$free_shipping = true;
}
Basically , the function get_product_id returns an array as follows: 41,32,56 etc etc.
The middle section gets the value of 'products_free_shipping' from db in relation to the product ID, and bungs the value in an array.
The bottom checks to see if certain requirements are met, and if so set free_shipping as true.
The problem is that the foreach only seems to be returning one value in the loop, then stopping. It doesnt go one to check the second one in the list.
What am i doing wrong?
pickle | Please use [ syntax=php ], [ syntax=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:
Posting Code in the Forums to learn how to do it too.
Re: stuck on a simple foreach
Posted: Mon Dec 20, 2010 6:09 am
by social_experiment
Code: Select all
<?php print_r($products_list_id_array); ?>
What do you get if you dump the contents of the variable?
Re: stuck on a simple foreach
Posted: Mon Dec 20, 2010 6:32 am
by adibranch
Array ( [0] => 462, 452 ) (these relate to products ID's)
I think that bit is fine.. its the foreach that seems to be the issue, as it just stops on the first check.
Re: stuck on a simple foreach
Posted: Mon Dec 20, 2010 8:12 am
by edhen
by the looks of it.. if $cart->get_product_id_list() is an array then you placing an array into another array otherwise $products_list_id must only be containing 1 value
Re: stuck on a simple foreach
Posted: Mon Dec 20, 2010 8:43 am
by adibranch
okay thanks, so how can i get round it?
Also, What is the following line checking for exactly?
f (in_array("1", $check_free_shipping_array) && !in_array("0", $check_free_shipping_array)) {
Re: stuck on a simple foreach
Posted: Mon Dec 20, 2010 9:51 am
by social_experiment
adibranch wrote:its the foreach that seems to be the issue, as it just stops on the first check
Yes and no. Yes because the foreach statement is only doing one check and no because there is only 1 element in the array.
Code: Select all
<?php if (in_array("1", $check_free_shipping_array) && !in_array("0", $check_free_shipping_array)) ?>
in_array — Checks if a value exists in an array. If the value 1 is inside the array called $check_free_shipping_array and the value 0 is not in the array $check_free_shipping_array the variable $free_shipping is true.
Re: stuck on a simple foreach
Posted: Mon Dec 20, 2010 10:07 am
by adibranch
aha got you on that bit, thanks.
So, the issue is, why isn't the following cycling through and checking the values of $products_list_id_array in turn? (assuming $products_list_id_array contains 462, 452). Why is it stopping at 462 and not checking 452?
Code: Select all
$products_list_id = $cart->get_product_id_list() ;
$products_list_id_array = array($products_list_id);
foreach($products_list_id_array as $pidvalue) {
$check_free_shipping_query = tep_db_query("select products_free_shipping from " . TABLE_PRODUCTS . " where products_id = '" . (int)$pidvalue . "'");
$check_free_shipping = tep_db_fetch_array($check_free_shipping_query);
$check_free_shipping_array[] = $check_free_shipping['products_free_shipping'];
}
Re: stuck on a simple foreach
Posted: Mon Dec 20, 2010 11:25 am
by social_experiment
When the foreach statement is run it returns the value of '462, 452' but in the query ($check_free_shipping_query) that value is converted to integer by the (int) in front of $pidvalue and that turns '462, 452' into '462'.
Re: stuck on a simple foreach
Posted: Mon Dec 20, 2010 11:39 am
by adibranch
hmmm
i thought the foreach checked each value of $products_list_id_array (462, 452) in turn
ie on the first run it see's 462 first in (int)$pidvalue, then loops through, and then on the next run it see's 452. Is this not the case then? If not, how can i alter it to do that?
Re: stuck on a simple foreach
Posted: Mon Dec 20, 2010 4:56 pm
by social_experiment
adibranch wrote:i thought the foreach checked each value of $products_list_id_array (462, 452) in turn
You're probably going to get tired of this but here goes: Your array only has
1 element, foreach doesn't loop through the values contained in the array elements but only through the elements.
The issue most likely lies with this statement : $products_list_id = $cart->get_product_id_list();. There's probably a reason why the query is manipulated in this way. You could contact the person who created the script and find out their reasoning for this.
Re: stuck on a simple foreach
Posted: Mon Dec 20, 2010 7:20 pm
by MartinW
$products_list_id looks more like a string to me, based on the print_r output. So $products_list_id_array is really just an array with a single element, and that element is the string '462, 452'. To convert that to an array, try this:
Code: Select all
$products_list_id_array = explode(',', $products_list_id);
This will give you an array containing 2 elements, the strings '462' and ' 452'. If you're certain that each number in the list (ie. the string) is separated by a comma with a space, you could replace the ',' with ', ' (ie. a comma followed by a space) to get the strings '462' and '452' (ie. '452' no longer has a leading space). But of course that's not required, as (int)$pidvalue will get rid of the space when it treats it like a regular number. I'd suggest using ', ' (comma followed by space) in the explode call, just in case you use $products_list_id_array later on in some way.
Martin
Re: stuck on a simple foreach
Posted: Tue Dec 21, 2010 6:41 am
by adibranch
aha i seeee.. i guess i ought to give a little background to this too. First off, i'm a web marketer who bodges bits of code when i need to, so a lot of stuff is beyond me.
The code i pasted is my attempt at amending a piece of code written elsewhere for the site. The original code uses a different set of data which i cant use on this particular page for various reasons. So, i attempted to rewrite it using an existing function get_product_id_list , to generate the product ID's. This is as follows:
Code: Select all
function get_product_id_list() {
$product_id_list = '';
if (is_array($this->contents))
{
reset($this->contents);
while (list($products_id, ) = each($this->contents)) {
$product_id_list .= ', ' . $products_id;
}
}
return substr($product_id_list, 2);
}
This returns the 462,452 etc, which i thought i could use to loop through and check the products_free_shipping state for each one. This is obviously where i fell down

i'll have another go with the guidance above. Thanks for the replies so far !
Re: stuck on a simple foreach
Posted: Tue Dec 21, 2010 7:03 am
by adibranch
MartinW wrote:$products_list_id looks more like a string to me, based on the print_r output. So $products_list_id_array is really just an array with a single element, and that element is the string '462, 452'. To convert that to an array, try this:
Code: Select all
$products_list_id_array = explode(',', $products_list_id);
Martin
Martin, that worked a treat .. really grateful, thanks !
I'd love to learn more about exactly how and why it works, but this is why we each do our own jobs and not try to do other peoples, so for the moment i'm just happy it does , but it's nice to know I was nearly there then with my original bodge coding, i was just missing something.
Thanks everyone.