• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
HTML Table Class

#1
Sad 
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"
 
   }
 
 }

Reply

#2
Why don't you try to make it without using the table library ?
Its just 1 loop of data like:

Code:
<table>
   <tr>
       <th>Day</th>
       <th>Sales Number</th>
       <th>Sales Amount</th>
   </tr>
   <?php foreach($results as $entry): ?>
   <tr>
       <td><?php echo $entry->Day; ?></td>
       <td><?php echo $entry->Sales_Number; ?></td>
       <td><?php echo $entry->Sales_Amount_USD; ?></td>
   </tr>
   <?php endforeach; ?>
</table>
Best VPS Hosting : Digital Ocean
Reply

#3
How big is your result data? Maybe your server geht a timeout by printing the table html. For debuggin try to do a simple loop or the example from sv3tli0:

PHP Code:
<?php foreach($results as $entry): ?>
    <?php echo $entry->Sales_Number?><br />
<?php endforeach; ?>

I never used the table helper and i think he runs in memory error.

Reply

#4
Ok, so I solved the problem by returning function result from Model to Controller with  return $query->result and then just getting the header from this array and passing them to $this->table->set_heading() class:
PHP Code:
$data["query"] = $this->model_getstats->getData_CBsales();
 
$keysArray array_keys($data["query"][0]);
 
$this->table->set_heading($keysArray); 

Maybe somebody will find it useful:
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.