2017-06-16 27 views
0

我遇到問題。我想從我的數據庫中取出一些數據。 我有兩個頁面的categorie.php在這裏我希望他顯示數據庫中的所有內容。我有第二頁。這是我的課程。我試着在categorie.php上做一個foreach,但是如果我這樣做的話,比顯示他的數據庫有4倍的數據是相同的,而不是其他的數據。 下面你可以看到我的代碼。 我希望你能幫助我。如何使用foreach和class獲取數據庫外的所有內容

謝謝。

這是我categorie.php

<?php 
require_once '../app/functions/second.php'; 
require_once '../app/db/dbpassword.php'; 
require_once 'include/head.php'; 


if (isset($_GET['categorie']) && !empty($_GET['categorie'])) { 
    $id = $_GET['categorie']; 
    $dbh = new PDO("mysql:host=$host; dbname=$dbname;", $usernamedb, 
$passworddb); 
    $cate = new Categorie($dbh); 
    $cate->loadCate($id); 
    // $page->loadId($id); 
    $categorie = $cate->getCategorie(); 
    $titel = ucwords($categorie); 

?> 

<h2 class="center_h2"><?= $titel ?></h2> 
<?php foreach ($cate as $key) { 
     $titelart = $cate->getTitel(); 
    $beschrijving = $cate->getBeschrijving(); 
    $plaatje = $cate->getImage(); 
    $id = $cate->getId(); 
    var_dump($titelart); 
} ?> 


<?php 
} else { 
    echo "Dit bericht is verwijderd of is verplaats."; 
} 
require_once 'include/footer.php'; 
?> 

這是我的類頁

<?php 
    class Categorie { 
    protected $dbh; 

    public function __construct($new_dbh){ 
     $this->dbh = $new_dbh; 
    } 

    public function loadCate($cate){ 
     $query = $this->dbh->prepare('SELECT * FROM schilderijen WHERE categorie=?'); 
     $query->execute(array($cate)); 
     while ($row = $query->fetch(PDO::FETCH_OBJ)) { 
      $this->id = $row->id; 
     $this->categorie = $row->categorie; 
     $this->titel = $row->titel; 
     $this->beschrijving = $row->beschrijving; 
     $this->plaatje = $row->plaatje; 
     } 


    } 

    public function getId(){ 
     return $this->id; 
    } 

    public function getCategorie(){ 
     return $this->categorie; 
    } 

    public function getTitel(){ 
     return $this->titel; 
    } 

    public function getBeschrijving(){ 
     return $this->beschrijving; 
    } 

    public function getImage(){ 
     return $this->plaatje; 
    } 
} 
?> 

回答

0

好了,所以你必須在使用你的類的問題。在SQL請求之後的while中,應用實例變量的值,如$this->id = $row->id;,但該變量將用下一行值重寫。

使用static功能,爲您的SQL請求,並返回Categorie這樣的一個數組:

class Categorie { 
    protected $id, $categorie, $title, $beschrijving, $plaatje; 

    public function __construct($id, $categorie, $title, $beschrijving, $plaatje){ 
     $this->id = $id; 
     $this->categorie = $categorie; 
     $this->title = $title; 
     $this->beschrijving = $beschrijving; 
     $this->plaatje = $plaatje; 
    } 

    public static function loadCate($dbh, $cate){ 
     $query = $dbh->prepare('SELECT * FROM schilderijen WHERE categorie=?'); 
     $query->execute(array($cate)); 

     $res = array(); 
     while ($row = $query->fetch(PDO::FETCH_OBJ)) { 
      $res[] = new Categorie($row->id, $row->categorie, $row->titel, $row->beschrijving, $row->plaatje); 
     } 

     return $res; 
    } 

    public function getId(){ 
     return $this->id; 
    } 

    public function getCategorie(){ 
     return $this->categorie; 
    } 

    public function getTitle(){ 
     return $this->title; 
    } 

    public function getBeschrijving(){ 
     return $this->beschrijving; 
    } 

    public function getImage(){ 
     return $this->plaatje; 
    } 
} 

而且你可以用它這樣的:

$categories = Categorie::loadCate($dbh, $id); 
foreach($categories as $categorie){ 
    var_dump($categorie->getTitle()); 
} 
相關問題