[eluser]timpiele[/eluser]
I am using the asset_helper to load images into my controller, and pass them through to the view with the $data array.
I am trying to keep as much PHP as possible out of the view because it scares my designer and makes her cry.
Specifically, I want to move most of this out of my view:
Code:
<?php foreach ($collections as $collection): ?>
<div>
<?php echo image_asset($collection['bg_img'], '', array('alt'=>'Some Background')); ?>
</div>
<div>
<?php echo image_asset($collection['title_img'], '', array('alt'=>'Some Title')); ?>
</div>
<?php endforeach; ?>
and turn it into this:
Code:
<?php foreach ($collections as $collection): ?>
<?php echo $collections['bg_img']; ?>
<?php echo $collections['title_img']; ?>
<?php endforeach; ?>
My database table has two columns which hold the filenames of images that apply to each row, a background image and a title. I need to take these files names out of the query and load them with the asset helper but it isn't working.
Here's the important part of the controller:
Code:
public function index() {
// load the asset helper access css. scripts and images
$this->load->helper('asset');
// call getCurrentCollections() method to load collection panels
$collections = self::getCurrentCollections();
}
private function getCurrentCollections() {
$this->load->model('Get_Collections');
$collections = $this->Get_Collections->getCurrentCollections();
return $collections;
}
and here's the getCurrentCollections method from the Model:
Code:
public function getCurrentCollections() {
// create query string to get collections where status is 1 (current)
$query = $this->db->get_where('collections', array('status' => '1'));
// order the results by the sequence field which is set by admin panel
$this->db->order_by("sequence", "asc");
// set the query results to the $results variable
$results = $query->result_array();
return $results;
}
So how do I loop through $collections and use the asset_helper to create the image tag etc?
I tried this but it didn't work, my logic is wrong:
Code:
foreach ($collections as $row) {
$data['collections']['background'] = image_asset($row['bg_img'], '', array('alt'=>'Some Background'));;
$data['collections']['title'] = image_asset($row['title_img'], '', array('alt'=>'Some Title'));
}
I know it's probably a noob error.