0

再次嗨 :) 我试图让每个用户都能够将他们自己的画廊版本上传到他们自己的个人资料中,但是当我这样做时,每个个人资料中都会出现相同的画廊。由于尝试执行此操作,不仅会出现相同的画廊,而且还会出现此错误:严重性:警告

消息:为 foreach() 提供的参数无效

我还打算将每个图库图像加载到数据库中每个用户的图库中,并且图像已经正确存储在我的文件上传目录中

这是我的画廊控制器:

class Gallery extends CI_Controller {



function index()
{
    $username = $this->session->userdata('username');
    $image = $this->session->userdata('images');

    $this->load->model("gal_model");

    if($this->input->post('upload'))
    {

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

    }




    $viewData['username'] = $username;

    $data['images'] = $this->gal_model->get_images();
    //var_dump($data);

    $this->load->view('shared/header');
    $this->load->view('gallery/galtitle', $viewData);
    $this->load->view('shared/nav');
    $this->load->view('gallery/galview', $data);
    $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 do_upload ()
{
   $config = array(
  'allowed_types' => 'gif|jpg|jpeg|png',
  'upload_path' => $this->gallery_path,
  'max_size'   => 10000     
            );


    $this->load->library("upload", $config);
    $this->upload->do_upload();
    $image_data = $this->upload->data();


    $image = 'gallery_path'.$image_data['file_name'];

    $data['image'] = 'gallery_path'.$image_data['file_name'];


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

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

    $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();
}


function get_images()
{
    $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;
}



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);

    }

   }

   }

这是我的画廊视图:

   <div id="gallery">

   <?=if (isset($images)): 
   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; ?>

   <?=form_open_multipart('gallery');?>
    <?=form_upload("userfile");?>
    <?=form_submit('upload', 'Upload')?>
    <?=form_close();?>

再次感谢大家的帮助,我只需要知道正确的方法就可以了:)。

4

1 回答 1

0

Invalid argument supplied for foreach()如果您尝试对不是数组的变量执行 foreach,则可能会发生这种情况。要删除该警告消息,请将图库视图更新为:

<div id="gallery">
   <?=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;?>
    <?php else:  ?>
        <div id = "blank_gallery">Please upload an Image</div>
    <?php endif; ?>
<div>   

我不熟悉 CI,但是$images从哪里获取值?

于 2012-12-11T02:31:55.557 回答