Difficulty with an array of objects

Hello all,

I am working with an extended class and one of the functions in the class is as follows:

	public static function list_pages() {

		$sql  = "SELECT * ";
		$sql .= "FROM " . static::$table_name;
		$sql .= " ORDER BY position ASC";

		echo "<ul>";
		$pages = self::find_by_sql($sql);
		print_r($pages);

		foreach($pages as $page) {
			echo "<li";
			if($pages[Page]->id == $sel_page) { echo " class=\\"selected\\""; }
			echo "><a href=\\"content.php?page=".urlencode($page->id)."\\">{$page->menu_name}</a></li>";
		}
		echo "</ul>";
	}

find_by_sql($sql) returns an array of objects:

 [0] => Page Object
        (
            [id] => 1
            [menu_name] => HOME
            [position] => 1
            [visible] => 1
            [content] => This is the home page.  It should be the first page that is seen by a visitor to the site and should have the path of index.php.
        )

    [1] => Page Object
        (
            [id] => 2
            [menu_name] => BIO
            [position] => 2
            [visible] => 1
            [content] =>This is the about me page. 
    [2] => Page Object
        (
            [id] => 3
            [menu_name] => PORTFOLIO
            [position] => 3
            [visible] => 1
            [content] => This is the portfolio page
        )

    [3] => Page Object
        (
            [id] => 4
            [menu_name] => OTHER WORKS
            [position] => 4
            [visible] => 1
            [content] => This is a page to place projects not connected to a production.
        )

    [4] => Page Object
        (
            [id] => 5
            [menu_name] => CONTACT
            [position] => 5
            [visible] => 1
            [content] => This is the contact page.
        )

)

The trouble I am having is with the if statement in list_pages(). I have tried several different things to use the page id of each object for a comparison, but none have worked as I expected. I either get class=\“selected\”" added to every link or get null (found through var_dump). Is there a specific way that I should use a comparison when working with an array of objects? You can be assured that find_by_sql() does return an array of objects and the rest of the foreach loop works as expected. It is only the if statement that is giving me difficulties.

[QUOTE=guido2004;4948472]Where do you do the var_dump of $sel_page? In the list_pages() function?

I did the var_dump right after I set $sel_page at the top of content.php. I just did a second var_dump from within the function and it evaluated to NULL.

Silly mistake, but it drove me nuts. I need to pass $sel_page as an argument when I call the function right?

That worked. Thanks so much for the help. Sometimes you can look at code so often that you fail to notice the seemingly obvious.

Where do you do the var_dump of $sel_page? In the list_pages() function?

if(isset($_GET['page'])) { $sel_page = $_GET['page']; }

    else { $sel_page = ""; } 

These lines are not in the function, so if you don’t pass the value of $sel_page to the function, it can’t see it (out of scope).

But where does $sel_page come from?[/QUOTE]

if(isset($_GET['page'])) { $sel_page = $_GET['page']; }
	else { $sel_page = ""; }

The page submits to itself with $page->id passed as a key/value pair. $sel_page works fine and is exactly what I expect it to be. I did a little checking and $page->id in the if statement is the right thing, however, the if statement does not execute. Both $page->id and $sel_page return a string with the correct value when I do a var_dump on them. I am perplexed as to why the if statement is evaluating to false and therefore, not executing.

If $pages is an array of objects, then $page should contain an object, and the if should be

if ($page->id == $sel_page) 

But where does $sel_page come from?