2

I'm trying to load photos from the database via a for each loop to display each photo in a formatted div. however i keep receiving a undefined variable results and i am unsure why

this is my view code

  <?php echo form_open(base_url('index.php/smite/getimages/1/1'));?>
              <?php foreach($imgres as $imgdata){?>
                    <img class="img-box" src="<?php echo base_url("assets/img/".$imgdata['name'].".".$imgdata['type']);?>">
                    <p style="font-size:140%"><?php echo $imgdata['description']?></p>
              <?php }?>
              <?php echo form_close();?>

this is the controller code

class Smite extends CI_Controller {

private $data = "";
private $session_data = array();

public function __construct()
{
    parent::__construct();
    $this->load->helper(array('form', 'url'));
    $this->load->library(array('form_validation', 'session'));
    $this->load->model(array('users_model','comments_model','images_model'));
}

public function getimages($g1,$g2){
    $this->data['imgres'] = $this->images_model->get_images($g1,$g2);
    $this->load_page("smite");

}

furthermore in the controller is a function to load the page view and send it data

    private function load_page($page)
{
    $this->data['page'] = $page;
    $this->load->view('template/head');
    $this->load->view('template/nav',$this->data);
    $this->load->view($page."_view",$this->data);
    $this->load->view('template/footer');
    $this->load->view('template/login_register_modal');
    $this->load->view('template/scripts',$this->data);
}

the query runs fine and returns results when tested on the mysql server but im not sure why its not returning results to the view. these are the sever error messages

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: imgres

Filename: views/smite_view.php

Line Number: 28

followed by

A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: views/smite_view.php

Line Number: 28

3
  • This could be a duplicate of how to set variables in the controller so they are accessible in the view? stackoverflow.com/a/9446865/6208463 Commented May 4, 2017 at 23:10
  • Where are you loading your view and how are you passing data to view? Commented May 4, 2017 at 23:33
  • load_page must be a method in your controller. Please show that code. Commented May 5, 2017 at 3:34

1 Answer 1

2

change your controller like this

public function getimages($g1,$g2){
    $data['imgres'] = $this->images_model->get_images($g1,$g2);
    $this->load->view("smite",$data);

}
Then In get_images fuction in images_model write the query to get images data from database

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

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.