codenoob
codenoob

Reputation: 539

Foreach loop, what did I do wrong?

Please see below code

$template->customer = $rephome->getCustomer($user_id);

foreach($template->customer as $value){
       echo $value->customer_id;
   }

The first line of code will get result of a query. it was fetched as object.
the second line of code will echo out all of the customer id field from the query result. This works fine. it will echo out 1234567.... to however many id there is.

however if I change the code to like the one below, the echo will only get the first customer_id. ie. 1

$template->customer = $rephome->getCustomer($user_id);

foreach($template->customer as $value){
$something = $value->customer_id;
   }

echo $something;

so the question is what is the correct way of assigning the list of results to $something. so I can use $something as a list of ids to be used to run other querys.

in another word. I am trying to use result of first query to run a second query.

$template->customers = $rephome->getCustomer($user_id);

foreach ($template->customers as $value){
$template->orders = $rephome->getOrder($value->customer_id);
}

the above code give me the same result as echo. I will get all the customer id as intented but I will only get repeating order information that is assoicated with first customer id. instead of different orders associated to different customer id.

Upvotes: 0

Views: 56

Answers (3)

Erik van de Ven
Erik van de Ven

Reputation: 4975

Yes of course, cause you are overwriting the $something variable each iteration of the foreach loop and echo the result afterwards.

A way to solve this is by using an array:

$something = array();
foreach($template->customer as $value){
    $something[] = $value->customer_id;
}

This way you are adding a value to the array each iteration.

so the question is what is the correct way of assigning the list of results to $something. so I can use $something as a list of ids to be used to run other querys.

To use the ID's for another SELECT query, so it will only return records associated with the id's inside the array, you can do this for example:

$sql = 'SELECT * FROM `table` 
        WHERE `id` IN (' . implode(',', array_map('intval',  $something)) . ')';

Which will result in something like:

$sql = 'SELECT * FROM `table` WHERE `id` IN (1,2,4,6,7,10)';

Does this answer your question?

Upvotes: 1

Marc Anton Dahmen
Marc Anton Dahmen

Reputation: 1091

Try this within your loop:

$something[] = $value->customer_id;

and then outside your loop:

print_r($something);

Upvotes: 0

Tiago Luz
Tiago Luz

Reputation: 129

On this code:

foreach($template->customer as $value){
$something = $value->customer_id;
}

$something will be overrriten on each loop for a new value. So, on last line, your code will output the last value of resultset´s $value->customer_id.

Got it ??

So, use an array:

$something = array();
foreach($template->customer as $value){
 $something[] = $value->customer_id;
}

Upvotes: 0

Related Questions