>

Thursday, November 10, 2016

CI_gallery_model

<?php
class Gallery_model extends CI_Model
{
   
public function get_dropdown_list()
    {
          $this->db->from('login');
          $this->db->order_by('username');
          $result = $this->db->get();
 
          $return = array();

          if($result->num_rows() > 0)
 {
               foreach($result->result_array() as $row)
      {
 $return[$row['userid']] = $row['username'];
 /*this will return like this
 $userid=>$username
                  0=>admin
                  1=>user
 2=>jalpesh*/
               }
          }
               return $return;
   
}
public function gallery_data($userid)

    {   //$userid=$this->input->post('userid'); its not give require output
   $query = $this->db->where(['userid'=>$userid])
                      ->get('gallery');

      return $query->result();
}

public function insert_gallery($data)

    {   $query = $this->db->insert('gallery',$data);
      return $query;
}

public function getonerow_edit($galleryid)

    {   $this->db->where('galleryid',$galleryid);
$query=$this->db->get('gallery');
return $query->row();
}

public function getonerow_edit_login($userid)
{
$this->db->where('userid',$userid);
$query=$this->db->get('login');
return $query->row();
}

public function EditGalleryModel_insert($data)

    {
  $galleryid=$this->input->post('galleryid');
 
  $query = $this->db->where('galleryid',$galleryid);
           $this->db->update('gallery',$data);

//return $query;
}

public function gallery_photos($galleryid)

    {   //$userid=$this->input->post('userid'); its not give require output
   $query = $this->db->where(['galleryid'=>$galleryid])
                      ->get('album');

      return $query->result();
}

public function Photo_del()
{  
   //1. Below line fetch PhotoID from Url and store in variable
   $PhotoID=$this->input->get('PhotoID');
//2. Below code use for update total Photo in gallery i.e. fetch galleryid before delete photo
//this quey getch row which has match PhotoID
$query = $this->db->where('PhotoID',$PhotoID)
             ->get('album');

foreach ($query->result() as $row)
{
              $galleryid = $row->galleryid;
 $userid = $row->userid;
 $PhotoName = $row->PhotoName;
}

//finding Gallery Name
$queryG = $this->db->where('galleryid',$galleryid)
              ->get('gallery');
foreach ($queryG->result() as $row)
{
              $gname = $row->gname;
}

//finding User Name
$queryU = $this->db->where('userid',$userid)
              ->get('login');
foreach ($queryU->result() as $row)
{
              $username = $row->username;
}

//3.Delete requested photo
$PhotoID=$this->db->where('PhotoID',$PhotoID);
$this->db->delete('album',$PhotoID);

$path="./userlist/".$username."/".$gname."/".$PhotoName;
unlink($path);

//4.Find total Photo in Album table by specific User has gallery
        $sql = "SELECT * FROM `album` WHERE `galleryid` = '".$galleryid."'";
        $query = $this->db->query($sql);
//5.Find total Photo and store in variable
        $count = $query->num_rows();
//6. Update gallery table
$sql1 ="UPDATE `gallery` SET `totalphoto` = '".$count."' WHERE `galleryid` = '".$galleryid."'";
   $query1 = $this->db->query($sql1);

return $galleryid;

}

public function AllPhoto_del()
{  
    //PART 1
   $galleryid=$this->input->get('galleryid');
$query = $this->db->where('galleryid',$galleryid)
             ->get('album');

foreach ($query->result() as $row)
{
              $galleryid = $row->galleryid;
 $userid = $row->userid;
}

//finding Gallery Name
$queryG = $this->db->where('galleryid',$galleryid)
              ->get('gallery');
foreach ($queryG->result() as $row)
{
              $gname = $row->gname;
}

//finding User Name
$queryU = $this->db->where('userid',$userid)
              ->get('login');
foreach ($queryU->result() as $row)
{
              $username = $row->username;
}


   // PART 2
   //Below line fetch PhotoID from Url and store in variable
   $galleryid=$this->input->get('galleryid');

$galleryid=$this->db->where('galleryid',$galleryid);
$this->db->delete('album',$galleryid);

//PART 2 after delete entry from DB ,Delete all Images from perticular gallery Directory
   array_map('unlink', glob("./userlist/".$username."/".$gname."/*"));

// PART 3
//Update gallery table set totalphoto field to 0.
$galleryid=$this->input->get('galleryid');
$sql ="UPDATE `gallery` SET `totalphoto` = 0 WHERE `galleryid` = '".$galleryid."' ";
   $query = $this->db->query($sql);

/*CI style also working
$galleryid=$this->input->get('galleryid');
$query = $this->db->where('galleryid',$galleryid);
        $this->db->set('totalphoto',0);
$this->db->update('gallery');
        */

return $galleryid;

}
         
}