2013-02-21 65 views
0

我想查看我從數據庫中獲取的圖像文件,我不知道爲什麼當我加載視圖時出現未定義的變量。使用Codeignitor從數據庫表中查看文件。

很多我只是想查看存儲在我的數據庫圖像中的圖像名稱。 任何幫助將是偉大的謝謝你。

下面是瀏覽器

A PHP Error was encountered 
Severity: Notice 
Message: Undefined variable: imagename 
Filename: layouts/home.php 
Line Number: 8 
A PHP Error was encountered 
Severity: Warning 
Message: Invalid argument supplied for foreach() 
Filename: layouts/home.php 
Line Number: 8 

這裏觀看,當我收到的錯誤是我的控制器

class Home extends CI_Controller { 

function __construct() 
{ 
    parent::__construct(); 
    $this->load->library('upload'); 

} 
    function index() { 
    $this->load->model('index_model'); 
    $foldername['foldername'] = $this->index_model->getFoldernames(); 
    $imagename['imagename'] = $this->index_model->getImagenames(); 

    $this->load->view('layouts/home', $foldername, $imagename, array('error' => ' ')); 
    } 

function create() { 
    if(array_key_exists('createFolder',$_POST)){ 
    $data = array(
     'folderName' => $this->input->post('folderName') 
    ); 
    $data = str_replace(' ', '_', $data); 
    $this->index_model->createFolder($data); 
    } 
    $this->foldercreated(); 
} 

function delete() { 
    $this->load->model('index_model'); 
    $foldername['foldername'] = $this->index_model->getFoldernames(); 
    $this->load->view('layouts/delete', $foldername); 
} 

function deleteFolder() { 
    if($this->input->post('checkbox') !== false) { 
    $checkbox = $this->input->post('checkbox'); 
    $this->index_model->deleteFolder($checkbox); 
    } 
$this->folderdeleted(); 
} 

function foldercreated() { 
    $this->load->view('partials/foldercreated'); 
} 

function folderdeleted() { 
    $this->load->view('partials/folderdeleted'); 
} 

function do_upload() { 
    $config['source_image'] = $this->upload->upload_path.$this->upload->file_name; 
    $config['upload_path'] = './uploads/'; 
    $config['allowed_types'] = 'gif|jpg|png'; 
    $config['max_size'] = '0'; 
    $config['max_width'] = '0'; 
    $config['max_height'] = '0'; 
    $this->upload->initialize($config); 
    $this->load->library('upload', $config); 

    if (!$this->upload->do_upload('userfile')) 
    { 
     // echo '<p>IMAGE NOT WORKING</p>'. '<br/><p>' . $this->upload->display_errors() .  '</p>'; 
    $uploadError = $this->upload->display_errors(); 
    $this->load->view('partials/upload_error', $uploadError); 
    } 
    else 
    { 
    // $imagefile = array('upload_data' => $this->upload->data()); 
    $upload_data = $this->upload->data(); 

    $db_data = array('imageName' => $upload_data['file_name']); 
    $this->index_model->addImage($db_data); // replace the tablename 

    $this->load->view('partials/upload_success', $db_data); 

    } 
} 
} 

這裏是我的模型

function getFolderNames() { 
    $this->db->order_by('id', 'DESC'); 
    $this->db->select('folderName'); 
    $q = $this->db->get('senior'); 

    if($q->num_rows() > 0) { 
     foreach ($q->result() as $row) { 
      $data[] = $row; 
     } 
     return $data; 
    } 
    } 

    function getImagenames() { 
    $this->db->order_by('id', 'DESC'); 
    $this->db->select('imageName'); 
    $query = $this->db->get('images'); 

    if($query->num_rows() > 0) { 
     foreach ($query->result() as $row) { 
      $data[] = $row; 
     } 
    return $data; 
    } 
    } 

這裏是我的看法

<div id="imagefiles"> 
    <?php 
     foreach ($imagename as $img) { ?> 
      <div><?php echo $img->imageName; ?></div> 
    <?php } ?> 
</div> 
+0

請查看如何[將數據發送到您的視圖](http://ellislab.com/codeigniter/user-guide/general/views.html)。 '$ this-> load-> view('layouts/home',$ foldername,$ imagename,array('error'=>''));'不發送'$ imagename'。 – 2013-02-21 14:45:22

回答

0

您沒有正確地將數據傳遞給您的視圖。

你有這樣的:$this->load->view('layouts/home', $foldername, $imagename, array('error' => ' '));

你的第二個參數應該是你想傳遞給您的視圖的數據的數組。試試這樣的:

$data['foldername'] = $this->index_model->getFoldernames(); 
$data['imagename'] = $this->index_model->getImagenames(); 

$this->load->view('layouts/home', $data); 

請檢查CodeIgniter documentation瞭解更多信息。

相關問題