Query not passing with View in Codeigniter - php

I have the foloowing controller (supernavigationloggedin):
<?php
class Supernavigationloggedin extends CI_Controller {
function index(){
#get current session id
$currentSessionID = $this->session->userdata('session_id');
#get all the account row for the given sessionID
$data['info'] = $this->db->get_where('Client', array('session_id'=>$currentSessionID))->row();
#views
$this->load->view('supernavigationloggedin',$data);
}
}
?>
and the following view named(supernavigationloggedin):
<div id="superNavigation">
<h5><strong>Welcome</strong>, <?php $info->fname; ?> Account Settings</h5>
<div class="clearL"> </div>
</div
>
It keeps throwing an error on line:<h5><strong>Welcome</strong>, <?php echo $info['fname']; ?> <a href="#">Account that Message: >>> Trying to get property of non-object
I've tried : <?php echo $info['fname']; ?> <?php echo $info->fname; ?> but neither seem to work.

It's because $info  is empty and no database request are made. You have to do this this way if you want your query to return an object:
$data['info'] = $this->db->get_where('Client', array('session_id'=>$currentSessionID))->row();
Or this way, if you prefer to get it into an array:
$data['info'] = $this->db->get_where('Client', array('session_id'=>$currentSessionID))->row_array();
This way it should work. row() or row_array() is necessary to execute your query.

Related

Display an array data from two tables in codeigniter

I've two tables on my database, monitor [pk = idMonitor] and monitor_data [pk = idMonitor_data].
Please click you can see the tables fields here. As you can see i put the array data in table monitor_data.
I want to Update the condition for every idinventory where monitor_data.idMonitor = $id.
But first i want to display the current data of 'monitordate','idinventory', and 'condition' from database to my view.
My controller
public function edit($id=0) {
$dataa = $this->monitor_m->get_record(array('monitor_data.idMonitor'=>$id),true);
$this->data->monitordate = $dataa->monitordate;
$this->data->condition = $dataa->condition; <-line 20
$this->data->detail = $this->monitor_m->get_record(array('monitor_data.idMonitor'=>$id),true);
$this->template->set_title('SMIB | Monitoring')
->render('monitor_edit',$this->data);
}
My View (monitor_edit)
<?php echo form_open(site_url("monitor/ubah"),'data-ajax="false"'); ?>
<?php foreach ($detail as $items): ?>
<h4><?php echo '[ '.$items['idinventory'].' ] '?> </h4>
<?php echo form_label ('Condition : ');
echo form_dropdown('condition', array('good'=>'Good','broke'=>'Broken','lost'=>'Lost'),#$items['condition']);
?>
<?php endforeach; ?>
<?php echo form_close(); ?>
My Model
class Monitor_m extends MY_Model {
public function __construct(){
parent::__construct();
parent::set_table('monitor','idMonitor');
}
public function get_record($id = 0,$get_user = FALSE) {
$this->db->where($id);
if ($get_user){
$this->db->join('monitor_data','monitor_data.idMonitor = monitor.idMonitor');
$this->db->join('inventory','inventory.idinventory = monitor_data.idinventory');
$this->db->join('user','user.id_user = monitor.id_user');
}
$data = parent::get_array();
return $this->improve_data($data);
}
Here is my problem : its work fine for monitordate code in my controller, BUT i keep getting an error for condition code
Maybe because i use 'monitor_data.idMonitor' as my parameter $id not idinventory. how can i use 2 parameters for example like where idMonitor=$id and idinventory=$idiventory.
Do i explain it right ?
Severity: Notice Message: Trying to get property of non-object
Filename: controllers/monitor.php Line Number: 20
Please Please help me, i dont know what is wrong with my controller :( i've searching the solution but none of those work. :(
It's weird if you can get monitordate but can't get condition.
Can you edit your controller to be like this?
public function get_record($id = 0,$get_user = FALSE) {
$this->db->where($id);
if ($get_user){
$this->db->join('monitor_data','monitor_data.idMonitor = monitor.idMonitor');
$this->db->join('inventory','inventory.idinventory = monitor_data.idinventory');
$this->db->join('user','user.id_user = monitor.id_user');
}
// $data = parent::get_array();
$data = $this->db->result_array();
print_r($data);
echo $this->db->last_query();
exit;
return $this->improve_data($data);
}

How to insert an argument in a function through a loop and then call it that into the view in codeigniter

Am trying to call a method from the model which requires an argument
but am stack the way am I have to pass an argument remember the value which I have to pass is the data from loop
I tried calling get_feeds_comments($id) method directly from the view it works fine
But how can I really archive this if I first call the model method from the controller remember I have to pass an argument and then processed the data into the view and in the view am looping through the post from the database and then I need to assign a comment to its appropriate post
Guys am really stack please help
This is the code for the model
<?php
public function get_feeds_comments($id){
$this->db->select('feeds_comment.comment, users.profile_img, users.last_name,users.first_name,feeds_comment.comment_date,feeds_comment.feedid');
$this->db->from('feeds');
$this->db->where('feeds.id',$id);
$this->db->join('feeds_comment', 'feeds_comment.feedid = feeds.id');
$this->db->join('users', 'users.id = feeds_comment.userid');
$this->db->order_by('feeds_comment.comment_date');
$result = $this->db->get();
return $result->result();
}
?>
This is the code for the view
<?php
foreach($feeds_users as $single_data){
?>
<div class="display_post">
<div class="the_post">
<?php echo $single_data->feed; ?>
<hr>
<?php print_r($comment); ?>
</div>
</div>
<?php
}
?>
This is the code for the controller
<?php
public function display_feeds(){
$feeds_user_data = $this->user_model->get_feeds_users();
if(empty($feeds_user_data)){
die();
}else{
foreach($feeds_user_data as $single_user_data){
$display_user_id = $single_user_data;
}
}
$data['test_comment_data'] = $this->user_model->display_comments($display_user_id->id);
}
?>
And this is the model function get_feeds_users()
public function get_feeds_users(){
$this->db->select('*');
$this->db->from('users');
$this->db->join('feeds', 'feeds.userid = users.id');
$this->db->order_by('feed_date',"DESC");
$result = $this->db->get();
return $result->result();
}
From view call another controller that will take the $id for you model to be processed. The same controllers view will be rendered for you there and you will get processed data finally.

Trying to get property of non object is the error what iam getting in codeigniter

I want to display individual values passed from controller to view page but when i try to do so iam getting error has TRYING TO GET PROPERTY OF NON OBJECT so request ci developers to help me
This is my controller page
$data['showdata'] = $this->searchresultss->login($per_page,$look,$age, $age_to,$age_from,$se_ct,$subsect,$coun_try,$sta_te, $ci_ty,$qualification);
$this->load->view('searchresult',$data);
**This is my view page**
<?php
echo "<pre>";
print_r($showdata);
if (isset($showdata)){
foreach ($showdata as $key) {
?>
<?php echo($key->gender); ?>// This is the line where iam getting error
<?php
}
}
?>
**Here is my model page**
<?php
Class Searchresultss extends CI_Model
{
function login($per_page=3,$look,$age,$age_to,$age_from,$se_ct,$subsect,$coun_try, $sta_te, $ci_ty,$qualification)
{
$query="SELECT *
FROM users
";
$data=array();
$query=$this->db->query($query);
$data['results']=$query->result_array();
$data['count']=$query->num_rows();
$data['pages']=ceil($data['count']/3);
return $data;
}
}
I am geting error has trying to get property of non object. I am trying to print individual values passed from controller to view page ..but I am not able to do so.
You are not load the model in controler. Load the model searchresultss in controller before calling the model function. Like,
$this->load->model('searchresultss');
And
Change
echo($key->gender);
To
echo $key['gender'];
This will solve the issue
your echo is not wrong.
but i guess you didnt take the result of your query so:
try this :
if (isset($showdata)){
foreach ($showdata->result() as $key){ // just add ->result() after $showdata
echo($key->gender); ?>
}
}

Undefined variable in view page

I can't passing the variables in view page
this is my controller code
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Management extends CI_Controller {
public function __construct()
{
parent::__construct();
if($this->session->userdata('level') != 1){
redirect('');
}
}
public function hotels()
{
$this->load->model('model_hotels');
$ss['most_view_hotels'] = '23';
$this->load->view("management/header_mng_view");
$this->load->view('management/hotels_mng_view' , $ss , true);
$this->load->view("management/footer_mng_view");
}
}
this is the error
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: most_view_hotels
Filename: management/hotels_mng_view.php
Line Number: 22
hotels_mng_view.php
<?php
echo $most_view_hotels;
foreach($most_view_hotels as $value):
?>
<div class="row">
<div class="cell en">adsf</div>
<div class="cell en">1212</div>
<div class="cell en">12</div>
<div class="cell en">32</div>
</div>
<?php endforeach;?>
In your controller:
$ss['most_view_hotels'] = '23';
$this->load->view("management/header_mng_view");
$this->load->view('management/hotels_mng_view',$ss);
$this->load->view("management/footer_mng_view");
In your view:
<?php echo $most_view_hotels?>
In you're template you would refer to the item as...
$most_view_hotels
So we need to verify this is what you are doing in the view management/hotels_mng_view.php
As said on Codeigniter manual:
There is a third optional parameter lets you change the behavior of the function
so that it returns data as a string rather than sending it to your browser.
This can be useful if you want to process the data in some way. If you set
the parameter to true (boolean) it will return data.
The default behavior is false, which sends it to your browser.
What this means is that your line
$this->load->view('management/hotels_mng_view' , $ss , true);
actually returns result of that view as string to php instead of sending it to browser.
The answer to your actual question ( undefined variable ), relies in in management/hotels_mng_view.php, possibly a typo.
I think the problem is with third parameter, ie true.
As codeigniter documentation says:
There is a third optional parameter lets .....
Remember to assign it to a variable if you want the data returned
And in your code you are not assigning, returned data to any code.
I suggest you to try something like this:
public function hotels()
{
$this->load->model('model_hotels');
$data = array();
$ss['most_view_hotels'] = '23';
$data['header'] = $this->load->view("management/header_mng_view",,true);
$data['body'] = $this->load->view('management/hotels_mng_view' , $ss , true);
$data['footer'] = $this->load->view("management/footer_mng_view");
$this->load->view("template",$data);
}
/*template view file*/
<?php
echo $header;
echo $body;
echo $footer;
?>
Hope this helps.

CakePHP adding columns to a table

I have a Profile model/controller in my cake app as well as an index.ctp view in /views/profiles. Now, when I go to add a column to my table that is already filled with data, and then add the corresponding code to the view to pick up this column's data, it just gives me an empty result.
My model:
<?php
class Profile extends AppModel
{
var $name = 'Profile';
}
?>
My controller:
<?php
class ProfilesController extends AppController
{
var $name = 'Profiles';
function index()
{
$this->set('profiles', $this->Profile->find('all'));
}
}
?>
My views printing (stripped down):
<?php foreach ($profiles as $profile): ?>
<?php echo $profile['Profile']['id']; ?>
<?php echo $profile['Profile']['username']; ?>
<?php echo $profile['Profile']['created']; ?>
<?php echo $profile['Profile']['thumbnail'];?>
<?php echo $profile['Profile']['account'];?>
<?php endforeach; ?>
Basically, the columns id, username, column, thumbnail always have been printing fine, but when I add a column called accountit returns no information (nothing prints, but no errors). Any suggestions?
I would delete the files in app/tmp/cache (but keep the directories).
For CakePHP 4, clearing tmp/cache/models worked for me.

Categories