2016-07-23 4 views
0

データベースからクラスへの配列を渡したいキャラクター、誰かが私を助けることができますか?OOP PHPで配列を渡すには?

マイコード:

クラスのデータベース

class Database { 

    public function DBCharactersMerchandise() { 

    $db = $this->connection(); 

    $uid = $_SESSION['id']; 

    $this->conn = $db->prepare("SELECT * FROM mercadoria WHERE cid = :mid"); 
    $this->conn->execute(array(':mid' => $uid)); 

    while($row = $this->conn->fetch(PDO::FETCH_ASSOC)){ 

     $array = [ 

      $row['type'], // here I have 'apple' and 'orange' 

     ]; 

     return $array; 

    } 

} 
} 

クラスのキャラクター

Class Characters { 

    public function getUserMerchandise() { 

     $db = new Database(); 
     $type = $db->DBCharactersMerchandise(); 

     foreach ($type as $value) { 

     echo $value; // I want echo 'apple' and 'orange' here 

     } 

    } 
} 

私はあなたが私が欲しいものを理解することができると思うので、私は、誰かが私を助けることができます私は感謝します。

+0

を返す準備ができrray;'あなたのクラスの 'Database' –

+0

あなたは[依存性注入](http://krasimirtsonev.com/blogに探してみてくださいすることができます/ article/Dependency-Injection-in-PHP-example-how-to-DI-create-your-own-dependency-injection-container)を参照してください。あなたのユースケースに適しているように思えます。 –

+0

'type'フィールド値だけが必要ですか? – RomanPerekhrest

答えて

0

あなたはwhileループで配列をロードし、その後、whileループが完了した後にそれを返す必要があります

class Database { 

    public function DBCharactersMerchandise() { 

     $db = $this->connection();  
     $uid = $_SESSION['id']; 

     $this->conn = $db->prepare("SELECT * FROM `mercadoria` WHERE `cid` = :mid"); 
     $this->conn->execute(array(':mid' => $uid)); 

     $array = []; 

     while($row = $this->conn->fetch(PDO::FETCH_ASSOC)){ 
      $array[] = $row['type']; 
     } 
     return $array;  
    } 
} 

またはより良いまだ、あなたは完全な結果aをロードするために->fetchAllを使用することができ、これは、クエリをスピードアップします、あなたが使用したいものだけを選択私はあなたが `リターン$配列を行うことができ、問題を発見

class Database { 

    public function DBCharactersMerchandise() { 

     $db = $this->connection();  
     $uid = $_SESSION['id']; 

     $this->conn = $db->prepare("SELECT `type` FROM `mercadoria` WHERE `cid` = :mid"); 
     $this->conn->execute(array(':mid' => $uid)); 

     $array = $this->conn->fetchAll(PDO::FETCH_ASSOC)){ 

     return $array;  
    } 
} 
-1

私が見ることができるあなたの質問ごとに戻り値はループの外にあるはずです。

+0

私はすでに複数の変数を持っているので、動かないでください。 –

+1

"複数変数 "とはどういう意味ですか? –

0

generatorsをお探しですか?

class Database { 

    public function DBCharactersMerchandise() { 

    $db = $this->connection(); 

    $uid = $_SESSION['id']; 

    $this->conn = $db->prepare("SELECT * FROM mercadoria WHERE cid = :mid"); 
    $this->conn->execute(array(':mid' => $uid)); 

    while($row = $this->conn->fetch(PDO::FETCH_ASSOC)){ 

     $array = [ 

      $row['type'], // here I have 'apple' and 'orange' 

     ]; 

     yield $array; 

    } 

} 
} 

class Characters { 

    public function getUserMerchandise() { 

     $db = new Database(); 

     foreach ($db->DBCharactersMerchandise() as $value) { 

     echo $value; // I want echo 'apple' and 'orange' here 

     } 
    } 
} 
0

のみ必要なフィールドから値を取得するためにPDO::FETCH_COLUMNフラグを使用して最適化されたバージョン:

class Database { 

    public function DBCharactersMerchandise() { 

     $db = $this->connection(); 
     $this->conn = $db->prepare("SELECT type FROM mercadoria WHERE cid = :mid"); 
     $this->conn->execute(array(':mid' => $_SESSION['id'])); 

     return $this->conn->fetchAll(\PDO::FETCH_COLUMN); 
    } 

} 

Class Characters { 

    public function getUserMerchandise() { 

     $db = new Database(); 
     $types = $db->DBCharactersMerchandise(); 

     if (!empty($types) && is_array($types)) { 
      foreach ($types as $type) { 
       echo $type; 
      } 
     } 
    } 

} 
関連する問題