0

Situation/Problem

I'm pulling all records form a table. I'm passing the returned data to my view. Num_rows says 5 but the result set is empty although there are records in the table.

controller

public function index()
{
     $this->load->model('Accessmodel', 'access');

     $data['query'] = $this->access->get_access_roles();

     $this->load->view('common/header');
     $this->load->view('admin/access/index', $data);
     $this->load->view('common/footer');
}

model

class Accessmodel extends Cruddy {
    function __construct() 
    {
        parent::__construct();
        self::$table = "EmployeeRoleAccess";
    }

    function get_access_roles()
    {
        return $this->db->get(self::$table);
    }
}

Relevant portion of cruddy

class Cruddy extends CI_Model {

protected static $table;

function __construct()
{

    parent::__construct();

}

function get_items()
{

    $this->db->from(self::$table);

    return $this->db->get();

}

view

<div class="column-left">
    <div class="sub-section-left">
        <?php include_once APPPATH . 'views/admin/menu.php'; ?>
    </div>
    <div class="sub-section-left shadows">
        <?php include_once APPPATH . 'libraries/minical.php'; ?>
    </div>
    </div>
    <div class="column-right">  
    <div class="sub-section-right">
        <h1>Employee Role Access Templates<input class="create right" type="button" name="" value="New Access Role" onClick=""></h1>
        <?php var_dump($query) ?>
    </div>
</div>

var_dump of query

object(CI_DB_mysql_result)#19 (8) {
  ["conn_id"]=>resource(11) of type (mysql link persistent)
  ["result_id"]=>resource(19) of type (mysql result)
  ["result_array"]=>array(0) { }
  ["result_object"]=>array(0) { }
  ["custom_result_object"]=>array(0) { }
  ["current_row"]=>int(0)
  ["num_rows"]=>int(5)
  ["row_data"]=>NULL
}
Mathew Thompson
  • 55,877
  • 15
  • 127
  • 148
deadbabykitten
  • 159
  • 3
  • 14

1 Answers1

2

I forgot that result is an object. I was trying to access it like an array

echo $Result['Name'];

which is why I used var_dump($Result) to see what was in the variable. When I saw result_array was empty I started to think it wasn't returning data... this was NOT the case. I just had not returned data using result_array so OF COURSE IT WAS EMPTY! You can specify which way to return data with codeigniter, the default being through an object.

Below is an example of the code I used in the view and it does work. I have to admit, I had not had any caffeine when I wrote this code this morning.... my apologies

if($query->num_rows() > 0) { 
    foreach($query->result() as $Result) {
        echo $Result->Name . "<br />\n";
    }
} else { 
    echo "There are no results!";
} 
deadbabykitten
  • 159
  • 3
  • 14