Foreach loop not working in codeigniter model and controller both -
my simple foreach loop working damn weird, please me solve this
here codes of controller
public function index() { $this->load->model('file_list'); $query = 'select * `files_data` file_extension = "jpg" , size > 0 limit 0,10'; foreach($this->db->query($query)->result() $file){ $data['check'] = $this->file_list->preview_maker($file->file_id, $file->file_full_path, $file->file_extension); } //$this->load->view('viewtest', $data); //$this->home_footer(); }
and here model
public function preview_maker($file_id, $file_full_path, $extension){ if(file_exists('preview/'.$file_id.'.jpg')){ $preview = 'preview/'.$file_id.'.jpg'; } else { if($extension == 'jpg' || $extension == 'gif' || $extension == 'png'){ $config = array( 'source_image' => 'stuff'.$file_full_path, //get original image 'new_image' => 'preview/'.$file_id.'.jpg', //save new image //need create thumbs first 'maintain_ratio' => true, 'width' => 80, 'height' => 80 ); $this->load->library('image_lib', $config); //load library $this->image_lib->resize(); //do whatever specified in config $preview = '/preview/'.$file_id.'.jpg'; } elseif ($extension == 'mp3'){ $preview = '/preview/music.jpg'; } elseif ($extension == 'apk'){ $preview = '/preview/android.jpg'; } elseif ($extension == 'jar'){ $preview = '/preview/java.png'; } elseif ($extension == 'zip'){ $preview = '/preview/zip.jpg'; } elseif ($extension == '3gp' || $extension == 'mp4' || $extension == 'avi'){ $preview = '/preview/movie.png'; } } return $preview; }
the problem is, foreach loop not working continious, working 1 record each refresh, next record next refresh
as @prix trying tell you,
foreach($this->db->query($query)->result() $file){ $data['check'] = $this->file_list->preview_maker($file->file_id, $file->file_full_path, $file->file_extension); }
should read
foreach($this->db->query($query)->result() $file){ $data['check'][] = $this->file_list->preview_maker($file->file_id, $file->file_full_path, $file->file_extension); }
otherwise keep writing on top of value.
also, read on http://php.net/manual/en/control-structures.switch.php
Comments
Post a Comment