Hello I am trying to create a simple gallery CMS. All the pictures will be saved in uploads/ folder in the root of Codeigniter. Looks like my errors have to do with php programming. I am trying to print the pictures gallery to the screen:

controllers/upload.php

<?php

class Upload extends CI_Controller {

function gallery_show()
    {
     foreach($pictures as $pic)
        {
        $data['html'] .= '<img src="uploads/'.$pic['thumbname'].'">';
        }

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

}
?>

views/gallery.php

 <?php echo $html; ?>  

I receive this error message:

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: pictures

Filename: controllers/upload.php

Line Number: 42

A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: controllers/upload.php

Line Number: 42

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: data

Filename: controllers/upload.php

Line Number: 47

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: html

Filename: core/Loader.php(829) : eval()'d code

Line Number: 2

Line 42: foreach($pictures as $pic)

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

Anyone can help me solve this problem?

Recommended Answers

All 2 Replies

$pictures is undefined, where is it coming from?

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..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.