CodeIgniter Forums
How to fetch multy records - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: How to fetch multy records (/showthread.php?tid=53996)



How to fetch multy records - El Forum - 08-17-2012

[eluser]Ayelius Maximous[/eluser]
Hi guys. I have a table to store Categories and a page to add category. in the add category page I have to display categories too.

Controller :
Code:
class Products extends CI_Controller {
function __construct(){
  parent::__construct();
}
function Categories ($message=NULL){
  $data = array(
  'categories' => $this->Categories_Model->get_categories(),
  'category-title' => $this->input->post('category-title'),
  'description' => $this->input->post('description'),
  'parent' => $this->input->post('parent'),
  'message' => $message
  );
  $this->load->library('form_validation');
  $this->form_validation->set_rules('category-title', 'Category Title', 'required');
  $this->form_validation->set_rules('description', 'Description', 'required');
  if ($this->form_validation->run() == FALSE) {
   $this->load->view('Admin/Header');
   $this->load->view('admin/template');
   $this->load->view('admin/Categories', $data);
   $this->load->view('Admin/Footer');
  }
  else {
   if ($this->Categories_Model->add()) {
   $this->load->view('Admin/Header');
   $this->load->view('admin/template');
   $this->load->view('admin/Categories', $data);
   $this->load->view('Admin/Footer');
   }
  
  
}
}
}

Model :

Code:
class Categories_Model extends CI_Model {
function __construct() {
  parent::__construct();
}
function get_categories() {
  $query = $this->db->get('categories');
  if ($query->num_rows() > 0)
  {
  $te = $query->num_rows();
foreach ($query->result_array() as $row){

$row = $query->row();
    echo $row->title;
    echo $row->description;
}
    return $row;
} else
   return "<option value='0'>Parent</option>";
}

View :

Code:
<table cellspacing=0 cellpadding=9 width='50%' >
<tr>
    <th>Category</th>
  <th>Description</th>
  <th>Amount</th>
   </tr>
<tr>
  <td class='td-mid'><b>&lt;?php echo "$categories->title"; ?&gt;</b></td>
  <td class='td-mid'>&lt;?php echo "$categories->description"; ?&gt;</td>
  <td class='td-mid'>&lt;?php echo "$categories->amount"; ?&gt;</td>
</tr>
</table>

it just display one record, not all of them.

what have I to do ?


How to fetch multy records - El Forum - 08-17-2012

[eluser]DarkManX[/eluser]
Create a controller which has 2 Methods to display all categories & to add new category. You can add a form to the displaying view for new categories, that will send a request to the adding method. After the validation and insert into db you just redirect to the display site.


How to fetch multy records - El Forum - 08-17-2012

[eluser]Ayelius Maximous[/eluser]
my problem is how to fetch more than one record.


How to fetch multy records - El Forum - 08-17-2012

[eluser]DarkManX[/eluser]
Well, thats the smallest problem you got i think. Sad I guess you didnt understand the mvc-concept. result_array() returns you all the results in an array. You dont output any stuff in der model btw. The model suposed to give data from the database to the controller that requested certain data. This controller will give the information to the view (stil no html output happened) and finally the view will output the whole stuff.


How to fetch multy records - El Forum - 08-17-2012

[eluser]CroNiX[/eluser]
You already are fetching more than 1 record. Just after you echo out your title/description in that loop, you return $row, which is only a single result created while looping over your result set.

Try returning the whole result set instead.
Code:
return $query->result_array();



How to fetch multy records - El Forum - 08-17-2012

[eluser]Ayelius Maximous[/eluser]
thanks CroNiX, I did it and I got error :


A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: Admin/Categories.php

Line Number: 14

what have I to write for controller and view ?


How to fetch multy records - El Forum - 08-17-2012

[eluser]CroNiX[/eluser]
Because when you return result_array(), it's an array, not an object.

Change it to return $query->result() if you want an object.

also, in your view, you will want to loop over $categories
Code:
&lt;?php foreach($categories as $category): ?&gt;
<tr>
  <td class='td-mid'><b>&lt;?php echo $category->title; ?&gt;</b></td>
  <td class='td-mid'>&lt;?php echo $category->description; ?&gt;</td>
  <td class='td-mid'>&lt;?php echo $category->amount; ?&gt;</td>
</tr>
&lt;?php endforeach; ?&gt;



How to fetch multy records - El Forum - 08-17-2012

[eluser]Ayelius Maximous[/eluser]
Really thanks dear CroNiX. it works. I really thank you for your help :X