1

This is where the code is at right now:

<ul>
    <?php
    while ($themes = $model->getAllThemes()) {
        echo '<li>' . $themes->id . ' ' . $themes->title . '</li>';
    }
    ?>
</ul>

The $themes array looks like this:

Array
(
    [0] => Theme Object
        (
            [id] => 11
            [title] => NewTheme
        )

    [1] => Theme Object
        (
            [id] => 12
            [title] => FakeTheme
        )
)

When I test it, it goes in an infinite loop pretty much, so I'm guessing it doesn't know how long the array is? I'm trying to avoid having to count the items in the array and I'm pretty sketchy on foreach loop syntax.

2
  • 1
    what is $model->getAllThemes ? is it a property, a method, a non-existing property in combination with __get() magic functions? At first sight it looks like you are looping for as long $model->getAllThemes is not false/0/null...probably forever. Could you provide the code behind getAllThemes? Commented Jun 28, 2011 at 20:14
  • sorry i was missing the () on it. it's a function that returns everything from the themes table Commented Jun 28, 2011 at 20:16

4 Answers 4

9

Since getAllThemes is not an iterator, it should always return non-null/non-false, thus causing an infinite loop. You should be using a foreach loop instead:

<?php
foreach ($model->getAllThemes as $themes) {
    echo '<li>' . $themes->id . ' ' . $themes->title . '</li>';
}
?>
2
  • i've used while loops for parsing mysql_result_arrays and this seems the same thing at the root of it (iterating an array).. what's the main difference that sets this apart? Commented Jun 28, 2011 at 20:25
  • Since getAllThemes is a property that isn't being modified each time you access it, it won't change to being a NULL value. All the data is already stored in getAllThemes and doesn't need to be pulled from a resource, such as a MySQL database. Commented Jun 28, 2011 at 20:27
7

You aren't using a foreach loop. Do this:

foreach($model->getAllThemes() as $theme) {
    echo '<li>' . $theme->id . ' ' . $theme->title . '</li>';
}

Note that inside the loop I am using $theme as opposed to $themes. This is because the foreach loop takes the top item off the array and assigns it to the variable that comes after that as. In this case as $theme.

What you are currently doing is using a while loop, with an assignment inside.

while ($themes = $model->getAllThemes())

Another way to write that, which might make your problem more apparent, is this:

while (($themes = $model->getAllThemes()) != false)

Or, more clearly, this:

$themes = $model->getAllThemes();
while($themes != false) 

Since $themes is populated by a valid array, it doesn't register in the loop as empty or false. It is always true. You're not actually cycling through any of the themes. You're just checking to see if the themes array exists, and if it does keep cycling. It always exists, hence infinite loop.

An actual foreach loop will run through each theme in your array and allow you to do something to it. It will work more like what you expect in your while loop's code.

1

You're looking for a foreach loop:

 foreach ($model->getAllThemes() as $themes) {
    echo '<li>' . $themes->id . ' ' . $themes->title . '</li>';
} 

Also looks like getAllThemes is a method, not a property?

1

Reading your piece of code....

while ($themes = $model->getAllThemes){
  // loop stuff
}

in this sentence, you are doing the following steps

  1. assign to $themes the array of themes (the two themes)
  2. evaluate if the assignment is true (yes, it is).
  3. do the loop stuff (inside code)
  4. back to step number one.

there is actually a infinite loop in your code.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.