Welcome Guest, Not a member yet? Register   Sign In
DB Query issues
#1

[eluser]stevefink[/eluser]
Can anyone tell me why $photos in my view is not being seen by my view? I keep getting a notice that it is an undefined variable. I've made sure $photos is being populated properly in the model/controller:

Here's my controller:

Code:
function get_photos()
    {
        // get all photo filesystem locations.
        $photos = $this->autodb->get_photos();
        $this->load->view('console/vehicle_photos_view', $photos);
    }

My model:

Code:
function get_photos()
    {
        // return all available photos for vehicle
        $sql = "select photoloc from photos";
        $all_photos = $this->db->query($sql);
        
        $photos = array();
        
        if ($all_photos->num_rows() > 0)
        {
            foreach ($all_photos->result_array() as $photo)
            {
                $photos[] = $photo;
            }
        }
        
        return $photos;
    }

$photos is still undefined after $this->load->view('console/vehicle_photos_view', $photos);

Any idea?
#2

[eluser]Seppo[/eluser]
Try this on your controller

Code:
function get_photos()
    {
        // get all photo filesystem locations.
        $data['photos'] = $this->autodb->get_photos();
        $this->load->view('console/vehicle_photos_view', $data);
    }
#3

[eluser]stevefink[/eluser]
No results, unfortunately. Is there a reason $data needs to be pushed in versus $photos? Is the view code searching for $data? I've done it before with arbitrary array names.
#4

[eluser]stevefink[/eluser]
I added a log_message directive into my controller to grab the data, and the array is indeed being populated. The view however, does not wish to do anything with this data.

function get_photos()
{
// get all photo filesystem locations.
$photos = $this->autodb->get_photos();
log_message('debug', "This is a test: " . print_r($photos, TRUE));
//$this->load->view('console/vehicle_photos_view', $data);
}

and my logs:

DEBUG - 2007-08-21 18:25:49 --> This is a test: Array
(
[0] => Array
(
[photoloc] => ./uploads/IMG0595.JPG
)

[1] => Array
(
[photoloc] => ./uploads/IMG0596.JPG
)

[2] => Array
(
[photoloc] => ./uploads/IMG0598.JPG
)

)

Any ideas folks?
#5

[eluser]Rick Jolly[/eluser]
How are you referencing the array in your view? Typically the data you send to your view is a key-value array. You are sending a numerically indexed array to the view. Personally, I'd just send the result array to the view. The extra loop in the model isn't necessary.
#6

[eluser]BravoAlpha[/eluser]
[quote author="stevefink" date="1187752934"]Is there a reason $data needs to be pushed in versus $photos?[/quote]
I believe that Seppo is just pointing out that you're not passing an associative array. ($data is just commonly used.)

http://ellislab.com/codeigniter/user-gui...oader.html
Quote:$this->load->view('file_name', $data, true/false)
...
The second optional parameter can take an associative array or an object as input, which it runs through the PHP extract function to convert to variables that can be used in your view files. Again, read the Views page to learn how this might be useful.
#7

[eluser]Michael Wales[/eluser]
How are you trying to echo them out in your view? Looks like the controller and model are fine (although I would put them in an associative array anyways, what if you need to pass more data than just these photos to the view?).

Paste your view code.
#8

[eluser]stevefink[/eluser]
I got this resolved, btw. The issue was, in my model I was converting the data to an array and then passing that into the view which was causing issues (I think.)

Final result looks like this:

Code:
<?php foreach($photos->result() as $photo): ?>
    <br><br> <hr>
    
    <img src="&lt;?= base_url() ?&gt;&lt;?= $photo->photoloc ?&gt;" />
    
&lt;? endforeach; ?&gt;




Theme © iAndrew 2016 - Forum software by © MyBB