Welcome Guest, Not a member yet? Register   Sign In
how to display single row result
#1
Bug 

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;
}
?>
#2

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
Hey, don't work without a PHP debugger. Several free IDEs have this features built in. Two are NetBeans and CodeLobster. Without a debugger, it's like you're driving with a blindfold on -- you are going to crash!
#3

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.




Theme © iAndrew 2016 - Forum software by © MyBB