1

I have the following piece of code that I am having problems resetting the $row array. It's a nested loop, $row works the first time, then is blank on additional loops.

/**Setup local variables with passed data**/
$ing = $this->getVariable('ingredients');
$row = $this->getVariable('unitdrop');
$unit= $this->getVariable('unit');

/**Start Displaying data**/
if (!$ing) {print("No ing");}
else
{while ($i = $ing->fetch_array())
{
/**Display $ing Data**/
    if (!$row) {print("No data row");}
    else
    {while($p = $row->fetch_array())
        {
        /**Display $row Data**/
        } 
    }
reset($row); // <-- Does not reset $row to first record
}
5
  • 1
    Just checking that you're closing your block comments /* Display $row data */ Commented Jul 23, 2012 at 21:49
  • Does $row return the same data with every loop? Why not iterate it once and store inside an array? Commented Jul 23, 2012 at 21:49
  • @Jamie - Yes the block comment should be closed, my code is different, I am using this as an example, can't share the actual code. Commented Jul 24, 2012 at 0:25
  • @mellamokb - Yes $row does return the same data each time. Thanks for the suggestion, I'll give it a shot. Commented Jul 24, 2012 at 0:36
  • @mellamokb - I appreciate the suggestion. However I find that option to be more coding then it's worth. Is there not a way to either reset the $row object or return it to the first record rather then creating a multidimensional array? Commented Jul 24, 2012 at 5:39

1 Answer 1

6

reset resets an array. $row is an object of class mysqli in your case. Use the method mysqli::data_seek instead (see the example in the link). Should be $row->data_seek(0); I guess.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, Thank you, Thank you!!! I spent hours last night reading and trying different combinations of reset, unset, close, mysql_data_seek and was going to embark on reprogramming hundreds of lines to change the way my app works.... $row->data_seek(0) worked like a charm! Thank you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.