I get the CI_DB_mysqli_driver::row_array() in Codeigniter - 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'];?>

Related

retrieve data from database in for loop

my controller is
function shiftstudentdetailsmultiple(){
$data['Show'] = $this->input->post('show[]');
$roll = $this->input->post('roll[]');
//echo sizeof($data1);
for($i=0;$i<sizeof($roll);$i++)
{
$data['results'] = $this->FetchData->getstudentsdetailscouseandmultiple($roll[$i]);
}
$this->load->view('registrar/shift_student_view', $data);
}
and model is
function getstudentsdetailscouseandmultiple($Uniq_Id){
$this->db->select("sprd.Uniq_Id, sprd.Name, sprd.Uni_Roll_No, cd.Course_Name, bd.Branch_Name, sprd.Year, sprd.Session, sprd.Section, sprd.Fee_Status, sprd.Curr_Status");
$this->db->where("sprd.Uniq_Id",$Uniq_Id);
$this->db->from("Students_Prim_Detals sprd");
$this->db->join('Course_Details cd', 'cd.Course_Id=sprd.Course_id');
$this->db->join('Branch_Details bd', 'bd.Branch_Id=sprd.Branch_id');
$query = $this->db->get();
if ($query->num_rows() > 0){
return $query->result();
}
else {
return false;
}
}
when showing in view the fetching data showing only one time.. but in my database i have more then 4 entry when i print_r
$data['results']=this->FetchData->getstudentsdetailscouseandmultiple($roll[$i]);
the data showing correct .. when load only showing one data..the last entry..
$this->load->view('registrar/shift_student_view', $data);
Each time you call...
$data['results'] = $this->FetchData->getstudentsdetailscouseandmultiple($roll[$i]);
you replace the value stored in $data['results'] with the new return. So only one return is sent to the view.
You will need to revise your controller along these lines.
$student_detail = array();
for($i=0;$i<sizeof($roll);$i++)
{
$student_detail[] = $this->FetchData->getstudentsdetailscouseandmultiple($roll[$i]);
}
$data['results'] = $student_detail;
Then in your view you will have to loop through $results to display each item in the array.
foreach($results as $detail){
echo "Name: ". $detail->name;
echo "Course: ". $detail->Course_Name;
//etc...
}

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!!

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; ?>

Codeigniter : displaying query result in controller

I am trying to display my db query result in my controller, but I don't know how to do it. could you please show me?
Controller
function get_name($id){
$this->load->model('mod_names');
$data['records']=$this->mod_names->profile($id);
// I want to display the the query result here
// like this: echo $row ['full_name'];
}
My Model
function profile($id)
{
$this->db->select('*');
$this->db->from('names');
$this->db->where('id', $id);
$query = $this->db->get();
if ($query->num_rows() > 0)
{ return $query->row_array();
}
else {return NULL;}
}
echo '<pre>';
print_r($data['records']);
or
echo $data['records'][0]['fullname'];
Model:
function profile($id){
return $this->db->
select('*')->
from('names')->
where('id', $id)->
get()->row_array();
}
Controller:
function get_name($id){
$this->load->model('mod_names');
$data['records']=$this->mod_names->profile($id);
print_r($data['records']); //All
echo $data['records']['full_name']; // Field name full_name
}
You do that inside a View, like this.
Controller:
function get_name($id){
$this->load->model('mod_names');
$data['records']=$this->mod_names->profile($id);
$this->load->view('mod_names_view', $data); // load the view with the $data variable
}
View (mod_names_view):
<?php foreach($records->result() as $record): ?>
<?php echo $record->full_name); ?>
<?php endforeach; ?>
I would modify your model then to something like this (it worked for me):
function profile($id)
{
$this->db->select('*');
$this->db->from('names');
$this->db->where('id', $id);
$query = $this->db->get();
if ($query->num_rows() > 0)
{
return $query; // just return $query
}
}

Codeigniter returning non-object

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
}

Resources