accessing result array in codeigniter - 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; ?>

Related

Order of the views in Query database and Views in codeigniter

The order of the views change wen i make a call to a database. In this case i make a Formulari.php that is a Controller.
Formulari.php
public function resum(){
**$this->load->view('header');**
$query = $this->db->query("SELECT * FROM tarifes");
# code...
foreach ($query->result_array() as $row) {
echo $row['operador'];
echo $row['minutatge'];
echo $row['permanencia'];
echo $row['dades'];
echo $row['preu'];
echo '</br>';
}
$this->load->view('resum_taula');
$this->load->view('footer');
}
When I see this controller the first i can see is the table that returns me. But que first view i want to see is the title.
Thanks a lot!
Controller
$data['formular_data'] = $this->your_model->getData();
$this->load->view('resum_taula', $data);
$this->load->view('footer');
Model
function getData() {
$query = $this->db->get('tarifes');
return $query->result_array();
}
View
<?php print_r($formular_data'); ?> // form $data['formular_data'];

Calling a model function in view

i am trying to access a function in model, in my view in codeigniter and its not working.
can somebody please tell me where is the problem.
MODEL:
function gettemplates()
{
$sql = "select * from srs_templates";
$query = $this->db->query($sql);
$result = $query->result_array();
echo "<pre>"; print_r($result);exit;
return $result;
}
VIEW:
<select id="special" >
<?php echo $this->dashboard_ext_model->gettemplates(); ?>
</select>
Change this:
echo "<pre>"; print_r($result);exit;
To this:
echo "<pre>"; print_r($result); echo "</pre>";
Basically remove the exit. Exit aborts the script.
You should never have a good reason to call the model from the view. Model data should be passed to the controller, to then pass to the view.
Since this is MVC (Model View Controller) you shouldn't call a model from a view.
You should be calling the Model inside the controller then passing to the view as an argument.
Model:
function gettemplates()
{
$sql = "select * from srs_templates";
$query = $this->db->query($sql);
$result = $query->result_array();
echo "<pre>"; print_r($result);exit;
return $result;
}
Controller:
function gettemplates(){
//$this->dashboard_ext_model = $this->load->model('dashboard_ext_model');
//uncomment this if you didn't load the model
$data['templates'] = $this->dashboard_ext_model->gettemplates();
$this->view->load('page_name', $data);
}
View:
<select id="special" >
<?php echo $templates ?>
</select>
MVC may look stupid at first but it really helps in large size projects.
MVC DESIGN: http://i.stack.imgur.com/Beh3a.png

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

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

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