Welcome Guest, Not a member yet? Register   Sign In
Problems getting foreach loop to display data
#1

[eluser]douglasbrownca01[/eluser]
I'm getting a Trying to get property of non-object error in my for each statement

My Model:

Code:
public function get_accessories($limit) {


  // BRINGS BACK DATA BY BRAND

  $q = $this->db->select('*')
   ->from('accessories')
   ->where('category','Air Inlet')
            ->limit($limit)
   ->order_by('brand')
   ->order_by('item_code');
  
  // BUILD RESULTS QUERY
  $ret['rows'] = $q->get()->result();

  // Capcitors
  $q = $this->db->select('*')
   ->from('accessories')
   ->where('category','Capacitor')
            ->limit($limit)
   ->order_by('brand')
   ->order_by('item_code');
  
  $ret['capcitors'] = $q->get()->result();
  
  return $ret;

  }

  public function get_fingerguards($limit) {
  $rows = $this->db->select('*')
   ->from('accessories')
   ->where('category','Finger guard')
            ->limit($limit)
   ->order_by('brand')
   ->order_by('item_code');
  
  $new['fingerguards'] = $rows->get()->result();
  
  return $new;

  }



}

My controller

Code:
$results = $this->accessories_model->get_accessories($limit);
  $data['products'] = $results['rows'];
  $data['capcitors'] = $results['capcitors'];
                $data['fingerguards'] = $this->accessories_model->get_fingerguards($limit);


My view


Code:
<?php foreach($fingerguards as $fingerguard):?>
                                          <tr  padding:0;">
                                             <td ><span class="cb-ass" ></span></td>
                                             <td colspan="2"><span class="cb-ass">&lt;?php if($fingerguard->image === 'placeholder.jpg') {echo '<image src="'.base_url().'FS/products/product-image.png">';} else {echo '<image src="'.base_url().'FS/products/'.$fingerguard-&gt;id.'/product-image.png">';} ?&gt;</span></td>
                                             <td align="left"><span class="cb-ass">&lt;?php echo $fingerguard['item_code'] ?&gt;</span> </td>
                                             <td align="left"><span class="cb-ass">&lt;?php echo $fingerguard->brand;?&gt;</span> </td>
                                             <td align="left"><span class="cb-ass">&lt;?php echo $fingerguard->item_description;?&gt;</span> </td>
                                             <td align="left"><span class="cb-ass">&lt;?php echo $fingerguard->fan_type;?&gt;</span></td>

                                         </tr>
                                         &lt;?php endforeach; ?&gt;


The two data sets from the $results variable are working fine and I'm able to display the rows. But not with the fingerguards variable. What I'm I missing?
#2

[eluser]Rowan Wilson[/eluser]
Does this work?

Code:
$results = $this->accessories_model->get_accessories($limit);
$fingerguards_result = $this->accessories_model->get_fingerguards($limit);
$data['products'] = $results['rows'];
$data['capcitors'] = $results['capcitors'];
$data['fingerguards'] = $fingerguards_result['fingerguards']

Probably need to rethink the $fingerguards_result variable name but it'll do for testing.

P.s. you're missing an A from capcitors. Unless that's deliberate ;-)
#3

[eluser]douglasbrownca01[/eluser]
Thanks for your reply. No, unfortunately, it doesn't. I get an empty response and a blank page. I tested the query in the database for fingerguards and it works fine. The "A" in capacitors was a mistake but carried all the way through so it wasn't an issue.

If I understand this right, I'm creating an array and sending the results to the view, but it is like it's not bringing back the rows.
#4

[eluser]Rowan Wilson[/eluser]
Out of interest, if you var_dump your $fingerguards variable in your view are your results there?
#5

[eluser]douglasbrownca01[/eluser]
Yes, I'm getting this result in my view from my controller

Code:
["fingerguards"]=>
  array(11) {
    [0]=>
    object(stdClass)#19 (15) {
      ["id"]=>
      string(1) "9"
      ["category"]=>
      string(12) "Finger guard"
      ["images"]=>
      string(2) "no"
      ["brand_grouping"]=>
      string(1) "2"
      ["brand"]=>
      string(6) "Ecofit"
      ["image"]=>
      string(0) ""
      ["item_code"]=>
      string(7) "21194-B"
      ["item_description"]=>
      string(66) "ECOFIT FINGERGUARDFOR 200 MM AND 225 MM DIAMETER FANS, ZINC COATED"
      ["free_to_sell"]=>
      string(2) "20"
      ["dim_1"]=>
      string(3) "200"
      ["dim_2"]=>
      string(3) "225"
      ["voltage"]=>
      string(0) ""
      ["fan_type"]=>
      string(5) "AXIAL"
      ["spec_sheet"]=>
      string(0) ""
      ["catalog"]=>
      string(0) ""
    }

Repeats ll times...
#6

[eluser]Rowan Wilson[/eluser]
ok cool, your result is there so that's good. I just wanted to make sure everything came through the same as your $results query as you said that worked.

Revert to how you previously were passing your fingerguard variable straight into data.

Code:
$data['fingerguards'] = $this->accessories_model->get_fingerguards($limit);

The non-object error is probably because, in your view you are trying to echo out item code as an array rather than an object like all the others.

Here:
Code:
&lt;?php echo $fingerguard['item_code'] ?&gt;
Should be:
Code:
&lt;?php echo $fingerguard->item_code ?&gt;
#7

[eluser]douglasbrownca01[/eluser]
Right. This is the way I had it originally. I that snippit was an me taking another tact. I've got

Code:
&lt;?php foreach($fingerguards as $fingerguard):?&gt;
as my foreach statement - $fingergards is the variable with the results from the controller and
each variable I'm trying to bring back looks like this:
Code:
&lt;?php echo $fingerguard->item_code ?&gt;

I'm still getting the property of non-object error in each instance. I'm new to code ignitor, but it seems like it's how the data is being stored in the results array and that the for each statement can't interpet it correctly. From looking at other code examples, this should work. I have to other statements in my model that are bring back data perfectly. The whole model is listed above. Thanks for the replys, would appreciate any insight you might have.
#8

[eluser]Rowan Wilson[/eluser]
Without seeing the var_dump from your last change I would guess you need to access the results as:

Code:
$fingerguard['0']->item_code

etc.
#9

[eluser]douglasbrownca01[/eluser]
Progress --- that pulls out on instance of the array and displays correctly. How do I loop through and get them all? The array dump is exactly as above...I'm getting the same dump.
#10

[eluser]Rowan Wilson[/eluser]
You were on the right track:

Code:
&lt;?php foreach($fingerguards as $fingerguard):?&gt;

<tr  padding:0;">
<td ><span class="cb-ass" ></span></td>
<td colspan="2"><span class="cb-ass">

&lt;?php

if($fingerguard['0']->image === 'placeholder.jpg')
{
echo '<image src="' . base_url() . 'FS/products/product-image.png">';
}
else
{
echo '<image src="'. base_url() . 'FS/products/' . $fingerguard['0']-&gt;id . '/product-image.png">' ;
}

?&gt;
</span></td>

<td align="left"><span class="cb-ass">&lt;?php echo $fingerguard['0']->item_code; ?&gt;</span> </td>
<td align="left"><span class="cb-ass">&lt;?php echo $fingerguard['0']->brand; ?&gt;</span> </td>
<td align="left"><span class="cb-ass">&lt;?php echo $fingerguard['0']->item_description; ?&gt;</span> </td>
<td align="left"><span class="cb-ass">&lt;?php echo $fingerguard['0']->fan_type; ?&gt;</span></td>
</tr>

&lt;?php endforeach; ?&gt;

I've spaced it out a little so that I could read the conditionals.




Theme © iAndrew 2016 - Forum software by © MyBB