Codeigniter: Invalid argument supplied for foreach(row) - php

Here's the code I'm using
Models folder
class Data_model extends CI_Model {
function getAll(){
$q = $this->db->query("SELECT * FROM users");
if($q->num_rows()>0){
foreach($q->result() as $row)
{
$data[] = $row;
}
return $data;
}
}
}
View folder
<p><?php foreach($row as $r){
echo '<h1>'. $r->title .'</h1>';
}
?></p>
Controller folder
function index() {
$this->load->model('data_model');
$data['row'] = $this->data_model->getAll();
$this->load->view('home',$data);
}
The error I get is:
Message: Invalid argument supplied for foreach()
Filename: views/home.php
Line Number: 10

Instead of this
foreach($q->result() as $row)
{
$data[] = $row;
}
try this
$data['row'] = $q->result();

Use Active record class
Your model:
public function getAll()
{
$query = $this->db->get("users");
return $query->result_array();
}
View file:
foreach($row as $item)
echo $item['title'];

Related

Undefined variable and Invalid argument supplied for foreach()

i'm trying to learn codeigniter and i have problem here. There was an error Undefined variable and Invalid argument supplied for foreach() on view. here is the code :
Model (Model_Users.php) :
class Model_users extends CI_Model {
function __construct() {
parent::__construct();
}
function getFirstNames(){
$query = $this->db->query('SELECT firstname FROM users');
if($query->num_rows() > 0) {
return $query->result();
} else {
return NULL;
}
}
function getUsers(){
$query = $this->db->query('SELECT * FROM users');
if ($query->num_rows() > 0) {
return $query->result();
} else {
return NULL;
}
}
}
Controller (welcome.php) :
class Welcome extends CI_Controller {
public function index()
{
$this->home();
}
public function home()
{
$this->load->model('model_users');
$data['title'] = 'MVC Cool Title';
$data['page_header']='Intro to MVC Design';
$data['firstnames'] = $this->model_users->getFirstNames();
$data['users'] = $this->model_users->getUsers();
$this->load->view('welcome_message',$data);
}
}
View (Welcome_message.php) :
<div id="container">
<h1><?php echo $page_header ?></h1>
<div id="body">
<?php
foreach ($firstnames as $object) {
echo $object->firstname .'<br/>';
}
echo '<br/><hr/><br/>';
foreach ($users as $object){
echo $object->firstname . '\'s email address is'. $object->email . '<br/>';
}
?>
</div>
<p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds</p>
</div>
Please help me what is wrong with my code ?
First check value return any value or not write this code in controller
use
var_dump($firstnames);
and
var_dump($users);
I think no data return from model
check any data in table
Thanks
You can eliminate the error by using return []; instead of return null; instead of checking for that case in every foreach call, because it appears that your user table might be empty based on the code provided.
Modify your model functions like below
function getFirstNames(){
$query = $this->db->get('users')->row()->firstname;
if($query->num_rows() > 0) {
return $query->result_array();
} else {
return NULL;
}
}
function getUsers(){
$query = $this->db->get('users');
if ($query->num_rows() > 0) {
return $query->result_array();
} else {
return NULL;
}
}
hope this will help

codeigniter retrieve data from database normal print_r working fine but can't use foreach why

I am going to fetch specific gender from db like gender='male' code is working fine but can't iterate it in foreach why it is
model you can check
class get_all_gender extends CI_Model {
//put your code here
public function select_male() {
$this->db->select('*');
$this->db->from('bride_groom_register');
$this->db->where("gender='male'");
$query = $this->db->get();
if ($query->result() > 0)
{
return $query;
} else {
return FALSE;
}
}
}
and controller i m getting data from database
class Welcome extends CI_Controller {
public function index()
{
$this->load->model('get_all_gender');
$data['res']=$this->get_all_gender->select_male();
$this->load->view('list',$data);
}
}
simple view
foreach ($res as $row)
{
echo $row->name;
}
Try this
In Model
class get_all_gender extends CI_Model {
//put your code here
public function select_male()
{
$this->db->select('*');
$this->db->from('bride_groom_register');
$this->db->where("gender='male'");
$query = $this->db->get();
$result = $query->result_array();
if (!empty($result))
{
return $result;
}
else {
return FALSE;
}
}
}
In Controller
class Welcome extends CI_Controller {
public function index()
{
$this->load->model('get_all_gender');
$data['res'] = $this->get_all_gender->select_male();
$this->load->view('list',$data);
}
}
In View
if ($res != FALSE) {
foreach ($res as $value) {
echo $value;
}
}
else {
echo "Seleted Category is Empty";
}
You should return the $query's result() instead of $query itself. Your model should look like this:
class Get_all_gender extends CI_Model {//put your code here
public function select_male() {
$this->db->where("gender","male");
$query = $this->db->get("bride_groom_register");
if ($query->num_rows() > 0) {
return $query->result();
} else {
return FALSE;
}
}
}
You are saving the result in the controller:
$data['res'] = $this->get_all_gender->select_male();
So you can access it like this in the view:
if($res !== FALSE){
foreach ($res as $row)
{
echo $row->name;
}
}
You should check for the FALSE condition, otherwise the view will throw PHP errors when there is no rows returned from the model.
Try code below on model
Class Name first letter upper case
Filename: Get_all_gender.php
<?php
class Get_all_gender extends CI_Model {//put your code here
public function select_male() {
$this->db->select('*');
$this->db->from('bride_groom_register');
$this->db->where('gender', 'male');
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result_array();
// Or
// return $query->row_array();
} else {
return FALSE;
}
}
}
On View
foreach ($res as $row)
{
echo $row['name'];
}
Class Name first letter upper case
class Get_all_gender extends CI_Model {//put your code here
public function select_male() {
$this->db->select('*');
$this->db->from('bride_groom_register');
$this->db->where("gender='male'");
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result_array();
} else {
return FALSE;
}
}
}

Undefined variable: results

Model
public function show()
{
$query = $this->db->get('text');
return $query->result();
}
2.Controller
public function inf()
{
$this->load->model('addm');
$data['results'] = $this->addm->show($query);
$this->load->view('backend/first',$data);
}
3.view
I am trying to get the data from my db but i can't solve this error:
Undefined variable: result
<?php
echo"<td>";
if (is_array($result)) {
foreach ($result() as $row) {
$id = $row->id;
$words = $row->words;
}
}
echo "</td>";
?>
Few things,
In your model function show is not expecting any parameters
Remove $query from $this->addm->show(); as not needed
In the view variable should be results not result
// controller
public function inf()
{
$this->load->model('addm');
$data['results'] = $this->addm->show();
$this->load->view('backend/first', $data);
}
// view
if (!empty($results)) {
foreach($results as $row) {
$id = $row->id;
$words = $row->words;
}
}
// model
public function show()
{
$query = $this->db->get('text');
return $query->result();
}

Can't access global variable in PHP codeigniter Model class.

I have a class in Codeigniter which extends CI_Model.
I have declared a global array $data, but I can't access this array from my functions which i want to push some items.
that shows
A PHP Error was encountered Message: Undefined variable: data
and Fatal error: Cannot access empty property in /api/system/core/Model.php on line 51
code for class:
class Subscription_collection_model extends CI_Model {
public $data=array();
/*
|-----------------------------------------
| Constructor
|-----------------------------------------
*/
function __construct() {
parent::__construct();
}
function get() {
$this->db->select('family.house_name,family.id,family_grade.amount');
$this->db->from('family');
$this->db->join('family_grade','family_grade.id = family.family_grade_id');
$query = $this->db->get();
if ($query->num_rows() > 0)
{
foreach ($query->result() as $row)
{
$this->findBalance($row->id);
}
}
return $data;
}
function findBalance($id) {
$this->db->where('family_id', $id);
$this->db->select_sum('collecting_amount');
$query = $this->db->get('subscription_collection');
if ($query->num_rows() > 0)
{
foreach ($query->result() as $row)
{
array_push($this->$data,$row->collecting_amount);
}
}
}
}
you got a typo there. you want to call $this->data not $this->$data.
the latter would assume, you wanna call a dynamic variable on the instance.
$prop = 'myProperty';
$this->$prop = 'test';
is the same as
$this->myProperty = 'test';
How about:
return $this->data;

undefined variable Codeigniter and invalid argument supplied foreach read database

I was following the instruction to get the entire record from table, and load them into html table. this is the model
private $namatabel;
public function __construct() {
parent::__construct();
$namatabel='ms_kategori_material';
}
function read()
{
$sql = $this->db->get($this->namatabel);
if($sql->num_rows() > 0)
{
foreach($sql->result() as $row)
{
$data[] = $row;
}
return $data;
}
else
{
return null;
}
}
then use the read() function on controller
public function __construct() {
parent::__construct();
$this->load->model('m_kategorimaterial');
}
function index()
{
$data['c_row'] = $this->m_kategorimaterial->read();
//pass the c_row into the views
$this->load->view('v/vkategorimaterial', $data);
}
to display them on the views
<?php
$no = 1;
foreach ($c_row as $row) { ?>
<tr id="row">
<td id="no"><?php echo $no;?></td>
<td id="judul"><?php echo $row->Kode_Kategori_Material_Jasa;?></td>
<td id="kategori"><?php echo $row->Nama_Material_Jasa;?></td>
</tr>
<?php
$no++;
}
?>
but then I got an error saying, undefined variable c_row and invalid argument supplied foreach(). I thought I have sent the c_row variable through the c_kategorimaterial/index and copy pasting foreach statement. what went wrong ?
1)Check whether you are getting the data right by using print_r($data). If that went wrong,you are probably missing something in the query.
2)If you got the db data perfectly,then just change the name of returning array in your model.
function read()
{
$sql = $this->db->get('ms_kategori_material');
if($sql->num_rows() > 0)
{
foreach($sql->result() as $row)
{
$c_row[] = $row;
}
return $c_row; //comment this return part while debugging
// print_r($c_row);
//exit;
}
else
{
return null;
}
}
It would be better if you print your data array to get exact idea of what you are getting in that array. Try following format, might be helpful
function getPosts() {
$query = $this->db->get('posts');
$posts = array();
foreach ($query->result() as $row) {
$posts[] = array(
'id' => $row->id,
'title' => $row->title,
'content' => $row->content
);
}
return $posts;
}

Categories