Welcome Guest, Not a member yet? Register   Sign In
Issues with calling model from view
#1

[eluser]Ghetolobster[/eluser]
Morning all,

I have a small (could be major) issue with once of my pages. Basically I want to load a list of views based upon a search made by the user. And for each for the rooms in the list I want to pull an image from a corresponding table. Now the issue arises when I try to call for the room's image. The first image will display but the rest wont, I get the error:

Code:
A PHP Error was encountered Severity: Notice

Message: Undefined offset: 0

Filename: views/search_view.php

Line Number: 27

A PHP Error was encountered Severity: Notice

Message: Trying to get property of non-object

Filename: views/search_view.php

Line Number: 27

This is what I have done so far

My Model:

Code:
function get_room_image($rid)
{
    $q = $this->db
        ->where('room_id',$rid)
        ->limit(1)
        ->get('room_image');

    return $q->result();

}

function search_rooms()
{
    $q = $this->db
        ->where('features', $this->input->post('features'))
        ->get('rooms');

    $room['results'] = $q->result();

    return $room['results'];
}

My Controller:

Code:
function search()
{
    $data['main_content'] = 'search_view';
    $data['page_title'] = 'Search';

    $data['results'] = $this->site_model->search_rooms();

    $this->load->view('includes/template',$data);
}

My View:

Code:
...    
<?php foreach ($results['results'] as $room) {

    $room_images = $this->site_model->get_room_image($room->id); ?>
        <div class="col2 bot">

            <div class="col_head"></div>
                <div class="col_info">
                <table border="0" cellpadding="5" cellspacing="5">
                <tr><td>
                <img src="&lt;?php echo base_url() . 'room/' . $room_images[0]-&gt;image_name; ?&gt;" border="0" class="imgbox" />
                </td>
...

Line 27 is actually the line with the img tag on it

When I do a var_dump I get the following:

Code:
array(1) {
  [0]=>
  object(stdClass)#27 (3) {
    ["id"]=>
    string(1) "1"
    ["room_id"]=>
    string(1) "1"
    ["image_name"]=>
    string(36) "1614d828817ac5b396660621048b167a.jpg"
  }
}

array(0) {}
array(0) {}
array(0) {}
...

SO it seems that it isn't performing the get_room_image function on each iteration correctly

How would I go about resolving this matter?
Any advice is most welcome.

Regards,
#2

[eluser]Ghetolobster[/eluser]
Issue was resolved as I forgot to add a ";" onto a line within my db query string




Theme © iAndrew 2016 - Forum software by © MyBB