Message: Undefined property: stdClass in codeigniter - php

I got this error:
A PHP Error was encountered Severity: Notice Message: Undefined property: stdClass::$positionsNum
when i run.
My model:
public function load_job($id){
$Where = array('jobs.id' => $id);
$this->db->select('jobs.id, positionsNum, companyName, contract_type.titleEN as contractType');
$this->db->from('jobs');
$this->db->join('contract_type', 'jobs.contractTypeId = contract_type.id', 'left');
$this->db->where($Where);
$Result = $this->db->get();
return $Result->first_row() ;
}
my view:
<?php if(isset($job) && count($job) > 0){ ?>
<div class="row job-detail-row">
<div class="col-lg-6 job-detail-label"><?php echo lang('contractType'); ?></div>
<div class="col-lg-6"><?php if(isset($job->contractType)) echo $job->contractType; ?></div>
</div>
<div class="row job-detail-row">
<div class="col-lg-6 job-detail-label"><?php echo lang('num_vacancies'); ?> </div>
<div class="col-lg-6"><?php echo $job->positionsNum; ?></div>
</div>
<?php } ?>
my Controller's function:
public function job() {
$job_id = $this->uri->segment(3);
if(isset($job_id) && !empty($job_id))
{
//one job
$job = $this->job_model->load_job($this->GetLang, $job_id);
$data['job'] = $job;
$this->load->view( 'front/singleJob', $data);
}
}
Also, i printed the returned object and it returns this:
stdClass Object(
[id] => 9
[positionsNum] => 2
[companyName] =>
[contractType] => type1
)
But, contractType won't be printed in page even if it is already has a value.
I can't figure out the problem. Any help?

The problem is here.
you have a function with one parameter.
public function load_job($id);
but in controller you are passing two values.
Try to pass correct values.
$job = $this->job_model->load_job($this->GetLang, $job_id);
it should be like this.
$job = $this->job_model->load_job( $job_id);

try like this
public function job() {
$job_id = $this->uri->segment(3);
if(isset($job_id) && !empty($job_id))
{
//one job
$data['job'] = $this->job_model->load_job($this->GetLang, $job_id);
$this->load->view( 'front/singleJob', $data);
}
}

So, it is solved now :)
This problem happened twice in two files:
First is which stated in my question, and I had an error in another query at the same page.
Second problem was in the HTML file. My complete view was like this:
<?php if(isset($service) && count($service) > 0){ ?>
<div class="row job-detail-row">
<div class="col-lg-6 job-detail-label"><?php echo lang('contractType'); ?></div>
<div class="col-lg-6"><?php if(isset($service->contractType)) echo $service->contractType; ?></div>
</div>
<?php } ?>
<ul>
<?php foreach ($similarServices as $service) { ?> <<========= NOTICE: $service here is the same as the above condition
<li>
<div class="title"><?php echo $service->title; ?>
</div>
</li>
<?php }//end foreach similarServices ?>
</ul>
It appeared because i used the same variable name $service, so i changed it and it works now. Hope this helps you.

Related

Warning: Invalid argument supplied for foreach()

I have an error in the product detail, I do not know what else to change where again. when I change $detail into $categories succeed but is the same as function submenu. It was not according to my wishes
My View
<!-- navbar -->
<div class="row">
<?php foreach ($categories as $row) : ?>
<div class="col-sm-3">
<h5><?php echo $row->categoriesName; ?></h5>
<ul><li><a href="<?php echo base_url();?>member/detail">
<?php echo $row->productName; ?></a> </li></ul>
</div>
<?php endforeach; ?>
</div>
<!-- product detail -->
<?php foreach ($detail as $details_new) : ?>
<div class="box">
<h1 class="text-center"><?php echo $details_new->productName;?></h1>
<p class="price"><?php echo $details_new->price;?></p>
</div>
<?php endforeach; ?>
My Controller
public function home() {
$data=array('title'=>'Pasar Online | Ayo Belanja Sekarang',
'username'=> $this->session->userdata('username'),
'product' => $this->product_model->all(),
'categories' => $this->categories_model->get_main(),
'isi' =>'member/index');
$this->load->view('template_home/wrapper',$data);
}
public function detail() {
$data['username'] = $this->session->userdata('username');
$data['categories'] = $this->categories_model->get_main();
$data['detail'] = $this->categories_model->get_details();
$this->load->view('member/coba',$data);
}
My Model
public function get_main(){
$this->db->select('product.*,categories.*');
$this->db->from('product');
$this->db->join('categories','product.categoriesID=categories.categoriesID');
$this->db->limit(5);
$query = $this->db->get();
if($query->num_rows() > 0) {
$results = $query->result();
} return $results;
}
public function get_details() {
$query = $this->db->query("SELECT * FROM product WHERE productID");
$row = $query->row();
}
still can not display a single product
I do not know where my mistake... please help me, thanks
you are iterating details array in wrong format.
public function get_details() {
$query = $this->db->query("SELECT * FROM product WHERE productID");
$row = $query->row();
return $row;
}
This will return only single row without any indexing like - 0,1,2...
and on view page you are using foreach loop and foreach loop use indexing to iterate value you can iterate like that --
<div class="box">
<h1 class="text-center"><?php echo $detail->productName; ?></h1>
<p class="price"><?php echo $detail->price; ?></p>
</div>
please use this way hope it will work..
In codeigniter row() selects only first row of your result in object format.So no need to use foreach loop.Just try like this...
<div class="box">
<h1 class="text-center"><?php echo $detail->productName;?></h1>
<p class="price"><?php echo $detail->price;?></p>
</div>
And
In model
public function get_details() {
$query = $this->db->query("SELECT * FROM product WHERE productID");
$row = $query->row();
return $row;
}
it is because of $detail variable is empty, no data available in this variable, always check for data in variable before looping
<?php if(!empty($detail)){
foreach ($detail as $details_new) : ?>
<div class="box">
<h1 class="text-center"><?php echo $details_new->productName;?></h1>
<p class="price"><?php echo $details_new->price;?></p>
</div>
<?php endforeach; } ?>

Notice Message: Array to string conversion

I'm a starter in codeigniter and php, and im facing this problem
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: query
Filename: inc/header_main_view.php
Line Number: 175
A PHP Error was encountered
VIEW:
<ul class="dropdown-menu">
<li>
<!-- inner menu: contains the actual data -->
<ul class="menu">
<?php foreach($query as $row) :?>
<li><!-- Task item -->
<a href="#">
<h3>
<?php echo $row->id_task; ?><?php echo $row->name; ?>
<small class="pull-right">20%</small>
</h3>
<div class="progress xs">
<div class="progress-bar progress-bar-aqua" style="width: 20%" role="progressbar" aria-valuenow="20" aria-valuemin="0" aria-valuemax="100">
<span class="sr-only">20% Complete</span>
</div>
</div>
</a>
</li><!-- end task item -->
<?php endforeach; ?>
</ul>
</li>
<li class="footer">
View all tasks
</li>
</ul>
CONTROLLER
public function get_todo($id = null)
{
$this->_require_login();
$this->load->model('todo_model');
if ($id != null) {
$result = $this->todo_model->get([
'id' => $id,
'id_user' => $this->session->userdata('id_user')
]);
} else {
$result = $this->todo_model->get([
'id_user' => $this->session->userdata('id_user')
]);
}
return $result;
}
OTHER CONTROLLER
public function index()
{
require('api.php');
$api = new api();
$query = $api->get_todo();
$this->load->view('dashboard/inc/header_main_view', $query);
$this->load->view('dashboard/admin_pages/dashboard_view');
$this->load->view('dashboard/inc/footer_main_view');
}
MODEL
public function get($id = null, $order_by = null)
{
if (is_numeric($id)) {
$this->db->where($this->_primary_key, $id);
}
if (is_array($id)) {
foreach ($id as $_key => $_value) {
$this->db->where($_key, $_value);
}
}
$q = $this->db->get($this->_table);
return $q->result_array();
}
Can someone please tell me why I am getting the error.
It will be like this
$data['query'] = $api->get_todo();
$this->load->view('dashboard/inc/header_main_view', $data);
You did not passed variable to view right way.$query is not available at your view.CI converts array or object keys as variable which is sent from controller.See Adding Dynamic Data to the View From the doc.
You need to fix your controller like this
$data['query'] = $api->get_todo();
$this->load->view('dashboard/inc/header_main_view', $data);
//your view will get all key of $data as variable.
//your view will get $query from above code

my query in codeigniter cannot produce result

i've been debbuging it many times based on my knowledge, i am new to codeigniter, any solution idea would be much appreciated... ty
here's my code
MODEL:
function userdisplay_info($id){
$result = $this->db->query("SELECT uid FROM user WHERE userIdentificationNumber LIKE '$id'");
//if($result->num_rows() > 0){
$info_data = $result->result();
$row = $info_data[0];
$result->free_result();
}
CONTROLLER:
function r_book(){
$data = array();
$bid = $this->input->post('bid');
$u_id = $this->input->post('userinputid');
$data = array(
'book_reserved' => 1
);
if($this->module2->reserved_book($bid,$data))
{
$data['info'] = $this->module2->userdisplay_info($u_id);
$this->load->view('User/template/header');
$this->load->view('User/template/navigator');
$this->load->view('User/landingpage',$data);
$this->load->view('User/template/footer');
return true;
}else
{
$this->load->view('User/template/header');
$this->load->view('User/template/navigator');
$this->load->view('User/landingpagefail');
$this->load->view('User/template/footer');
return false;
}
}
VIEW:
<div class="col-md-3">
<div class="panel panel-default">
<div class="panel-body">
<?php if($info) : foreach($info as $u_info) : ?>
<ul class="list-unstyled">
<li>Last Name : <?php echo $u_info->uid; ?></li>
<li>First Name : <?php echo $u_info->userFirstname; ?></li></li>
<li>Middle Name : <?php echo $u_info->userMiddlename; ?></li></li>
<li>Course : <?php echo $u_info->userCourse; ?></li></li>
</ul>
<?php endforeach; ?>
<?php else :?>
<h4> No Record Found</h4>
<?php endif; ?>
</div>
</div>
</div>
Rewrite your model function as
function userdisplay_info($id){
$this->db->select('uid');
$this->db->like('userIdentificationNumber', $id);
$query = $this->db->get('user');
return $query->result_array();
}

error getting the array result in Join Query in Codeigniter

Hello this is my query and I have the problem getting the result_array().
This is the error:
Fatal error: Call to a member function result_array() on a non-object in C:\xampp\htdocs\plss\application\views\Admin\post_refresh.php on line 6
<?php
$this->db->select('*');
$this->db->from('forum_thread');
$this->db->join('useraccount','useraccount.user_id = forum_thread.user_id');
$post= $this->db->get();
foreach($post->result_array() as $row): ?>
<div class="panel panel-default" >
<div class="panel-body">
<p>
<small>Clinton Puds</small>
<small style="float:right;color:#808080;font-size:10px;"><b>Posted Last</b> <?php echo $row['date_last_post']?></small>
</p>
<p><?php echo $row['slug'];?></p>
</div>
<div class="panel-footer"><small>Comment</small></div>
</div>
<?php endforeach ?>
Try this:
$post = $this->db->get()->result_array();
foreach($post as $row): //etc
Try this :
$post->result() as $row

PHP Error: Trying to get property of non-object

I am working in CodeIgniter framework and I have tried to apply all the other solutions I found on stack but I could not make it work so here is my problem...
I am trying to retrieve a record from MySQL database table called 'questions' based on uri segment. Then I am trying to display this record in a view. As far as I can tell, the uri segment is passed along everywhere it needs to be, and record is retrieved from database.
The problem comes up when I am trying to access the data from the controller, in my view.
Error I am getting for each echo in my loop in 'view_thread_view' is
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: views/view_thread_view.php
Any solutions would be greatly appreciated.
Here is my code:
Controller thread
function view_thread()
{
$quest = $this->uri->segment(3);
echo $quest;// for checking if uri segment is passed
if($query = $this->data_model->get_thread($quest))
{
$data['records'] = $query;
}
$this->load->view('view_thread_view', $data);
Model data_model
public function get_thread($quest)
{
if($q = $this->db->get_where('questions' , 'qid' , $quest));
{
return $q;
}
}
View view_thread_view
<div title="question_view">
<h1>Question View<h1>
<?php foreach($records as $data) : ?>
<div>
<h2>Title</h2>
<?php
echo $data->title;
?>
</div>
<div>
<h2>Question</h2>
<?php
echo $data->contents
?>
</div>
<div>
<h2>Tags</h2>
<?php
echo $data->tags
?>
</div>
<div>
Thread owner
<?php
echo $records->uname
?>
</div>
<?php endforeach; ?>
</div>
EDIT: QUESTION ANSWERED
Thanks to Girish Jangid fixed the problem this is the working code:
Controller
function view_thread()
{
$quest = $this->uri->segment(3);
echo $quest;// for checking if uri segment is passed
//$data = array();
if($query = $this->data_model->get_thread($quest))
{
$data['records'] = $query;
}
$this->load->view('view_thread_view', $data);
}
Model
public function get_thread($quest)
{
if($q = $this->db->get_where('questions' , array('qid' => $quest)));
{
return $q;
}
}
View
<div title="question_view">
<h1>Question View<h1>
<?php foreach($records->result() as $data) : ?>
<div>
<h2>Title</h2>
<?php
echo $data->title;
?>
</div>
<div>
<h2>Question</h2>
<?php
echo $data->contents
?>
</div>
<div>
<h2>Tags</h2>
<?php
echo $data->tags
?>
</div>
<div>
Thread owner
<?php
echo $data->uname
?>
</div>
<?php endforeach; ?>
</div>
</div>
You should user db function to fetch results, please use CodeIgniter db Query Results functions, like this
if($records->num_rows() > 0){
foreach($records->result() as $data){
if(isset($data->title)) echo $data->title;
}
}
For more detail please read CodeIgniter Query Result function
Query Results
Try this code
<div title="question_view">
<h1>Question View<h1>
<?php if($records->num_rows() > 0) { ?>
<?php foreach($records->result() as $data) : ?>
<div>
<h2>Title</h2>
<?php
echo $data->title;
?>
</div>
<div>
<h2>Question</h2>
<?php
echo $data->contents
?>
</div>
<div>
<h2>Tags</h2>
<?php
echo $data->tags
?>
</div>
<div>
Thread owner
<?php
echo $records->uname
?>
</div>
<?php endforeach; ?>
<?php } ?>
</div>
$records is an array, not an object, therefore you cannot do $records->uname. You probably meant $data there too.
Edit: On second look, it appears $data is an array as well! Arrays are accessed via ['key'] not ->key
Also, your code is way over complicated.
<?php
foreach($records as $data){
$string = <<<STR
<div>
<h2>Title</h2>
{$data['title']}
</div>
...etc
STR;
echo $string;
}
http://www.php.net/manual/en/language.types.string.php

Categories