2010-09-21 93 views
0

我是OOP的新手,我試圖掌握它。我想要做的是返回表中的所有數據。現在只有2行,但我不知道如何將所有數據放入數組,然後將其返回到$ results變量。試圖從一個類傳遞數組到另一個類

請幫忙。

<?php 
include('dates.php'); 

$events = new dates(); 
echo $events->getNumberEvents(); <-----returns the number of rows in the table (Works as expected) 

$results = $events->getRows(); <------Doesn't work. 

?> 

日期類

<?php 
/******************************************** 
* Class to connect and get dates from a database 
*********************************************/ 
require 'phpDatabaseClass.php'; 

class dates{ 

    private $db; 
    private $arr2 = array(); 

    /**************************** 
    * Constructor which creates the $db variable 
    ****************************/ 
    function __construct() 
    { 
     $this->db = new phpDatabaseClass(); 
    } 

    /**************************** 
    * Function which calls the database class and returns the number of rows 
    ****************************/ 
    public function getNumberEvents() 
    { 
     $sql = "select count(*) from events";   
     return $this->db->queryNumber($sql); 
    } 

    /**************************** 
    * Function which calls the database class and returns the actual rows 
    ****************************/ 
    public function getRows() 
    { 
     $sql = "select * from events"; 

     $this->arr2 = $this->db->queryString($sql); 

     foreach($this->arr2 as $key=>$val) 
     { 
      //$this->arr2 = $val; 
      //echo $val;  
     } 

    } 
} 
?> 

phpDatabaseClass

<?php 
/******************************************** 
* Class to connect and query a mysql database 
*********************************************/ 
require 'config.php'; 

class phpDatabaseClass { 

    private $db; 
    private $arr = array(); 

    /**************************** 
    * Constructor function which makes the connection to the database 
    ****************************/ 
    public function __construct() 
    { 
     try 
     { 
      $this->db = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME, DB_USERNAME, DB_PASSWORD); 
     } 
     catch(PDOexception $e) 
     { 
      echo 'Can\'t connect to database. Please contact the site administrator'; 
     } 
    } 

    /**************************** 
    * Function which returns the number of rows 
    ****************************/ 
    public function queryNumber($sql) 
    {       
     $stmt = $this->db->prepare($sql); 
     $stmt->execute(); 
     $resultsCount = $stmt->fetchColumn(); 

     return $resultsCount; 
    } 


    /**************************** 
    * Function which returns the data in the rows 
    ****************************/ 
    public function queryString($sql) 
    {       
     $stmt = $this->db->prepare($sql); 

     $stmt->execute(); 

     $results = $stmt->fetch(PDO::FETCH_ASSOC); 

     foreach($results as $key=>$val) 
     { 
      $this->arr = $val; 
      //echo $this->arr; 
     } 

     return array($this->arr); 
    } 
} 
?> 

回答

2
public function getRows() 
{ 
    $sql = "select * from events"; 

    $this->arr2 = $this->db->queryString($sql); 
    $return = array(); 
    foreach($this->arr2 as $key=>$val) 
    { 
     $return[$key] = $val; 
    } 
    return $return; 
} 

,如果你想在這個階段做任何事情,就是。爲了簡單地返回,你可以這樣做:

return $this->db->queryString($sql); 

爲了呼應信息:

foreach($results as $result) 
{ 

    echo $result->fieldname; 

} 

爲了呼應的一切需要:

foreach($results as $result) 
{ 
    foreach($result as $key => $value) 
    { 
     echo $key.': '.$value; 
    } 

} 

原因訪問作爲一個對象,而不是一個數組是mysqli將行作爲對象返回。

+0

如果我只是返回$ this-> db-> queryString($ sql),那麼我如何輸出所有的值?如果我在我的原始調用中這樣做:$ results = $ events-> getRows(); \t \t foreach($ result as $ key => $ val){\t \t \t echo $ val; \t}它只打印出第一行數據的最後一列。 – Catfish 2010-09-21 19:49:42

+0

編輯原始答案。 – Gazler 2010-09-21 19:53:04

+0

但是如果我不想在getRows函數中回顯,我只想將結果一直返回到原始調用? – Catfish 2010-09-21 19:55:05

相關問題