0

我正在尝试查看从数据库中获取的图像文件,但我不知道为什么在加载视图时会得到一个未定义的变量。

几乎我只是想查看存储在我的数据库图像中的图像名称。任何帮助都会非常感谢。

这是我在浏览器中查看时收到的错误

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>
4

1 回答 1

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 文档以获取更多信息。

于 2013-02-21T15:14:23.763 回答