I'm trying to read out some articles from my database using PHP OOP. But now I have these two errors that I really can't find any solution to.
Notice: Undefined variable: id in /Applications/MAMP/htdocs/web 2.0/projektet/class/pagesClass.php on line 57
Warning: Invalid argument supplied for foreach() in /Applications/MAMP/htdocs/web 2.0/projektet/index.php on line 31
My code
$page = new CMS();
$gp = $page->getPage();
foreach ($gp as $sp) {
//var_dump($sp);
echo "<div class='pub'>";
echo "<h4 class='pub-headline'>" . $sp['title'] . "</h4>";
echo "<article class='pub_art'>" . $sp['content'] . "</article>";
echo "<p class='pub_created'>" . $sp['created'] . "</p>";
echo "<p class='pub_created_by'>". $sp['writer'] ."</p>";
echo "<button class='show'>Show</button>";
echo "<button class='noshow'>Hide</button>";
echo "<button class='btn-like'>Like</button>";
echo "</div>";
}
// >>>>>>>>>>>>>>>>>>>>>>>> Function for reading out page
public function getPage() {
//$id = intval($id);
$sql = "SELECT * FROM pages
WHERE id = '$id' ";
$result = mysqli_query($this->db, $sql) or die('Fel vid SQL fråga GET PAGE');
if(mysqli_num_rows($result)) {
return mysqli_fetch_all($result, MYSQLI_ASSOC);
} else {
return false;
}
}