title: How should I save gallery images for a profile into a database?
body: <p>I'm currently working on a gallery that allows you to upload to a screen and I want to know how should I save the images into a database so each selection of images is specific to each user. I have already completed a profile image upload that saves the link of the file for each user in a database, and now since Im trying to save a selection of images for each user I'm not sure how you would store an array of images for one ID. How could this be done? Currently my gallery database has 2 fields, user and galleryimage, but Im thinking this should become galleryimages since this is holding a selection of image names. My profile image before just stored the filename location as a varchar. Please point out to me how I should save a collection of images in one field, or if its not possible.</p>
  
  <p>Here is what I have so far:</p>
  
  <p>Controller:</p>
  
  <pre><code>class Gallery extends CI_Controller {
  
  
  
    function __construct()
     {
      // Call the parent construct
      parent::__construct();
  
      $this-&gt;load-&gt;model("profiles");
      $this-&gt;load-&gt;model("gal_model");
      $this-&gt;load-&gt;helper(array('form', 'url'));
  
     }
  
  
    function index()
    {
  
              //The commented out lines below signify what I have so far and when I uncomment them the image will no longer upload
              // At the moment the images just upload to the page
      //$this-&gt;load-&gt;library('upload');
      $username = $this-&gt;session-&gt;userdata('username');
  
  
  
  
      //$file_data = $this-&gt;upload-&gt;data();
  
      //$image = 'gallery_path'.$file_data['file_name'];
  
      //$data['image'] = 'gallery_path'.$file_data['file_name'];
  
      if($this-&gt;input-&gt;post('upload'))
      {
  
          $this-&gt;gal_model-&gt;do_upload();
  
      }
  
  
  
  
      //$this-&gt;gal_model-&gt;putGalleryImage($username, $image);
  
  
      $viewData['username'] = $username;
  
      $data['images'] = $this-&gt;gal_model-&gt;get_images();
  
      $this-&gt;load-&gt;view('shared/header');
      $this-&gt;load-&gt;view('gallery/galtitle', $viewData);
      $this-&gt;load-&gt;view('shared/nav');
      $this-&gt;load-&gt;view('gallery/galview', $data);
      $this-&gt;load-&gt;view('shared/footer');
  
  }
  
  }
  </code></pre>
  
  <p>Model:</p>
  
  <pre><code>class Gal_model extends CI_Model
  {
  var $gallery_path;
  var $gallery_path_url;
  
  function Gal_model()
  {
      parent::__construct();
  
      $this-&gt;gallery_path = 'web-project-jb/assets/gallery';
      $this-&gt;gallery_path_url = base_url().'web-project-jb/assets/gallery/';
  }   
  
  
  function exists($username)
  {
      $this-&gt;db-&gt;select('*')-&gt;from("gallery")-&gt;where('user', $username);
      $query = $this-&gt;db-&gt;get();
  
      if ($query-&gt;num_rows() &gt; 0)
      {
  
          return true;
          /*
           echo "user $user exists!";
          $row = $query-&gt;row();
          echo " and his profileimage is $row-&gt;profileimage";
          */
      }
  
      else
  
      {
  
          return false;
          //echo "no such user as $user!";
      }
  
  }
  
  
      //this function is aiming to put the galleryimages into the database
  function putGalleryImage($username, $image)
  {
  
  
      $record = array('user' =&gt; $username, 'galleryimage' =&gt; $image);
      //$this-&gt;session-&gt;set_userdata($image);
      if ($this-&gt;exists($username))
      {
          $this-&gt;db-&gt;where('user', $username)-&gt;update('gallery', $record);
  
  
      }
      else
      {
          $this-&gt;db-&gt;where('user', $username)-&gt;insert('gallery', $record);
  
      }
  
  }
  
  function do_upload ()
  {
     $config = array(
    'allowed_types' =&gt; 'gif|jpg|jpeg|png',
    'upload_path' =&gt; $this-&gt;gallery_path,
    'max_size'   =&gt; 10000     
              );
  
  
      $this-&gt;load-&gt;library("upload", $config);
      $this-&gt;upload-&gt;do_upload();
      $image_data = $this-&gt;upload-&gt;data();
  
      $config = array(
          'source_image'  =&gt; $image_data["full_path"],
          'new_image'     =&gt; $this-&gt;gallery_path. '/thumbs',
          'maintain_ration'   =&gt; true,
          'width' =&gt; 150,
          'height' =&gt; 100     
  
  
              );
  
      $this-&gt;load-&gt;library("image_lib", $config);
     $this-&gt;image_lib-&gt;resize();
  }
  
  
  function get_images()
  {
      $files = scandir($this-&gt;gallery_path);
      $files = array_diff($files, array('.', '..', 'thumbs'));
  
      $images = array();
  
      foreach ($files as $file){
          $images[] = array(
              'url'   =&gt; $this-&gt;gallery_path_url.$file,
              'thumb_url' =&gt; $this-&gt;gallery_path_url.'thumbs/'.$file
  
  
                  );
  
      }
  
      return $images;
  }
  
  
   }
  </code></pre>
  
  <p>My gallery view:</p>
  
  <pre><code>  &lt;?php if ( is_array($images) &amp;&amp; (count($images)&gt;0) ):
     foreach($images as $image): ?&gt;
     &lt;div class="thumb"&gt;
         &lt;a href="&lt;?php echo $image['url']; ?&gt;"&gt;
            &lt;img src ="&lt;?php echo $image['thumb_url']; ?&gt;"/&gt;
         &lt;/a&gt; 
         &lt;br&gt;
     &lt;/div&gt;
  &lt;?php endforeach; else:  ?&gt;
      &lt;div id = "blank_gallery"&gt;Please upload an Image&lt;/div&gt;
  &lt;?php endif; ?&gt;
  </code></pre>