Welcome Guest, Not a member yet? Register   Sign In
Undefined variable: getproductdetail and Invalid argument supplied for foreach()
#1

[eluser]freshigniter[/eluser]
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 :bug:
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

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

Code:
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


Code:
<?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.
#2

[eluser]InsiteFX[/eluser]
Code:
function products()
{
    $product_id = $this->uri->segment(3, 0);

    $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:
Code:
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.
#3

[eluser]freshigniter[/eluser]
[quote author="InsiteFX" date="1335520738"]
Code:
function products()
{
    $product_id = $this->uri->segment(3, 0);

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


[/quote]
i tried the above code
same problem exists...
#4

[eluser]freshigniter[/eluser]
SOLVED..................


the problem was in view
Code:
&lt;?php echo $getproductdetail->product_name;?&gt;<br />
&lt;?php echo $getproductdetail->description;?&gt;<br />
&lt;?php echo $getproductdetail->price;?&gt;<br />
<img src="&lt;?php  echo  base_url().'productimages/'.$getproductdetail-&gt;image; ?&gt;" height="190px" width="190px" />





Theme © iAndrew 2016 - Forum software by © MyBB