CodeIgniter Forums
how to display single row 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: how to display single row result (/showthread.php?tid=62911)



how to display single row result - nady - 09-07-2015

Hi everbody

I want to send a single row from model to controller then from controller to view. i checked some of the same kind of question discussed earlier here but couldn't fix it

ERROR: Invalid argument supplied for foreach()

code for my controller is
<?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();
$data['top'] = $this->select->topmenu();
$data['title'] = $this->select->get_title('categories');
$this->load->view('HomeView', $data);
}
}
?>
------------------------Select.php(Model)----
<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<?php
class Select extends CI_Model
{
public function get_title($table,$where=NULL)
{
if(!empty($where))
{
$query = $this->db->get($table);
if($query->num_rows>0)
{
return $query->row();
}
else
{
return 0;
}}}}
------------------HomeView.php--------
<?php
foreach($title as $t)
{
echo $t->title;
}
?>


RE: how to display single row result - RobertSF - 09-07-2015

Hi Nady. the error message indicates that $title is not an array nor an object in your view file. It must have the value of 0. Let me see if I can give you a couple of suggestions.

In your model, you have this code.
PHP Code:
$query $this->db->get($table);
if (
$query->num_rows 0)
{
 
   return $query->row();
}
else
{
 
   return 0;

If the query returns one or more rows, the function returns the first row, otherwise the function returns zero. Is that the logic you want? If yes, then the function must be returning zero, and the foreach() can't do anything with it, so you get an error.

Try using debugging statements to confirm that. In your controller, do something like this.
PHP Code:
public function index()
{
 
   $this->load->database();
 
   $this->load->model('select');
 
   $data['cat'] = $this->select->select();
 
   $data['top'] = $this->select->topmenu();
 
   $data['title'] = $this->select->get_title('categories');
var_dump($data['title']); exit;    
    $this
->load->view('HomeView'$data);


If $this->select->get_title('categories'); isn't returning data, then we'll need to figure out why.

By the way, when you post code, please use the code buttons to format the code correctly. It's kind of hard to follow unfamiliar code even when it looks nice, so you can imagine...  Smile


RE: how to display single row result - Narf - 09-10-2015

Moved this thread from the "Best Practices" forum here and it's a duplicate of displaying a single row in a view, so closing it.

Also moved how to retrieve data from multiple table from "Regional User Groups" to "General Help" ...

@nady, please post your threads only once and in the relevant forum.