CodeIgniter Forums
errror Result() - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: errror Result() (/thread-3799.html)



errror Result() - El Forum - 10-22-2007

[eluser]MMCCQQ[/eluser]
Fatal error: Call to a member function result() on a non-object in /home/tunerspo/public_html/application/views/spot_side.php on line 8


why???

spot_side.php
Code:
<div id="wrap_contx">
    <div id="sidebar">
        <div id="t_in">Photos of This Spot</div>
        <div id="div_details">
    Add Photos
    </div
&lt;?php foreach($query->result() as $row):?&gt;

<img src="&lt;?=$row->thumb;?&gt;">


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

Code:
function GetGallerySpot()
    {
        
        
        $this->db->where('id_spot',$this->uri->segment(3));
        $query = $this->db->get('gallery');
        
        if($query->num_rows > 0 )
        {
            
            return $query;
            
        }
        else
        {
            
            return FALSE;
            
        }



errror Result() - El Forum - 10-22-2007

[eluser]Michael Wales[/eluser]
Please show us your Controller - this appears to be a View and a Model.

This code will only work if your Controller reads something like:

Code:
$this->data->query = $this->myModel->GetGallerySpot();
$this->load->view('spot_side', $this->data);

Note: I am naming the variable being passed to the View query.


errror Result() - El Forum - 10-22-2007

[eluser]Michael Wales[/eluser]
Here's how I would write this (not using a Model, but you could):

Controller
Code:
function GetGallerySpot() {
  $query = $this->db->getwhere('gallery', array('id_spot'=>$this->uri->segment(3)));
  if ($query->num_rows() > 0) {
    $this->data->images = $query->result();
  } else {
    $this->data->images = FALSE;
  }
  $this->load->view('spot_side', $this->data);
}

View:
Code:
<div id="wrap_contx">
    <div id="sidebar">
        <div id="t_in">Photos of This Spot</div>
        <div id="div_details">
    Add Photos
    </div
&lt;?php foreach($images as $row):?&gt;

<img src="&lt;?=$row->thumb;?&gt;">


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



errror Result() - El Forum - 10-22-2007

[eluser]MMCCQQ[/eluser]
Model

Code:
[quote]function GetGallerySpot()
    {
        
        
        $this->db->where('id_spot',$this->uri->segment(3));
        $query = $this->db->get('gallery');
        
        if($query->num_rows > 0 )
        {
            
            return $query;
            
        }
        else
        {
            
            return FALSE;
            
        }
        
        
        
        
        
    }[/quote]

Controller
Code:
$side['query'] = $this->TS->ViewInfoProfile();
        $data['side'] = $this->load->view('side',$side, true);
        $data['main'] = $this->load->view('spot/view_panel', $main, true);
    
        $this->load->view('main_template', $data);

Code:
<div id="wrap_contx">
    <div id="sidebar">
        <div id="t_in">Photos of This Spot</div>
        <div id="div_details">
    Add Photos
    </div>
&lt;?php foreach($query->result() as $row):?&gt;

<img src="&lt;?=$row->thumb;?&gt;">


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



errror Result() - El Forum - 10-22-2007

[eluser]MMCCQQ[/eluser]
A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: views/side_spot.php

Line Number: 8

8 line
Code:
&lt;?php foreach($images as $row):?&gt;



errror Result() - El Forum - 10-22-2007

[eluser]Rick Jolly[/eluser]
You've shown errors for 2 different views: side_spot and spot_side. However, the controller code you've shown doesn't load either of those views (it loads spot.php). You also have a GetGallerySpot() method, but your controller code doesn't call it. Please post the relevent code and maybe someone can help.


errror Result() - El Forum - 10-22-2007

[eluser]MMCCQQ[/eluser]
function View()
{

if ($base = $this->TS->View())
{
$data['title'] = "View you Spot";
$main['query'] = $base;
$side['images'] = $this->TS->GetGallerySpot();
$data['side'] = $this->load->view('side_spot',$side, true);
$data['main'] = $this->load->view('spot/spot_view', $main, true);

$this->load->view('main_template', $data);


}
i call GetGallerySpot() in $side['images']


errror Result() - El Forum - 10-22-2007

[eluser]Rick Jolly[/eluser]
Ok, 2 things:

1) Most importantly, you return the query object from the model instead of the query->result();. It's probably best to return the query result. Otherwise you'll need to edit your view to something like "foreach($images->row() as $row)".

2) If GetGallerySpot() has no records it will return false. You should check for that in the controller or view before the foreach loop.


errror Result() - El Forum - 10-22-2007

[eluser]MMCCQQ[/eluser]
ready!