为什么我的图库视图页面会出错?

时间:2012-12-18 01:09:20

标签: php codeigniter gallery

我目前正在使用codeigniter开发交互式配置文件中的图库,每个用户都可以上传自己的图片部分。目前我的视图页面正在抛出一条消息:未定义的变量:图像。尽管我的模型中已经有了一个get_images函数,但在我的视图中还有一个if语句,因此如果还没有上传图像,它将会理解不尝试显示图像。我该如何解决这个问题?我还试图存储上传到图库数据库中的任何图像,以便在每个用户的名字旁边显示他们上传的任何图像。到目前为止,这是我的代码:

控制器:

class Gallery extends CI_Controller {



function __construct()
{
    // Call the parent construct
    parent::__construct();

    $this->load->model("profiles");
    $this->load->model("gal_model");
    $this->load->helper(array('form', 'url'));

    $this->gallery_path = 'web-project-jb/assets/gallery';
    $this->gallery_path_url = base_url().'web-project-jb/assets/gallery/';

}

function upload()
{

    $config = array(

            'allowed_types' =>'gif|jpg|jpeg|png',
            'upload_path' => $this->gallery_path,
            'max_size' => 10000,
            'max_width' => 1024,
            'max_height' => 768);

    $this->load->library('upload', $config);

    $image_data = $this->upload->data();

    $config = array(
            'source_image'  => $image_data["full_path"],
            'new_image'     => $this->gallery_path. '/thumbs',
            'maintain_ration'   => true,
            'width' => 150,
            'height' => 100


    );


    $this->load->library("image_lib", $config);
    $this->image_lib->resize();

    $username = $this->session->userdata('username');

    if ( ! $this->upload->do_upload())
    {
        $error = array('error' => $this->upload->display_errors());

        $username = $this->session->userdata('username');


        $viewData['username'] = $username;

        $this->load->view('shared/header');
        $this->load->view('gallery/galtitle', $viewData);
        $this->load->view('shared/nav');
        $this->load->view('gallery/galview', $error, $viewData, array('error' => ' ' ));
        $this->load->view('shared/footer');



    }
    else
    {
        $file_data  = $this->upload->data();

        $image = $this->gallery_path.$file_data['file_name'];

        $data['image'] = $this->gallery_path.$file_data['file_name'];

        $this->username = $this->session->userdata('username');

        $this->gal_model->putGalleryImage($username, $image);

        $this->session->set_userdata($image);

        $viewData['username'] = $username;
        $data['gal_model'] = $this->gal_model->get_images($username);



        $username = $this->session->userdata('username');

        $this->load->view('shared/header');
        $this->load->view('gallery/galtitle', $viewData);
        $this->load->view('shared/nav');
        $this->load->view('gallery/galview', $data, $viewData);
        $this->load->view('shared/footer');


    }
}

function index()
{

    $username = $this->session->userdata('username');

    $this->load->library('upload');

    $data['profileimages'] = $this->gal_model->get_images($username);

    $file_data = $this->upload->data();

    $file_data['file_name'] = $this->gal_model->get_images($username);

    $image = $this->gallery_path.$file_data['file_name'];

    $data['image'] = $file_data['file_name'];


    $viewData['username'] = $username;


    $this->load->view('shared/header');
    $this->load->view('gallery/galtitle', $viewData);
    $this->load->view('shared/nav');
    $this->load->view('gallery/galview', $viewData, $data, array('error' => ' ' ));
    $this->load->view('shared/footer');

}

 }

型号:

 class Gal_model extends CI_Model
{
var $gallery_path;
var $gallery_path_url;

function Gal_model()
{
    parent::__construct();

    $this->gallery_path = 'web-project-jb/assets/gallery';
    $this->gallery_path_url = base_url().'web-project-jb/assets/gallery/';
}   


function exists($username)
{
    $this->db->select('*')->from("gallery")->where('user', $username);
    $query = $this->db->get();

    if ($query->num_rows() > 0)
    {

        return true;
        /*
         echo "user $user exists!";
        $row = $query->row();
        echo " and his profileimage is $row->profileimage";
        */
    }

    else

    {

        return false;
        //echo "no such user as $user!";
    }

}



function putGalleryImage($username, $image)
{


    $record = array('user' => $username, 'galleryimage' => $image);
    $this->session->set_userdata($image);
    if ($this->exists($username))
    {
        $this->db->where('user', $username)->update('gallery', $record);


    }
    else
    {
        $this->db->where('user', $username)->insert('gallery', $record);

    }


}


function get_images($username)
{

    $this->db->select('*')->from('gal_model')->where('user', $username);

    $files = scandir($this->gallery_path);
    $files = array_diff($files, array('.', '..', 'thumbs'));

    $images = array();

    foreach ($files as $file){
        $images[] = array(
            'url'   => $this->gallery_path_url.$file,
            'thumb_url' => $this->gallery_path_url.'thumbs/'.$file


                );

    }

    return $images;
}

  }

查看:

 <?php if ( is_array($images) && (count($images)>0) ):
   foreach($images as $image): ?>
   <div class="thumb">
       <a href="<?php echo $image['url']; ?>">
          <img src ="<?php echo $image['thumb_url']; ?>"/>
       </a>  
       <br>
   </div>
<?php endforeach; else:  ?>
    <div id = "blank_gallery">Please upload an Image</div>
<?php endif; ?>

再次感谢所有帮助人员

2 个答案:

答案 0 :(得分:1)

如果我在这里错了,请纠正我,但似乎你从来没有向视图分配$ images。您将$image$profileimages分配给index(),但我看到$images

答案 1 :(得分:0)

您应该在视图中将if更改为

if(isset($images) && is_array($images)) 

这样一来,如果未定义的$ images它将会抛出一个错误,它只会跳过所有这些