CodeIgniter Forums
unable to get data through result() - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: unable to get data through result() (/showthread.php?tid=62895)



unable to get data through result() - nady - 09-05-2015

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>


RE: unable to get data through result() - InsiteFX - 09-06-2015

PHP Code:
return $qry->row_array(); 

SEE:


Database Reference - Generating Query Results


RE: unable to get data through result() - Wouter60 - 09-06-2015

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.


RE: unable to get data through result() - jLinux - 09-06-2015

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


RE: unable to get data through result() - nady - 09-06-2015

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>


RE: unable to get data through result() - pdthinh - 09-06-2015

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>



RE: unable to get data through result() - nady - 09-06-2015

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.


RE: unable to get data through result() - pdthinh - 09-06-2015

(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);




RE: unable to get data through result() - nady - 09-06-2015

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