2
votes

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

1
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 - Jason Joslin
Where are you loading your view and how are you passing data to view? - shaggy
load_page must be a method in your controller. Please show that code. - DFriend

1 Answers

2
votes

change your controller like this

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

}