Welcome Guest, Not a member yet? Register   Sign In
views and data arrays
#1

[eluser]edhrx[/eluser]
I gather data and provide an array for a view using $row= $query->row_array(); . However this falls over if no row is returned. I now need to create an array with fieldnames in so the view will work ok.

Is it possible to get to the fieldnames of a table to do this....or is there some cute other type of function of which I am unaware :red:

best wishes Ed.
#2

[eluser]Michael Wales[/eluser]
You really have two questions here: one, how to gracefully degrade when what you expect to happen does not; and two, how to get the fieldnames from a table.

This code can easily be moved out into a model, but I am writing it now as if it's all in the controller.
Code:
// Run a query we know will fail
$slug = 'jkshfijhasdfhauisdhfaidfhuia';
$query = $this->db->getwhere('pages', array('slug'=>$slug));

// Now check to see if we actually have results, if not - return FALSE
if ($query->num_rows() > 0) {
  $this->data->page = $query->row();
} else {
  $this->data->page = FALSE;
}
$this->load->view('pages/view', $this->data);

In your view:
Code:
if ($page) {
    echo 'Title: ' . $page->title;
  } else {
    echo '<div class="error">There are no pages.</div>';
  }


Your second question - how do you return all of the fields from a table: this is answered within the first paragraph of the User Guide: Database - Field Metadata.

Code:
$pages_fields = $this->db->list_fields('pages');
  echo '<pre>';
  print_r($pages_fields);
  echo '</pre>';




Theme © iAndrew 2016 - Forum software by © MyBB