2017-06-16 16 views
0

問題があります。私はいくつかのデータを自分のデータベースから取り出したい。 私は2つのページのcategorie.phpを持っています。私は彼がデータベースからすべてを見せたいと思っています。私は2ページ目を持っています。ここに私のクラスがあります。私はcategorie.phpでforeachを試してみましたが、私がそれを行うならば、彼は1つの事をデータベースから4回同じであり、別のデータではないことを示しています。 下に私のコードを見ることができます。 私があなたを助けてくれることを願っています。foreachとクラスを使用してデータベースの外にあるものをすべて取得する方法

ありがとうございます。

これは私の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

OKです。 SQLリクエスト後のwhileでは、$this->id = $row->id;のようなインスタンス変数の値を適用しますが、この変数は次の行の値で書き換えられます。

あなたのSQL要求のためstatic機能を使用し、そのような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()); 
} 
関連する問題