Undefined property: stdClass - El Forum - 01-30-2011
[eluser]Unknown[/eluser]
Hi im having these two errors and I can`t find whats wrong... propably that I just started to use codeigniter.
Message: Undefined property: stdClass::$quantity
Filename: products/index.php
Line Number: 28
Message: Undefined property: stdClass::$website
Filename: products/index.php
Line Number: 32
products/index.php
Code: <?php
Header('Cache-Control: no-cache');
Header('Pragma: no-cache');
$this->load->view('header');
$this->load->view('invoices/invoice_new');
?>
<h2><?php echo $page_title;?></h2>
<?php if ($message != ''):?>
<p class="error topmessage"><?php echo $message;?></p>
<?php endif;?>
<p class="button">
<?php echo anchor('products/newproduct', $this->lang->line('products_create_new_product'), array('class'=>'productnew'));?>
</p>
<?php foreach($all_products->result() as $product): ?>
<h3 class="display productHeader productHeader<?php echo $product->id;?>">
<a class="displayLink">id;?>" href="#productHeader<?php echo $product->id;?>"><?php echo $product->name;?></a></h3>
<div class="productInfo" id="productInfo<?php echo $product->id;?>">
<p>
<?php if ($product->name != '') {echo $product->name;}?>
<?php if ($product->quantity != '') {echo $product->quantity;}?>
<?php if ($product->jm != '') {echo $product->jm;}?>
<?php if ($product->value != '') {echo ', ' . $product->value;}?>
<?php echo auto_link(prep_url($product->website));?>
</p>
<p class="product_options">
<?php echo anchor('products/notes/'.$product->id, $this->lang->line('products_notes'), array('class' => 'product_notes'));?> |
<?php echo anchor('products/edit/'.$product->id, $this->lang->line('products_edit_product'), array('class' => 'product_edit'));?> |
<?php echo anchor('products/delete/'.$product->id, $this->lang->line('products_delete_product'), array('class'=>'lbOn deleteConfirm product_delete'));?>
</p>
</div>
controllers/products.php
Code: function index()
{
$data['clientList'] = $this->clients_model->getAllClients();// activate the option
$data['extraHeadContent'] = "<link type=\"text/css\" rel=\"stylesheet\" href=\"" . base_url()."css/products.css\" />\n";
$data['extraHeadContent'] .= "[removed][removed]\n";
if ($this->session->flashdata('product_edit'))
{
$data['message'] = $this->lang->line('products_edited');
$data['extraHeadContent'] .= "[removed]\nfunction openCurrent() {\n\tEffect.toggle ('product_info".$this->session->flashdata('product_edit')."', 'Blind', {duration:'0.4'});\n}\naddEvent (window, 'load', openCurrent);\n[removed]";
}
else
{
$data['message'] = $this->session->flashdata('message');
}
$data['total_rows'] = $this->products_model->count_all_products();
// Run the limited version of the query
$data['all_products'] = $this->products_model->get_all_products();
$this->_validation_products(); // validation info for id, name, jm(type of measurment), value
$data['page_title'] = $this->lang->line('menu_products');
$this->load->view('products/index', $data);
}
products_model.php
Code: function get_all_products()
{
$this->db->orderby('name', 'asc');
return $this->db->get('products');
}
Appreciate the help
Undefined property: stdClass - El Forum - 01-30-2011
[eluser]Twisted1919[/eluser]
orderby() is deprecated as far as i know, so this can be a reason.
Other than that, the thing is that you don't get any records back from your query, i can say that if you var_dump($all_products) in your view, you won't have any rows to iterate through.
i would reqrite the query as:
Code: function get_all_products()
{
$q=$this->db->select('*')->from('products')->order_by('name', 'asc')->get();
return $q->num_rows() > 0 ? $q->result() : NULL ;
}
then in your view:
Code: if(!empty($all_products))
{
foreach($all_products AS $product)
{
// do things here with $product->
}
}
|