-3

I would like to create a photo gallery, taking images from the database. I'm using codeigniter. Database

this is a static page located in views/pages/gallery.php

does anyone have any ideas?

2 Answers2

2

What you want is to query the database table, get the relevant fields, and return that to a view. In MVC, it looks something like this:

Model:

class Portfolio_model extends CI_Model {

    public function get_items() {
        $this->db->select('name, description, image');
        $this->db->order_by('date', 'DESC');
        $q = $this->db->get('tablename'); // your tablename here
        if ($q->num_rows() > 0) {
            return $q->result();
        } else {
            return null;
        }
    }

}

Controller:

class Portfolio extends CI_Controller {
    public function index() {
        $this->load->helper('html');
        $this->load->model('portfolio_model');
        $data['items'] = $this->portfolio_model->get_items();
        $this->load->view('portfolio', $data);
    }
}

View:

if (!is_null($items)) {
    foreach ($items as $item) {
        echo $item->name . '<br>';
        echo $item->description . '<br>';
        echo 'Image src: ' . base_url() . $item->image . '<br>';  // might need slash after base_url, don't remember
        echo img($item->image);
    }
} else {
    echo 'No items found!';
}
Alex
  • 9,215
  • 8
  • 39
  • 82
  • I paste all the code and I edit the table name but I have an error on 2nd row in view folder. Message: Undefined variable: items – user3465752 Mar 22 '18 at 22:44
  • 1
    Not possible items is defined here: `$data['items']`. Check again. Some other error must be occurring outside the scope of this code. – Alex Mar 22 '18 at 23:31
  • add print_r($data['items')); after $data['items'] in controller and print_r($items) before everything in the view. see if the array is there in both cases. – Alex Mar 22 '18 at 23:37
  • Message: Undefined variable: items Filename: pages/portfolio.php Line Number: 2 Backtrace: File: C:\xampp\htdocs\canoa\application\views\pages\portfolio.php Line: 2 Function: _error_handler File: C:\xampp\htdocs\canoa\application\controllers\Pages.php Line: 16 Function: view File: C:\xampp\htdocs\canoa\index.php Line: 315 Function: require_once – user3465752 Mar 23 '18 at 13:02
  • Again refer to my previous comments. The above code is solid. You must have made a mistake somewhere. Post your code on pastebin or something - I'm guessing you aren't using my code verbatim. – Alex Mar 23 '18 at 19:43
0

This worked for me :

Controller -

public function index(){
    $this->load->model('galleryModel');
    $data1['items'] = $this->galleryModel->get_items();
    $this->load->view('gallery', $data1);
}

Model -

public function get_items() {
    $this->db->select('*');
    $this->db->from('gallery');
    $query = $this->db->get();

    if($query->num_rows() != 0){
        return $query->result_array();
    }else{
        return false;
    }
}

View -

<?php
    foreach ($items as $item) {       
        $image_id = $item['image_id'];       
        $name = $item['name'];
        $category = $item['category'];
        $image = $item['image'];
        ?>

    <div class="tile scale-anm <?php echo $category; ?>">
        <a href="#"><img src="<?php echo $image; ?>" class="film-img-gallery" alt="" /></a>
    </div>

<?php } ?>