unable to get data through result() |
hi everyone
i am new in codeigniter. please help me. i am unable to fetch the records from database. my HomeController file is following... <?php defined('BASEPATH') OR exit('No direct script access allowed'); class HomeController extends CI_Controller { public function index() { $this->load->database(); $this->load->model('select'); $data['cat'] = $this->select->select(); $this->load->view('HomeView', $data); $this->load->database(); $this->load->model('select'); $msg['menu'] = $this->select->topmenu(); $this->load->view('HomeView', $msg); } } ----------model---------------- select.php <?php defined('BASEPATH') OR exit('No direct script access allowed'); ?> <?php class select extends CI_Model { public function select() { //data is retrive from this query $query = $this->db->get_where('categories',array('category_name'=>'English')); return $query; } public function topmenu() { $qry=$this->db->get('categories'); return $qry; } } ?> -----------Home_view.php-------------------- <?php foreach($menu->result() as $m) { echo "<div class='panel-body'><a href='#'>". $m->category_name ."</a></div>"; } $menu->free_result(); ?> <div class="panel-heading">CATEGORIES</div> <?php foreach($cat->result() as $c) { echo "<div class='panel-body'><a href='#'>". $c->subcategory_name."</a></div>"; } ?> </div> PHP Code: return $qry->row_array(); SEE: Database Reference - Generating Query Results What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
First try this in your controller, right after the line $data['cat'] = ... :
PHP Code: echo '<pre>'; This will show what your model returns as a result.
what's wrong in my code... please check it again and help me please....
i also used return $qry->row_array(); instead of return $qry.... following is error message ERROR : Fatal error: Call to a member function result() on a non-object in C:\xampp\htdocs\sms\application\views\HomeView.php on line 12 ------------------------------------------ HomeController.php <?php defined('BASEPATH') OR exit('No direct script access allowed'); class HomeController extends CI_Controller { public function index() { $this->load->database(); $this->load->model('select'); $data['cat'] = $this->select->select(); $this->load->view('HomeView', $data); $msg['top'] = $this->select->topmenu(); $this->load->view('HomeView', $msg); } } ?> ------------------- Select.php(Model) <?php defined('BASEPATH') OR exit('No direct script access allowed'); ?> <?php class Select extends CI_Model { public function select() { //data is retrive from this query $query = $this->db->get_where('categories',array('category_name'=>'English')); return $query; } public function topmenu() { $qry=$this->db->get('categories'); return $qry; } } ?> ------------------------------ HomeView.php <?php foreach($top->result() as $m) { echo "<div class='panel-body'><a href='#'>". $m->category_name ."</a></div>"; } ?> <div class="panel-heading">CATEGORIES</div> <?php foreach($cat->result() as $c) { echo "<div class='panel-body'><a href='#'>". $c->subcategory_name."</a></div>"; } ?> </div>
In your model:
Quote:class Select extends CI_Model In your view: Quote:<?php
Now i got 1 notice 1 warning
Notice Message: Undefined variable: top Warning Message: Invalid argument supplied for foreach() both error shows in view file. (09-06-2015, 11:18 AM)nady Wrote: Now i got 1 notice 1 warning Modify this in your controller: PHP Code: public function index() |
Welcome Guest, Not a member yet? Register Sign In |