Welcome Guest, Not a member yet? Register   Sign In
Help with query results and asset_helper
#1

[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>
          &lt;?php echo image_asset($collection['bg_img'], '', array('alt'=>'Some Background')); ?&gt;
     </div>
     <div>
          &lt;?php echo image_asset($collection['title_img'], '', array('alt'=>'Some Title')); ?&gt;
     </div>
&lt;?php endforeach; ?&gt;

and turn it into this:

Code:
&lt;?php foreach ($collections as $collection): ?&gt;
     &lt;?php echo $collections['bg_img']; ?&gt;
     &lt;?php echo $collections['title_img']; ?&gt;
&lt;?php endforeach; ?&gt;


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.
#2

[eluser]Aken[/eluser]
In this part of your logic:

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'));
}

You're just setting the same array values over and over again. You need to make sure you're creating a new array element in the $data['collections'] array.
#3

[eluser]timpiele[/eluser]
So use a for loop and then set $data['collections'][$i]['background'] etc?

And then iterate through the foreach loop on the view?
#4

[eluser]CroNiX[/eluser]
I'd just leave your view the way it is. Creating an additional loop to iterate over the exact same data is wasteful and inefficient. Instead of looping through the results in the controller, adding additional data, sending to your view and THEN looping over the same data (again)....just loop over it once in the view like you were....




Theme © iAndrew 2016 - Forum software by © MyBB