Hi
Could you please help me to fix the problem with HTML Table Class.
I'm trying to generate a HTML table from the results of DB query. My query runs in Model and then I'm passing the results of function to the controller by the return $query->result();. And unfortunately table->generate give's Error 500 in this case.
I did some tests and found out that probaly the problem in structure of the array which table->generate gets. As when I passing the results from the model to Controller with return $query->result_array(); table->generate build the table, but without headers.
So the questions are:
1. Why table->generate doesn't work in first case
2. How should I send the data to table->generate so it will build the table with headers.
Below you can find code I used and dumps of Arrays:
Model:
PHP Code:
<?php
class Model_getstats extends CI_Model {
function getData_CBsales(){
# CB Sale report
$query = $this->db->query("
SELECT ...
");
return $query->result();
}
Controller
PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class main extends CI_Controller {
public function stats_CBsales() {
$this->load->library('table');
$this->load->model('model_getstats');
$data['results'] = $this->model_getstats->getData_CBsales();
$this->load->view('table_view', $data);
}
}
View
PHP Code:
<?php
echo $this->table->generate($results);
?>
Dumps of $data array:
return $query->result_array(); (table was generated but without the headings)
PHP Code:
array(1) {
["results"]=>
array(1) {
[0]=>
array(3) {
["Day"]=>
string(10) "2014-11-25"
["Sales_Number"]=>
string(1) "8"
["Sales_Amount_USD"]=>
string(9) "2507.6000"
}
}
}
return $query->result(); Table generation failed with Error 500
PHP Code:
array(1) {
["results"]=>
array(1) {
[0]=>
object(stdClass)#18 (3) {
["Day"]=>
string(10) "2014-11-25"
["Sales_Number"]=>
string(1) "8"
["Sales_Amount_USD"]=>
string(9) "2507.6000"
}
}
}