Trying to get property of non-object error using codeigniter - php

I've problem with codeigniter. this is the message
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: employee/detail.php
Line Number: 15
I heve model like this:
public function getByID($id){
$this->db->select('*');
$this->db->from('master_employee me');
$this->db->join('master_position mp', 'mp.mp_id=me.mp_id');
$this->db->where('me.me_id', $id);
$query = $this->db->get();
return $query->row();
}
Controller:
public function edit($id){
/**
* [$data get data from database]
* #var array
*/
$data = array($id);
$data['msg'] = $this->_get_flashdata();
$data['rows'] = $this->m_employee->getByID($id);
$data['position'] = $this->m_position->get();
/**
* [$html call all wireframe]
* #var array
*/
$html = array();
$html['header'] = $this->load->view('admin/header',$data,true);
$html['kiri'] = $this->load->view('admin/kiri',null,true);
$html['content'] = $this->load->view('admin/employee/edit',$data,true);
$this->load->view('admin/template',$html);
}
and view like this
<?php if($rows->me_photo == NULL): ?>
<img src="<?php echo base_url('/upload/be/employee/default-no-image.png'); ?>" class="img-responsive" title="no-photo" style="margin-bottom:10px" />
<?php else: ?>
<img src="<?php echo base_url('/upload/be/employee'.$rows->me_name); ?>" class="img-responsive" style="margin-bottom:10px" />
<?php endif; ?>
How to solve my problem? Please

Try this one:
<?php if($rows['me_photo'] == NULL): ?>
<img src="<?php echo base_url('/upload/be/employee/default-no-image.png'); ?>" class="img-responsive" title="no-photo" style="margin-bottom:10px" />
<?php else: ?>
<img src="<?php echo base_url('/upload/be/employee'.$rows['me_name']; ?>" class="img-responsive" style="margin-bottom:10px" />
It's an array. Not object. -> stands for object.
You need to change $rows->m_ephoto like this $rows['me_photo'].

Modify your query
public function getByID($id){
$this->db->select('*');
$this->db->from('master_employee me');
$this->db->join('master_position mp', 'mp.mp_id=me.mp_id');
$this->db->where('me.me_id', $id);
$query = $this->db->get();
return $query->result();
}
in in view use like this
foreach ($query->result() as $row){
echo $row->title;
}
read here i hope you will get the answer

Related

echo user information on a product not working in codeigniter

I have been trying to echo user information on a product but its not working. I used to try it with this join function in my model :
public function getdata()
{
$this->db->select('*');
$this->db->from('users');
$this->db->join('products','products.user_id = users.user_id');
$query = $this->db->get();
if($query->num_rows()>0)
{
return $query->result_array();
}
}
It didn't work properly because it echoed for example all the usernames from the database when I tried to echo a username that belongs to the same user_id.
Now I tried to do it like this:
This is my Controller function (AlleCadeausController):
public function index()
{
/** Laad de models **/
$this->load->model('Product_model');
$selectProducts = $this->Product_model->selectProducts();
foreach ($selectProducts as $key => $product)
{
# first name
$user = $this->Product_model->get_user_name($product['user_id']);
#append both NEW VALUES to same array
$data[$key]['voornaam'] = $user[0]['voornaam'];
}
/** Make $data **/
$data['products'] = $this->Product_model->selectProducts();
/** Laad de allecadeaus view en geef $data mee **/
$this->load->view('allecadeaus', $data);
}
My model functions in my Prduct_model.php file:
public function selectProducts()
{
$query= $this->db->query("SELECT * FROM products");
$result = $query->result_array();
return $result;
}
function get_user_name($name)
{
$query= $this->db->query("SELECT voornaam FROM users WHERE user_id = $name ");
$result = $query->result_array();
return $result;
}
And this is how I tried to echo the user information in the product foreach loop in the view:
<?php foreach($products as $product) : ?>
<a href="<?php echo base_url() ?>/Product/details/<?php echo $product["product_id"]; ?>">
<div class="main-products">
<img id="cadeaufoto" src="<?php echo base_url(); ?>upload/<?php echo $product["product_foto_thumb"]; ?>">
<div class="product_naam"><?php echo $product["product_naam"]; ?></div>
<div class="ophaal_plaats"><?php echo $product["ophaal_plaats"]; ?></div>
</div>
</a>
<div class="aangeboden_door">
<p>
<tr>
<a href="<?php echo base_url() . 'User/userdetails/'.$product['user_id'];?>">
<td><?php echo $product['voornaam'];?></td>
</tr>
</p>
</div>
<?php endforeach; ?>
at this line :
<td><?php echo $product['voornaam'];?></td>I try to echo the first
name of the user of the product but its not working.
The PHP error that I'm receiving is: undefined index: voornaam , line: 55 which is this line: <td><?php echo
$product['voornaam'];?></td>
Database information of both tables products and users:
Table 1 : products:
product_id
product_naam
product_beschrijving
user_id
ophaal_plaats
product_foto
product_foto_thumb
date_created
date_updated
category_id
table 2 : users:
user_id
email
voornaam
achternaam
beschrijving
profiel_foto
You can use the active records instead of create multiple function:
Your controller:
public function index()
{
$data = array();
$this->load->model('product_model');
$data['products'] = $this->product_model->selectProducts();
$this->load->view('allecadeaus', $data);
}
Your view:
Your view:
<?php foreach($products as $product) : ?>
<a href="<?php echo base_url() ?>/Product/details/<?php echo $product["product_id"]; ?>">
<div class="main-products">
<img id="cadeaufoto" src="<?php echo base_url(); ?>upload/<?php echo $product["product_foto_thumb"]; ?>">
<div class="product_naam"><?php echo $product["product_naam"]; ?></div>
<div class="ophaal_plaats"><?php echo $product["ophaal_plaats"]; ?></div>
</div>
</a>
<div class="aangeboden_door">
<p>
<tr>
<?php
//Here is the active record query which is getting the 'voorname' and other data
$userarray = $this->db->get_where('users', array('user_id'=>$product["user_id"]))->row_array();
// you can print_r($userarray); for see the array you get
?>
<a href="<?php echo base_url() . 'User/userdetails/'.$product['user_id'];?>">
<td><?php echo $userarray['voornaam'];?></td>
</tr>
</p>
</div>
<?php endforeach; ?>

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

Can't define variable for link to edit page

I am building a blog and trying to create a link that goes to an edit page. My database table blog_posts has the following variables: postDate, postID, userId, postDesc. They all display correctly on my render page for each blog record.
I now am trying to create a link to my edit page and having trouble getting my $blog['edit'] variable below to work. I have tried defining it in both the Model and controller - no luck.
Thanks for any help.
CONTROLLER:
public function indexAction() {
$request = $this->getRequest();
$this->view->blogs = Blog_Model_Blog::findAll();
$blogs = Blog_Model_Blog::getAllBlogEntries();
if($blogs) {
foreach($blogs AS $blog) {
$blog['edit'] = WM_Router::create($this->getRequest()->getBaseUrl() . '?module=blog&controller=admin&action=edit&id=' . $blog['postID']);
$this->view->blogs[] = $blog;
}
}
}
MODEL:
public static function getAllBlogEntries() {
$db = JO_Db::getDefaultAdapter();
$query= $db->select ()
-> from ('blog_posts',array('*'))
-> order ('blog_posts.postDate DESC');
return $db->fetchAll($query);
}
public static function findAll($_callbackQuery = null) {
$blog = parent::findAll($_callbackQuery);
return $blog;
}
VIEW:
<?php if ($this->blogs) { ?>
<?php foreach ($this->blogs AS $blog) { ?>
<tr id="<?php echo $blog->postID; ?>">
<td><?php echo $blog->postID; ?></td>
<td><?php echo $blog->userId; ?></td>
<td align="center">
<img title="<?php echo $this->translate('Edit');?>" alt="" class="tooltip" src="cms/admin/images/edit.png">
</td>
</tr>
<?php } ?>
<?php } ?>

Loading images from database php codeigniter

Im trying to load images from the database, i can get the full string and no display on the image and when i use the full array I get a array message.
here is the model :
public function get_webstore($webstore_id = '') {
/* get video */
if (empty($webstore_id)) {
$webstores = $this->get('webstore');
} else {
$webstores = $this->get('webstore', $webstore_id);
}
$webstore_array = array();
foreach ($webstores as $webstore) {
$singleWebstorePair = array();
if (!empty($webstore['images'])) {
$allWebstores = unserialize($webstore['images']);
foreach ($allWebstores as $allWestores) {
$singleWebstorePair[] = str_replace("./", "", base_url($allWebstores));
}
}
$webstore_array[] = array(
'id' => $webstore['id'],
'title' => $webstore['title'],
'content' => $webstore['content'],
'images' => $singleWebstorePair,
'created' => $webstore['created']
);
}
return $webstore_array;
}
and here is the front end code
<div class="col-md-12" style="height: 225px;">
<div class="col-md-4 text-center thumbnail123">
<img class="thumbnail" width="225px" src="<?php echo $webstore['images']?>.jpg"/>
<h3><?php echo $webstore['title']; ?></h3>
<p>Date Added: <?php echo date('d-m-y', strtotime($webstore['created'])) ?></p>
<hr>
<hr>
</div>
here is the controller if needed:
public function __construct() {
parent::__construct();
$this->load->model("post");
$this->load->database();
$this->load->helper("url");
}
public function index() {
$this->data['posts'] = $this->post->get_webstore($limit=null, $offset=null); // calling Post model method getPosts()
$this->data['page_title'] = 'Store';
$this->layout("pages/webstore", $this->data);
and the error:
Severity: Notice
Message: Array to string conversion
Filename: pages/webstore.php
Line Number: 45
thanks guys
When you populate your array, you set 'images' => $singleWebstorePair,˙ which is an array.
In your template, your are echoing this:
<img class="thumbnail" width="225px" src="<?php echo $webstore['images']?>.jpg"/>
But here $webstore['images'] is an array. So if $webstore['images'] holds multiple image source, then loop through it:
<?php foreach($webstore['images'] as $image) : ?>
<img class="thumbnail" width="225px" src="<?php echo $image ?>.jpg"/>
<?php endforeach; ?>

Message: Undefined property: stdClass in codeigniter

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.

Categories