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.