0

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;
    } 


}
Nisse Engström
  • 4,636
  • 22
  • 26
  • 40
mackeemackee
  • 161
  • 2
  • 13
  • 1
    How are you passing an `$id` to your `getPage()` method? That's the source of your problem, if it's not Global, pass it through like so: `getPage($id){}` and then `$gp = $page->getPage(1);` – yardie Mar 04 '16 at 18:31
  • `public function getPage() {` must be `public function getPage($id) {` and when you are calling it then also you need to call it like `getPage($id);` also check `$id` must have some value – Anant Kumar Singh Mar 04 '16 at 18:33
  • Problem solved! cheers – mackeemackee Mar 04 '16 at 18:49

0 Answers0