Welcome Guest, Not a member yet? Register   Sign In
How to do this with table class
#1

[eluser]Bainzy[/eluser]
Hello everyone,

I'm a little stumped as to how to only return the cols I want from a database end show them with the table class here is what I have so far :

$this->table->set_heading(array('forums', 'topics', 'posts', 'lastPost'));
$data = $this->HomeModel->fetchAllRows('topics');
$this->table->generate($data);

now the problem here is that my database has 5 rows in it and I only want to show the 4 that corrospond to the headers, how can I do this ?

Regards
chris
#2

[eluser]Ben Edmunds[/eluser]
This is probably the best way to do it but there are many ways to accomplish this:

Code:
$columns = array(‘forums’, ‘topics’, ‘posts’, ‘lastPost’);
$this->table->set_heading($columns);
$rows = $this->HomeModel->fetchAllRows(‘topics’);

foreach ($rows as $key => $row) { //loop through our data
   foreach ($columns as $column) { //loop through the columns
      if (isset($row[$column])) {
         $data[$key] = $row[$column]; //save the data if it is in one of the columns we want
      }
   }
}
$data = array_values($data); //clean up the indexes
unset($rows); //clean up

$this->table->generate($data);
#3

[eluser]Bainzy[/eluser]
Can you explain how this code work because at the moment only the headers are showing but there is no data ?
#4

[eluser]Ben Edmunds[/eluser]
Make sure that $this->HomeModel->fetchAllRows(‘topics’); is returning a multi-dimensional array.

You might want to try a print_r() on the results of the method call to make sure it is returning the correct data as well.

Here's the code with comments:

Code:
$columns = array(‘forums’, ‘topics’, ‘posts’, ‘lastPost’); //the fields you want / the columns in your table
$this->table->set_heading($columns); //set the heading of the table from the columns array
$rows = $this->HomeModel->fetchAllRows(‘topics’); //fetch your rows

foreach ($rows as $key => $row) { //loop through each row in the array
   foreach ($columns as $column) { //loop through the columns
      if (isset($row[$column])) { //check to see if the column exists in the row array
         $data[$key] = $row[$column]; //save the data if it is in one of the columns we want - this will effectively filter out the columns you don't want
      }
   }
}
$data = array_values($data); //clean up the indexes of the array
unset($rows); //get rid of the rows array since we don't need it

$this->table->generate($data); //generate the table
#5

[eluser]Bainzy[/eluser]
hi again ben,

This still appears not to be working, here is my code for the fetchAllRows statement :

Code:
// Fetch all rows
    function fetchAllRows($table) {
        
        $query = $this->db->get($table);
        if($query->num_rows()>0){
            // Return result set as an associative array
            return $query->result_array();
        }
    }

If i use the code that i originally posted then this works so the data is definately there, my database column names also match that of the code you posted and still nothing i am just getting the headers coming up no problem however the data is missing.

Is there something i am missing, i am also running the template library so sending values to my template is a little bit more awkward so i have made some minor adjustments to your code. Here is the code i have in my 'Home' Controller


Code:
// Lets create the content
      $columns = array('forum', 'topics', 'posts', 'lastPost');
      $this->table->set_heading($columns);
      $rows = $this->HomeModel->fetchAllRows('topics');
      foreach ($rows as $key => $row) {
          foreach ($columns as $column) {
              if (isset($row['$column'])) {
                  $data['$key'] = $row['$column'];
              }
          }
      }
      $data =  array_values($data);
      
      unset($rows);
      $data['content'] = $this->table->generate($data);
      $this->template->write_view('content', 'content_tpl', $data);
#6

[eluser]Bainzy[/eluser]
Hey again, no need to answer my question now looks like i must have had a bug in the code as copied and pasted yours in and it worked even though i could not see anything wrong.

Anyway thankyou for this code it was very helpful and i think i have got my head around how it works.

Regard
Chris




Theme © iAndrew 2016 - Forum software by © MyBB