0

My simple foreach loop is working damn weird, please help me to solve this

here are my codes of controller

public function index()
{
    $this->load->model('file_list');
    $query = 'SELECT * FROM `files_data` WHERE file_extension = "jpg" and size > 0 LIMIT 0,10';
    foreach($this->db->query($query)->result() as $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 is 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 as new image //need to 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 is not working continious, its working for 1 record only for each refresh, then next record next refresh

4
  • Tip: you want to save more than 1 record however you are writing to the same variable and than overwriting it for the next record: $data['check'] Commented Aug 15, 2013 at 22:12
  • no it was not about setting up value, but about making thumbs here what i did now for learning i just make image generator code from it and kept it in foreach, then in foreach loop i kept printing name and making image but printing name work and only 1st image get created of mysql result :( Commented Aug 15, 2013 at 22:15
  • Yes only once because you are overwriting the data so it will only hold 1 entry. Why do you think 5 is the only entry on the variable of this code eval.in/43426 ? Commented Aug 15, 2013 at 22:18
  • whats $preview equal if the file doesn't exist and the ext isn't jpg, gif, png, mp3, apk, jar, zip, or 3gp? Commented Aug 15, 2013 at 22:31

2 Answers 2

1

as @prix was trying to tell you,

foreach($this->db->query($query)->result() as $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() as $file){
    $data['check'][] = $this->file_list->preview_maker($file->file_id, $file->file_full_path, $file->file_extension);
}

otherwise you just keep writing over the top of your value.

Also, read up on http://php.net/manual/en/control-structures.switch.php

Sign up to request clarification or add additional context in comments.

Comments

0

Actually this thing worked for me... i had problem about generating thumb

$config = array(
            'source_image' => 'stuff'.$file_full_path, //get original image
            'new_image' => 'preview/'.$file_id.'.jpg', //save as new image //need to create thumbs first
            'maintain_ratio' => true,
            'width' => 80,
            'height' => 80
                    );

            $this->image_lib->initialize($config);
            $resize = $this->image_lib->resize();
            $this->image_lib->clear(); //do whatever specified in config
            $preview = '/preview/'.$file_id.'.jpg';

i didnt use

$this->image_lib->clear(); 

last time

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.