2015-01-17 97 views
2

我想傳遞一個數組到一個類的構造函數,並由於某種原因構造函數接收不同的數組比我發送的。 代碼構建2個陣列和發送它們:PHP數組沒有正確接收在類的構造函數

$exportArray=array(); 
      foreach($new->arr as $ar){ 
       $values[]=intval($ar[4]); 
       $dates[]=strtotime($ar[1]); 
       $exportArray[]=array($ar[5],$ar[0],$ar[2],$ar[3],$ar[1]); 
      } 
      $new->query="SELECT distinct c_name,date,m_name,t_amount,t_id FROM transport,customer,driver,material WHERE" 
        . " customer.c_id=transport.c_id AND material.m_id=transport.m_id AND transport.d_id=$name ORDER BY date DESC LIMIT $lim"; 
      //var_dump($new->query); 
      $new->again(); 
      foreach($new->arr as $ar){ 
       echo "<tr><td><a href='customers.php?c_name=$ar[0]'>$ar[0]</a></td><td>$ar[1]</td><td><a href='materials.php?m_name=$ar[2]'>$ar[2]</a></td><td>$ar[3]</tr>"; 
       //var_dump($ar); 
      } 
      ?> 
     </table> 
    <?php 
    if(isset($lim)&&$lim!="1"){ 
     $qr=new query("SELECT last FROM graph"); 
     $last=$qr->arr[0][0]; 
      var_dump($values); 
      var_dump($dates); 
     $graph1=new graphs($dates,$values,"Deliveries With Respect To Dates","driver"); 
     $graph1->getGraph("date"); 
     $qr->again(); 

類構造函數接收它們,並執行檢查:

class graphs{ 

    public $xtype=NULL; 
    public $ytype=NULL; 
    public $graph=NULL; 
    private $xs=array(); 
    private $ys=array(); 
    public $type=NULL; 

    public function __construct($arr1,$arr2,$title,$type){ 
     if(!is_array($arr1)||!is_array($arr2)){ 
      die("in order to see the graph, you need more than one result!"); 
     } 
     elseif(count($arr1)<=1||count($arr2)<=1){ 
      var_dump($arr1); 
      var_dump($arr2); 
      die("in order to see the RELEVANT graph, you need more than one result!"); 
     } 
     $this->title=$title; 
     $this->xs=$arr1; 
     $this->ys=$arr2; 
     $this->graph= new Graph(600,400,'auto'); 
     $this->graph->SetScale("textlin"); 
     $this->graph->SetShadow(); 
     $this->graph->title->Set($this->title); 
     $this->graph->title->SetFont(FF_ARIAL,FS_NORMAL,9); 
     $this->graph->xaxis->setTickLabels($arr1); 
     if($type=="driver"){ 
      $this->type=1; 
     } 
     elseif($type=="customer"){ 
      $this->type=2; 
     } 
     elseif($type=="material"){ 
      $this->type=3; 
     } 
    }//construct 

獲取這個瀏覽器上(第一2點轉儲是什麼我送,第二那些都是類傾銷的):

enter image description here

+1

請給我們的類定義,一切 – Rizier123

+0

完整劇本我無法複製 - 你肯定的是,代碼爲您提供了錯誤?因爲根據錯誤,你不知何故獲取字符串「customers」,它根本不存在於你的輸入數據中。 http://phpfiddle.org/main/code/szvx-6byt – andrewsi

+0

添加了更多代碼信息 – shnorkel

回答

1

,我認爲你的一些初始化的構造函數裏面,ES pecially您$arr1 & $arr2參數

$this->.. = $arr1; 
    $this->.. = $arr2 ; 

前應宣告你的if(!is_array($arr1)||!is_array($arr2)){..