2011-03-25 133 views
0

有人可以請求幫助,出於某種原因,我可以在我的導出函數中回顯並設置文件和部門變量,但是當我嘗試在exportdata中調用它們時,它們爲空。OOP公共變量問題

Class customersController Extends baseController { 
    public $file; 
    public $department; 


    public function export() { 
     $this->registry->template->title = 'Ainsworthstudio - Export Customer Info'; 

     $this->registry->model->createModel('db', 'db'); 
     $this->registry->model->createModel('customers', 'customers'); 
     $this->registry->model->getModel('db')->addConnection("localhost", "sdgsdg", "sdg0", "sdg"); 
     $data = $this->registry->model->getModel('customers')->getDepartments(); 

     while(list($k, $v)=each($data)) 
      $$k = $v; 

     $this->registry->template->departments = $data; 
     $this->registry->template->show('customers_export'); 
    } 

    public function exportcheck() { 
     if(!empty($_POST['filename'])) { 
      $this->file = $_POST['filename']; 
      $this->department = $_POST['depart']; 

      echo "<div class='error_message'>Exported Successfully</div>"; 

      echo $this->file; 
      echo $this->department; 
      echo 'success'; 
     } else { 
      echo "<div class='error_message'>Wrong Username or Password</div>"; 
      exit(); 
     } 
    } 

    public function exportdata() { 
     return $this->file; 
     return $this->department; 

     echo $this->department; 
    } 
} 
+2

它會是非常不錯的,如果你能縮進它呼應$this->file

值的編碼和刪除不必要的代碼,因此願意幫助您的人們可以更輕鬆地閱讀它。其次,你有沒有設置這些屬性? – deceze 2011-03-25 05:48:46

+0

是的,清除代碼並顯示你如何使用它。 PS。它是一個CI框架,對嗎? – biakaveron 2011-03-25 06:10:39

+0

對不起,我正在構建的框架,我仍然必須簡化很多東西到功能我只是想讓所有工作 – 2011-03-25 17:21:35

回答

2

你的問題是在這裏:

public function exportdata() { 
    return $this->file; 
    return $this->department; 

    echo $this->department; 
} 

你回來的$this->file不呼應的價值。

如果嘗試echo $myCustomersController->exportdata()你會看到如果你想echo的值刪除return小號

public function exportdata() { 
    echo $this->file; 
    echo $this->department; 
} 
+2

yep。看起來一切都按需要分配,回聲只是永遠不會運行! – 2011-03-25 06:21:46

+0

我試着用回聲,沒有任何反應。我正在使用MVC,因此它們上的每個功能都是不同的頁面,這使得它無法工作> – 2011-03-25 11:56:33

+0

@Josh,是的。實際上,當您查看與exportdata()相對應的頁面時,並沒有調用'exportcheck()'的代碼,因此'$ this-> file'和'$ this-> department'值沒有設置。你需要做的是從'exportcheck()'中設置'$ this-> file'和'$ this-> department'的代碼,並將它分離出來,你可以從'exportcheck )'和'exportdata()'。 – xzyfer 2011-03-25 16:48:25