Jump to content

foreach is not looping through all the records


dotolee

Recommended Posts

hi there. i have a class that queries my db, creates an array objects representing a collection of the items returned.

when i pass the array back to the caller and then try to loop through the array, it keeps displaying the same record - i think it's the last record that is displayed

however, when i try the same code in the class, it prints out each unique record.

i tried doing a reset(myarray); on the caller but that dind't work either.

 

any suggestions?

here's the code to loop through the array:

 

                      if ( count( $myarray ) > 0 ) {

 

//reset($lineItem_array);

                          foreach ( $myarray as $lineitem ) {

        echo($lineitem->getName()."~");

                                echo($lineItem->getOId()."~");

                                echo($lineItem->getProduct()."~");

                                echo($lineItem->getPIP()."~");

                                echo($lineItem->getQuantity()."~");

  }

}

 

 

  }

  }

Link to comment
Share on other sites

We need to see the whole thing (class). It sounds like your class is passing back a string, not an array.  Being that the last record that is passed back, it would indicate that the loop is assigning string values and not adding to a stack.

 

$items = "MYSQL RESULT SET";
foreach ($items as $item) {

      $list = $item;

}
return $list;
//THIS WILL RETURN THE LAST ITEM IN $items

 

$items = "MYSQL RESULT SET";
foreach ($items as $item) {

      $list[] = $item;
      //IDEALLY, YOU WOULD NEED THIS LOOP BECAUSE YOU WANTED TO PERFORM AN OPERATION ON EACH ITEM, OTHERWISE YOU WOULDN'T NEED THE LOOP

}
return $list;
//THIS WILL RETURN AN ARRAY OF ALL $items

Link to comment
Share on other sites

i stepped out of the office so i don't have access to the code. but i can tell you that i think i am passing more than just one string from the database because from the caller, the first thing i do is take a count of the number of elements in the array, and it always matches the count from the database.

and then the foreach loop prints the last item in the array 235 times (the count(myarray); value)

 

Link to comment
Share on other sites

I think I see the problem. Check your variable names, specifically the "case" of the letters.

foreach ( $myarray as $lineitem ) {
    echo($lineitem->getName()."~");
    echo($lineItem->getOId()."~");
    echo($lineItem->getProduct()."~");
    echo($lineItem->getPIP()."~");
    echo($lineItem->getQuantity()."~");

 

The foreach loop, and the first usage, uses $lineitem, but the other usages of the variable have $lineItem

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.