EllisLab text mark
Advanced Search
     
Undefined variable: getproductdetail and Invalid argument supplied for foreach()
Posted: 27 April 2012 05:23 AM
Avatar
Joined: 2012-04-20
8 posts

hello genius men,am really happy to post here as i always get a lot of responses within a short span of time , which indeed helps me a lot. cheese so hers is another bug for you guys.
have a happy Helping big surprise
about my project: its an online store, with the main categories displayed on login page, the user hover the mouse over these categories to get subcategories[sc]. if he clicks on the SC he will be directed to a page where all the products with that SCs will be displayed.
MY PROBLEM IS : when i click those products the controller will take me to the URL[written below] with product id, with the following error.


A PHP Error was encountered

in the URL :http://localhost/business/index.php/detail/products/12

controller:detail
 

function products($id='')
 
{
    $this
->load->model('Product_model');
    
$data['getproductdetail']=$this->Product_model->get_productdetail($id);
    
$this->load->view('detail/product');
 

  model:Product_model   

function get_productdetail($id)
 
{
  $this
->db->select('*');
  
$this->db->from('product');
  
$this->db->where('id',$id);
  
$query=$this->db->get();
  return 
$query->row();
 


VIEW:product.php

<?php 


  
foreach($getproductdetail as $product)
        
echo $product->product_name; echo $product->description}
 ?> 

 

DATABASE NAME: business
TABLE NAME:product

——————————————————————————————————————————
id |product_name|category_id| subcategory_id| description       |  price
——————————————————————————————————————————
8 |home     |  14   |  14   |build home within budget |  $1000
7 |antivirus   |  12   |  9         |protects all your digital data|  $500
6 |rice1     |  1   | 1         |awesome rice,easy to cook   |  $50/kg
——————————————————————————————————————————


RESULT:

 

Home
PRODUCTS
————————————————————

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: getproductdetail

Filename: detail/product.php

Line Number: 12
————————————————————
————————————————————
A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: detail/product.php

Line Number: 12

————————————————————

please let me know where am going wrong.

 
Posted: 27 April 2012 05:58 AM   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2009-06-19
6593 posts
function products()
 
{
    $product_id 
$this->uri->segment(30);

    
$this->load->model('Product_model');
    
$data['getproductdetail']=$this->Product_model->get_productdetail($product_id);
    
$this->load->view('detail/product'$data);
 

If that does not work:

echo var_dump($data); 

To see if you are getting the values from your model…

The rest of your errors are probably caused because your not getting your product id.

 

 Signature 

Certified State of CT Computer Programming Teacher.
Custom Designed Icons, eBook Covers Software Boxes. CD, DVD Etc. New iPhone® Tab Bar Icons and iPhone® Applications Icons.

Skype: insitfx

STOP! Before posting your questions, remember the WWW Golden rule:
What did you try? What did you get? What did you expect to get?

Input -> Controller | Processing -> Model | Output -> View

 
Posted: 27 April 2012 06:16 AM   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2012-04-20
8 posts
InsiteFX - 27 April 2012 05:58 AM
function products()
 
{
    $product_id 
$this->uri->segment(30);

    
$this->load->model('Product_model');
    
$data['getproductdetail']=$this->Product_model->get_productdetail($product_id);
    
$this->load->view('detail/product'$data);
 

 

i tried the above code
same problem exists…

 
Posted: 27 April 2012 07:51 AM   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2012-04-20
8 posts

SOLVED…...............

the problem was in view

<?php echo $getproductdetail->product_name;?><br />
<?php echo $getproductdetail->description;?><br />
<?php echo $getproductdetail->price;?><br />
<
img src="<?php  echo  base_url().'productimages/'.$getproductdetail->image; ?>" height="190px" width="190px" />