dotolee Posted November 14, 2011 Share Posted November 14, 2011 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()."~"); } } } } Quote Link to comment Share on other sites More sharing options...
phporcaffeine Posted November 14, 2011 Share Posted November 14, 2011 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 Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted November 14, 2011 Share Posted November 14, 2011 Right, we need to see more of the code. it's not in the snip you provided. -Dan Quote Link to comment Share on other sites More sharing options...
dotolee Posted November 14, 2011 Author Share Posted November 14, 2011 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) Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 14, 2011 Share Posted November 14, 2011 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 Quote Link to comment Share on other sites More sharing options...
dotolee Posted November 14, 2011 Author Share Posted November 14, 2011 ok i will double check when i get a chance. thanks. Quote Link to comment 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.