Undefined variable: results - php

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

Related

How to call query function in Bonfire model?

Customers_model
Class Customers_model extends BF_Model{
protected $table_name = 'customers';
protected $key = 'customer_id';
protected $date_format = 'datetime';
My query function in model
function get_customerlist()
{
$sql = $this->db->query('SELECT first_name, last_name , customer_id FROM customers ');
return $sql->result();
}
}`
Controller
public function listCustomer()
{
$this->load->model('customers_model'); // whatever you call it
$data['list'] = $this->customers_model->get_customerlist();
$this->load->view('myview', $data);
}
View
foreach($list as $value)
{
echo $value->first_name. '<br />'; output.
}
It can't show $list array from controller : Undefined list variable
In your Model Query method i made few changes,
function get_customerlist()
{
$selelct = array('first_name','last_name','customer_id');
return $this->db->select($select)
->from('customers')
->get()
->result();
}
In your Controller,
public function listCustomer()
{
$this->load->model('customers_model'); // whatever you call it
$list = $this->customers_model->get_customerlist();
$data['list'] = $list; //returned result is an array of object
$this->load->view('myview', $data);
}
In Your view,try this,
foreach((array)$list as $item)
{
echo $item->first_name. '<br />';
}
It should work.

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

Load specific data from array

So i have this class for loading data from my MySQL database :
class Db {
protected static $connection;
public function connect() {
if(!isset(static::$connection)) {
$config = parse_ini_file('config.ini');
static::$connection = new mysqli('localhost',$config['username'],$config['password'],$config['dbname']);
}
if(static::$connection === false) {
return false;
}
return static::$connection;
}
public function query($query) {
return $this->connect()->query($query);
}
public function select($query) {
$rows = array();
$result = $this->query($query);
if($result === false) {
return false;
}
while ($row = $result->fetch_assoc()) {
$rows[] = $row;
}
return $rows;
}
public function error() {
return $this->connect()->error;
}
public function quote($value) {
return "'" . $this->connect()->real_escape_string($value) . "'";
}
}
I use that class like this :
$db = new Db();
$rows = $db -> select("SELECT name, shortlink FROM `test` WHERE id=3");
It gives me an array with the data.
The problem is that i want to pull out specific data, for example the shortlink field.
How do i do that? I have tried echo $rows['shortlink'], but that gives me the following error :
Undefined index: shortlink
So how do I print specific data?
Your returned $rows columns is an array of associative arrays, to pull out shortlink data returned from the query you have to do something like this:
foreach($rows as $row) {
echo $row['shortlink'];
}

Codeigniter: Invalid argument supplied for foreach(row)

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'];

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