Welcome Guest, Not a member yet? Register   Sign In
unable to get data through result()
#1
Exclamation 

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>
Reply
#2

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 )
Reply
#3

First try this in your controller, right after the line $data['cat'] = ... :
PHP Code:
echo '<pre>';
print_r($data);
echo 
'</pre>'

This will show what your model returns as a result.
Reply
#4

FYI, you dont have to load the database library or the select model twice.
Reply
#5

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>
Reply
#6

(This post was last modified: 09-06-2015, 08:03 AM by pdthinh.)

In your model:
Quote:class Select extends CI_Model  
  {  
     public function select()  
     {  
        //data is retrive from this query  
        $query = $this->db->get_where('categories',array('category_name'=>'English')); 
        // you should return result() if the result contain multi rows, or row() if you want only one row.
        return $query->result();
     }  
 public function topmenu()
 {
  $qry=$this->db->get('categories');
return $qry->result();
 }
  }  
?>

In your view:
Quote:<?php 
//foreach($top->result() as $m)
foreach($top 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)
foreach($cat as $c)
{
echo "<div class='panel-body'><a href='#'>". $c->subcategory_name."</a></div>";
}
?>
</div>
Reply
#7

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.
Reply
#8

(09-06-2015, 11:18 AM)nady Wrote: 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.

Modify this in your controller:

PHP Code:
public function index()

{
$this->load->database();
$this->load->model('select');
$data['cat'] = $this->select->select();
$data['top'] = $this->select->topmenu();
$this->load->view('HomeView'$data);

Reply
#9

Thank you very much now it's working fine...
Thanks a lot...
Reply




Theme © iAndrew 2016 - Forum software by © MyBB