Jump to content

[SOLVED] Simple foreach array question


amwd07

Recommended Posts

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  ???

Link to comment
https://forums.phpfreaks.com/topic/116755-solved-simple-foreach-array-question/
Share on other sites

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);

 

 

Now no values are printing  :o

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 =

 

 

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++;
}
?>

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 ) )

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/>"; 
  } 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.