amwd07 Posted July 26, 2008 Share Posted July 26, 2008 Hello I am trying to use for each instead of while now in my coding here is what I have so far $pquery = Db::getInstance()->ExecuteS('SELECT id_image, id_product FROM image GROUP BY id_product'); $p = $pquery['id_product']; $products = array($p=>1,$p=>2,$p=>3,$p=>4,$p=>5,$p=>6); foreach ($products as $key => $product) { print "$key - $product <br/>"; } The problem is it only prints the last id in the array ??? Quote Link to comment https://forums.phpfreaks.com/topic/116755-solved-simple-foreach-array-question/ Share on other sites More sharing options...
.josh Posted July 26, 2008 Share Posted July 26, 2008 well it looks like you are setting all of your element keys to the same thing in your $products = array($p=>1,$p=>2,$p=>3,$p=>4,$p=>5,$p=>6); so it's just overwriting each other so in the end all you are really doing is making an array with 1 element $products = array($p => 6); Quote Link to comment https://forums.phpfreaks.com/topic/116755-solved-simple-foreach-array-question/#findComment-600452 Share on other sites More sharing options...
amwd07 Posted July 26, 2008 Author Share Posted July 26, 2008 Thanks so how do I print all the elements in the array? there may be more than 6 which I will define in the WHERE clause query all I really need to do is loop id_product & possibly id_ image without using WHILE Quote Link to comment https://forums.phpfreaks.com/topic/116755-solved-simple-foreach-array-question/#findComment-600465 Share on other sites More sharing options...
amwd07 Posted July 26, 2008 Author Share Posted July 26, 2008 I see what you mean going back to the basics $products = array(a=>1,b=>2,c=>3,d=>4,e=>5,f=>6); how would I loop id_product ??? $products = array(product_id=>$p); :-\ Quote Link to comment https://forums.phpfreaks.com/topic/116755-solved-simple-foreach-array-question/#findComment-600492 Share on other sites More sharing options...
amwd07 Posted July 26, 2008 Author Share Posted July 26, 2008 Now no values are printing please help this is driving me mad $eachproduct = array(); $products = Db::getInstance()->ExecuteS('SELECT id_image, id_product FROM ps_image GROUP BY id_product'); foreach ($products as $key => $product) { $productID = $eachproduct[$products['id_product']]; $imageID = $eachproduct[$products['id_image']]; print "product ID = $productID image ID = $imageID <br/>"; } results from print below product ID = image ID = product ID = image ID = product ID = image ID = product ID = image ID = product ID = image ID = product ID = image ID = product ID = image ID = Quote Link to comment https://forums.phpfreaks.com/topic/116755-solved-simple-foreach-array-question/#findComment-600531 Share on other sites More sharing options...
Barand Posted July 27, 2008 Share Posted July 27, 2008 You can only use foreach on an array. Does your ExecuteS() method return an array? Quote Link to comment https://forums.phpfreaks.com/topic/116755-solved-simple-foreach-array-question/#findComment-600554 Share on other sites More sharing options...
amwd07 Posted July 27, 2008 Author Share Posted July 27, 2008 it returns the DB query which is later on defined as an array I have studied this for hours now & come up with slightly different code hope this one is closer. all I need to do is print the product_id & image_id to use later on in my script i realise foreach has to have an array so have defined a empty away first & then set the variables for the away within the loop it's almost 2am can anyone please point me in the right direction please ??? <?php /*** create DB sql query to prepare for the array ***/ $sql = db::getInstance()->execute('SELECT id_image, id_product FROM ps_image GROUP BY id_product'); /*** create empty array ***/ $carousel = array(); $i = 0; /*** loop over the array ***/ foreach( $carousel as $car ) { $carousel[$car['id_product']] = $car['id_image']; print_r($car); $i++; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/116755-solved-simple-foreach-array-question/#findComment-600602 Share on other sites More sharing options...
DarkWater Posted July 27, 2008 Share Posted July 27, 2008 Do me a favor. Show me the output of: print_r($sql); Quote Link to comment https://forums.phpfreaks.com/topic/116755-solved-simple-foreach-array-question/#findComment-600614 Share on other sites More sharing options...
amwd07 Posted July 27, 2008 Author Share Posted July 27, 2008 Sorry typo it's now showing Array ( [0] => Array ( [id_image] => 1 [id_product] => 1 ) [1] => Array ( [id_image] => 5 [id_product] => 2 ) [2] => Array ( [id_image] => 15 [id_product] => 5 ) [3] => Array ( [id_image] => 18 [id_product] => 6 ) [4] => Array ( [id_image] => 24 [id_product] => 7 ) [5] => Array ( [id_image] => 33 [id_product] => 8 ) [6] => Array ( [id_image] => 36 [id_product] => 9 ) ) Quote Link to comment https://forums.phpfreaks.com/topic/116755-solved-simple-foreach-array-question/#findComment-600617 Share on other sites More sharing options...
amwd07 Posted July 27, 2008 Author Share Posted July 27, 2008 thanks for all your help it seems I had things mixed a little this works now /*** create DB sql query to prepare for the array ***/ $eachproduct = array(); $products = Db::getInstance()->ExecuteS('SELECT id_image, id_product FROM ps_image GROUP BY id_product'); /*** loop over the array ***/ foreach ($products as $key => $product) { $productID = $product['id_product']; $imageID = $product['id_image']; print "product ID = $productID image ID = $imageID <br/>"; } Quote Link to comment https://forums.phpfreaks.com/topic/116755-solved-simple-foreach-array-question/#findComment-600621 Share on other sites More sharing options...
DarkWater Posted July 27, 2008 Share Posted July 27, 2008 That's what I figured. Use: foreach ($sql as $row) { echo "Image ID: " . $row['id_image']; echo "Product ID: " . $row['id_product']; } Quote Link to comment https://forums.phpfreaks.com/topic/116755-solved-simple-foreach-array-question/#findComment-600622 Share on other sites More sharing options...
amwd07 Posted July 27, 2008 Author Share Posted July 27, 2008 Your solution is much better but I thought I had to an array with foreach :-\ Quote Link to comment https://forums.phpfreaks.com/topic/116755-solved-simple-foreach-array-question/#findComment-600625 Share on other sites More sharing options...
DarkWater Posted July 27, 2008 Share Posted July 27, 2008 $sql is an array. ExecuteS returns an array. Quote Link to comment https://forums.phpfreaks.com/topic/116755-solved-simple-foreach-array-question/#findComment-600626 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.