-1

I have a function that has a query and a foreach loop:

$sql = "SELECT * FROM explore WHERE id = $id";
$object = $this->db->select($sql);

foreach($object as $val){
    $results = array('id'=>$val->id, 'from_id'=>$val->from_id);
    $this->result[] = $this->notify($results);  
    }

return $results;

The issue here is that if I return $object I get 2 records:

Array
(
[0] => stdClass Object
    (
        [from_id] => 6
        [id] => 3
    )

[1] => stdClass Object
    (
        [from_id] => 6
        [id] => 1
    )

)

and return $results has 1 record:

Array
(
    [id] => 1
    [from_id] => 6
)

Also if I return $this->result;, $this->result[] = $this->notify($results); does run twice but uses the same record twice returned by $results instead of using the 2 records from $object

Hope you guys can understand my issue.

ps: I am using the zend framework

Any ideas?

Edit: notify is a function in another class

5
  • Where are the variables $getResults and $this->result coming from, and what is $this->notify? What do you want the resulting array to look like?
    – gen_Eric
    Dec 27, 2011 at 21:23
  • 3
    Sorry, where exactly did you assign a value to $getResults?
    – DaveRandom
    Dec 27, 2011 at 21:23
  • my bad, i was talking about $results, please rank this question up, it is valid question, if we look over some of my typing mistakes Dec 27, 2011 at 21:30
  • 1
    What is your concrete question? What you describe as issue(s), is just telling that your code works as it should. Obviously it does not what you expect, so what do you expect it to do? What do you want to do?
    – hakre
    Dec 27, 2011 at 21:35
  • the issue with return $this->result is that i get 2 results, but they use the first result from $results instead of using both results from the array. $results has 2 results in the array, 0 and 1. return $this->result will use 0 twice Dec 27, 2011 at 21:49

2 Answers 2

2

$getResults isn't being set anywhere in your code, visibly. It looks like you ought to be returning $this->result instead, as that's where the results are being stored. That's my best guess given the amount of code you've given us. If you can provide more code, I can further update my answer if it doesn't work for you.

Given your comment, update your code to this:

$sql = "SELECT * FROM explore WHERE id = $id";
$object = $this->db->select($sql);

foreach($object as $val){
    $results = array('id'=>$val->id, 'from_id'=>$val->from_id);
    $this->result[] = $this->notify($results);  
    }

return $this->result;

If you're only returning $results, it'll be filled with the last item, not every item.

3
  • the issue with return $this->result; is that i get 2 results, but they use the first result from $resultsinstead of using both results from the array. $results has 2 results in the array, 0 and 1. return $this->result; will use 0 twice Dec 27, 2011 at 21:48
  • @Patrioticcow: Then that sounds like an issue in your code elsewhere. What does $this->notify do?
    – Cyclone
    Dec 27, 2011 at 21:54
  • $this->notify returns some other content Dec 27, 2011 at 22:08
2

The reason you have a problem is that you redefine the $results array on each iteration of the foreach loop, instead of adding an element to it.

This is where the problem is:

//...
foreach($object as $val){
    $results = array('id'=>$val->id, 'from_id'=>$val->from_id);
//...        ^ you are reassigning the whole value of $results

Do this instead:

//...
$results = array();
foreach($object as $val){
    $results[] = array('id'=>$val->id, 'from_id'=>$val->from_id);
//...       ^^ note the array push instead of complete reassign

EDIT as @Cyclone has rightly pointed out, the above answer is in fact wrong. You need to be doing one of two things:

  • returning $this->result instead of $results
  • populating the $results variable with the processed data, instead of $this->result.

Essentially, you either need to change:

return $results;

to:

return $this->result;

Or, change the loop to this:

$results = array();
foreach($object as $val){
    $results[] = $this->notify(array('id'=>$val->id, 'from_id'=>$val->from_id));  
}

Which one you want to do depends on whether you actually need $this->result - i.e. whether you need to keep the results in the object after this code has executed.

2
  • -1, the results array should be pushed through the $this->notify function with each pass, as you can see in OP's code. Fix that and I'll change my vote, fairly.
    – Cyclone
    Dec 27, 2011 at 21:38
  • @Cyclone You are absolutely right and I have edited my answer and +1ed yours. Didn't pay complete attention to the whole code, always a mistake...
    – DaveRandom
    Dec 27, 2011 at 21:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.