Codeigniter returning non-object - codeigniter

I have this happen all the time and I'm never quite sure why it's happening. Here is the query in my model:
$query = $this->db->query('SELECT * FROM members WHERE email = "' . $this->session->userdata('email') . '"');
return $query;
I try to output this in my view with:
foreach($query->result() as $row) {...echo $row->name, etc.}
But I get an error:
Fatal error: Call to a member function result() on a non-object ...
I ran the profiler and my query is valid and there is data in the database to be pulled. So what am I do wrong?

Try this.
In your model:
function get_members()
{
$query = $this->db->query('SELECT * FROM members WHERE email = "' . $this->session->userdata('email') . '"');
return $query->result();
}
In your controller:
You should then be assigning the data from the database in your controller to your view like so.
$data['query'] = $this->yourmodel->get_members(); // Data['query'] will be turned into $query in your view.
$this->load->view('mytemplate', $data);
In your view:
foreach($query as $row) {...echo $row->name, etc.}

If you are passing the $query variable to the view, codeigniter will convert it into a keyed array if it is an object.
http://codeigniter.com/user_guide/general/views.html
Note: If you use an object, the class variables will be turned into array elements.
Edit:
In your controller:
$query = // do whatever to get the $query.
$members = array();
foreach($query->result() as $row) {
$members[] = $row;
}
//send $members to view
In view:
foreach($members as $member) {
echo $member['name']; // etc
}

Related

I get the CI_DB_mysqli_driver::row_array() in Codeigniter

I get the CI_DB_mysqli_driver::row_array() error when trying to display a cell value.
This is my model
function list_get($id){
$this->load->database();
$query = $this->db-> select('*')
->from('lists')
->join('list_items', 'list_items.items_list_id = lists.list_id')
->where('items_list_id', $id);
return $query->row_array();
}
The error appears when I try to echo it to the view <?echo $query['list_by'];?>

Codeigniter model pass value controller to view

Table products: id, product, lining, ...
Table tbl_lining: id, article, description
Model
public function getliningsale($id)
{
$this->db->select('*');
$this->db->from('products');
$this->db->join('tbl_lining', 'products.lining=tbl_lining.id', 'left');
$this->db->where('products.id', $id); // Also mention table name here
$query = $this->db->get();
if($query->num_rows() > 0)
return $data->result();
}
Controller
function liningsale($id)
{
$data['liningsale'] = $this->sales_model->getliningsale($id);
$this->load->view('add', $data);
}
View
echo '<td><input class="span1 tran2" name="lining\'+ count +\'" type="text"';
echo 'value="';
foreach ($liningsale as $valor) {
echo $valor->article;
echo '-';
echo $valor->description;
echo '">';
}
echo '</td>';
This doesn't display any record.
I've tried several ways without success.
Can anyone help?
In your model, you need to return the $query result.
At the minute you are trying to return the result set of a variable named $data which I presume doesn't exist!!
Try changing return $data->result(); to return $query->result();
Hope that helps!!

CodeIgniter pass variables form controller to model

Ok I want to pass two variables from a controller to a model but I get some kind of error. Am I passing variables on right way? My syntax is:
Controller:
public function add_tag(){
if(isset($_POST['id_slike']) && isset($_POST['id_taga'])){
$slika = $_POST['id_slike'];
$tag = $_POST['id_taga'];
$this->load->model("Member_model");
$res = $this->Member_model->add_tags($slike, $tag);
foreach ($res->result() as $r){
echo $r->name;
}
}
else{
echo "";
}
}
Model:
public function add_tags(){
$data = array(
'tags_id' => $tag ,
'photos_id' => $slika
);
$check = $this->db->query("SELECT tags_id,photos_id FROM bridge WHERE bridge.tags_id='{$tag}' AND bridge.photos_id={$slika} ");
if($check->num_rows()==0){
$this->db->insert('bridge',$data);
$res = $this->db->query("SELECT name FROM tags where `tags`.`id`='{$tag}' ");
return $res;
}
}
you are passing variables correctly, but do not get them correctly in the model, which should look like this:
public function add_tags($slike, $tag){
//your other code
}
The following code write on the controller file:-
$data = array();
$this->load->model('dbmodel');
$data['item'] = $this->dbmodel->getData('*','catagory',array('cat_id'=>21));
$this->load->view('listing_view', $data);
The following code write on the dbmodel file:-
public function getData($cols, $table, $where=array()){
$this->db->select($cols);
$this->db->from($table);
$this->db->where($where);
$query = $this->db->get();
$result = $query->result();
return $result;}

accessing result array in codeigniter

I have the following happening in my model:
public function load_user_menu($username)
{
$this->db->select('*');
$this->db->from('menu');
$this->db->where('username', $username);
$query = $this->db->get();
return $query->result();
}
and the following in my controller:
public function index()
{
/*If user previously logged in and not logged out, username remains in session.
load username and load profile data.
*/
//check if user logged in or not
if(($this->session->userdata('username')!=""))
{
//load data from model
$profile = array();
$username = $this->session->userdata('username');
$result = $this->profileModel->user_profile($username);
foreach($result as &$value)
{
$profile['userdetails'] = $value;
}
$this->load->view('profile', $profile);
}else{
//redirect to login function
$this->login();
}
}
but I am getting errors on profile view. I am sure I am accessing them wrong because I am doing this on view:
<? echo $userdetails['profilepic']; ?>
and nothing is showing but this error:
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: controllers/profile.php
Line Number: 60
which am certain because of the wrong accessing. How can I access the details based on the above?
assumming you have profilepic inside userdetails and seeing the error you are trying to get he property of nonobject that means
<? echo $userdetails['profilepic']; ?>
should be
<? echo $userdetails->profilepic; ?>
since you are taking you result as object and not array
return $query->result();
or you change this to
return $query->result_array();
$query->result(); produces an object; $query->result_array(); gives you an array
either is fine, depending on your needs
-There is no need of foreach loop in your controller as
$result = $this->profileModel->user_profile($username);
foreach($result as &$value)
{
$profile['userdetails'] = $value;
}
-Try this instead.
$profile['userdetails'] = $this->profileModel->user_profile($username);
-To Access in View,use this as
echo $userdetails->profilepic;
just try this on your view page
<? echo $profilepic; ?>

I have a question about using form_dropdown()

I have a question about using form_dropdown().
table:category
cat_id
cat_name
Controller:
function index()
{
$this->load->model('category_model');
$data['category'] = $this->category_model->cat_getallcat();
$this->load->view('category_input',$data);
}
Model:category_model
function cat_getallcat()
{
$this->load->database();
return $this->db->get('category')->result();
}
View:
<?php
$this->load->helper('form');
echo form_open('send');
$list[''] = 'Please select a Category';
foreach($query as $row)
{
$list[$row->cat_id] = ucfirst(htmlspecialchars($row->cat_name));
}
echo form_dropdown('category', $list);
echo form_close();
?>
error obtained:
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: views/category_input.php
Line Number: 28
the ->result() will return the first row only, so this function is what you want to loop over.
Model
function cat_getallcat()
{
$this->load->database();
return $this->db->get('category');
}
View
foreach($query->result() as $row)
{
$list[$row->cat_id] = ucfirst(htmlspecialchars($row->cat_name));
}
EDIT:
Also, you are sending the result as $data['category'] then trying to access it as $query. So really the foreach would be foreach($category->result() as $row) in this example
You are asking foreach to loop over $query, but I you haven't set $query as a variable anywhere that I can see.
Since you set $data['category'] to hold your query result() in your controller, you need to loop over $category in the view:
foreach($category as $row)
{
$list[$row->cat_id] = ucfirst(htmlspecialchars($row->cat_name));
}

Resources