3
Contributors
2
Replies
29
Views
4 Years
Discussion Span
Last Post by veedeoo
0

Hi,

I think you are making codeIgniter more complex than it should. What is happening in your code is that you are trying iterate on an array that codeIgniter is not aware of.

If the filenames of the pictures are stored in the database, then you should build your model, if not then the controller method gallery_show should have to read the upload directory and then pass the results on the view.

if the filenames are in the database, then your model should look something similar to this..

Here is an example model class for the data stored in the database...

class Upload_model extends CI_Model {

## create a constructor method

public function __construct(){

## initiliaze the database connector from the library
$this->load->database();

}

## create the query method that will deliver the result to your controller

public function get_photos(){

## I will use the standard query here to prevent any further confusion.

$thisQuery = "SELECT * FROM photos";
$query = $this->db->query($thisQuery);

return $query->result_array(15);


}

}

The result_array method is capable of taking the limit parameters.

Now that we have our model, we create our controller

class Upload extends CI_Controller {


public function __construct() {
    parent::__construct();

    ## we need to load the url helper, just in case the router 
    ## becomes really excited in routing everything back to the application directory

    $this->load->helper("url");
    $this->load->model('upload_model');

}

public function index(){
    $data['photos'] = $this->upload_model->get_photos();
    $data['url'] = base_url();

     $this->load->view('gallery', $data); 


}

}   

Your view file gallery should look something similar to this.

<?php foreach ($photos as $photo): ?>

<img src="<?php echo $url.'/'.$photo['thumbname'];?>">

<?php endforeach ?>

If you are running PHP 5 >, then then the database query can be chained like

$this->db->select('thumbname')->from('photos');

Like the most common problem with frameworks, it becomes more and more server resource hog, when given a more complex query like multiple joins..

Edited by veedeoo: info added

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.