Undefined variable and Invalid argument supplied for foreach() - php

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

Related

Invalid arguments in foreach() function

This is the error which i am getting
I am trying to fetch an array in codeignitor but it is giving me this error:
<?php
foreach($name as $object)
{
echo $object->name . '<br/>';
}
?>
And my Controller code is
public function index()
{
$this->home();
}
public function home()
{
$this->load->model('model_users');
$data['title'] ='First MVC tutorial';
$data['page_header'] ='Intro to MVC';
$data['name'] = $this->model_users->getNames();
$data['users'] = $this->model_users->getUsers();
$this->load->view('welcome_message',$data);
}
This is the model
class Model_users extends CI_Model {
function _construct()
{
parent::_construct();
}
function getNames()
{
$query = $this->db->query('SELECT name 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;
}
}
So can anyone help me solve this error??
As i am new to new to codeignitor
Thanks in advance.
This is the screenshot of my table
Do you have any rows in your users table?
It seems that your model returns null, and foreach expects an Array as it's array_expression var. Could be an empty array as well.
So you might wan't to change the
return NULL;
to
return [];
in your model and that should at least prevent the error.
p.s.
http://php.net/manual/en/control-structures.foreach.php

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

how to debug this? Call to a member function result() on a non-object

Please Help.. im using CODEIGNITER
I want to print the value of my userlevel and email using echo.. to know if im getting the right data from the database.. then suddenly this error keep bugging me.
beginner in CODEIGNITER need some help here.. thanks a lot!
in my controller:
if ($this->is_validated($rules))
{
$where = array('email' => $this->input->post('txt_email'));
$data['query'] = $this->database_model->select_userlevel('userlevel', $where);
$user = $data['query'];
foreach($user->result() as $row)
{
echo $row->email;
echo $row->userlevel;
}
}
in my model:
public function select_userlevel($table, $where)
{
$this->db->where($where);
$query = $this->db->get($table);
if ($query->num_rows() > 0)
{
return true;
}
else
{
echo 'no value';
return false;
}
}
Instead of return true you need to return your query in model
Model
public function select_userlevel($table, $where)
{
$this->db->where($where);
$query = $this->db->get($table);
if ($query->num_rows() > 0)
{
return $query;// return query here
}
else
{
return false;
}
Controller
$user = $this->database_model->select_userlevel('userlevel', $where);
foreach($user->result() as $row)
{
// your code here
}
UPDATED FOR better solution and proper use of MVC.
Instead of return query you need to return data from your model
MODEL
public function select_userlevel($table, $where)
{
$this->db->where($where);
$query = $this->db->get($table);
if ($query->num_rows() > 0)
{
return $query->result(); // return your result
}
else
{
return false;
}
}
CONTROLLER
$user = $this->database_model->select_userlevel('userlevel', $where);
foreach ($user as $row) {
// your code here
}
$user is not an active record object, so it does not have a reference to the result method.
All of this logic belongs in the Model and not the Controller.
Controller
public function doSomething()
{
if( !$this->is_validated($rules) )
return;
$email = $this->input->post('txt_email');
$users = $this->database_model->select_userlevel($email);
// for debugging
var_dump( $users )
die();
}
Model
public function select_userlevel($email)
{
$query = $this->db->select()
->where('email', $email)
->get('userlevel');
return ( $query->num_rows() > 0) ? $query->result() : false;
}

Database Query showing in view and not $data

The below code seems to be showing in the "view" and not via $data as it should be (I have not echoed the $data['companyName'] into my view yet but $data['pageTitle'] works fine).
Issue:
Model:
function companyName()
{
$companyName = $this->db->query("SELECT company_name FROM core");
if ($companyName->num_rows() > 0)
{
foreach ($companyName->result() as $row)
{
echo $row->company_name;
}
}
Controller:
public function index()
{
$data['companyName'] = $this->core_model->companyName();
$data['pageTitle'] = "Admin Login";
$this->load->view('admin/assets/header', $data);
$this->load->view('admin/login.php');
$this->load->view('admin/assets/footer');
}
By echoing something in the model, that echo happens when the model is processed, which is way before the view is even a twinkle in its controller's eye.
Instead, you should do something like this in your model:
function companyName()
{
$companyName = $this->db->query("SELECT company_name FROM core");
if ($companyName->num_rows() > 0)
{
$company_names = '';
foreach ($companyName->result() as $row)
{
$company_names .= $row->company_name;
}
}
return $company_names;
}
Then you can pass $data into the view and the string will be ready to echo out like you want.

Categories