Trying to pass images from a model/herlper file to a slideshow in
codeigniter using a an array and foreach
I have a controller pages.php:
class Pages extends CI_Controller {
public function index($page = 'home')
{
if ( ! file_exists('application/views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$this->lang->load('common/menu.php');
$this->lang->load('pages/'.$page.'.php');
$data = array();
$this->load->helper('slideshow');
$data['slideshow'] = get_image_array();
$this->load->view('templates/common/header', $data);
$this->load->view('modules/slideshow', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/common/footer', $data);
}
}
My slideshow_helper file:
function get_image_array(){
$images = array(img("slideshow/desktop/home1.jpg",
"one"),img("slideshow/desktop/home2.jpg",
"two"),img("slideshow/desktop/home3.jpg", "three"));
$captions = array("first image", "second image", "third image");
return array_combine($images, $captions);
}
And the slideshow view:
<div class="container">
<div class="row">
<div class="col-xs-12">
<div id="carousel-desktop" class="carousel slide visible-md
visible-lg">
<ol class="carousel-indicators">
<li data-target="#carousel-desktop" data-slide-to="0"
class="active"></li>
<li data-target="#carousel-desktop"
data-slide-to="1"></li>
<li data-target="#carousel-desktop"
data-slide-to="2"></li>
</ol>
<div class="carousel-inner">
<?php $i = 1; ?>
<?php foreach ($image as $imageSrc => $caption ):?>
<?php $item_class = ($i == 1) ? 'item active' :
'item'; ?>
<div class="<?php echo $item_class; ?>">
<?php echo $image; if($caption != ""){ ?>
<div class="carousel-caption">
<?php echo $caption;?>
</div>
<?php } ?>
</div>
<?php $i++; ?>
<?php endforeach; ?>
</div>
<a class="left carousel-control" href="#carousel-desktop"
data-slide="prev">
<span class="icon-chevron-sign-left"></span>
</a>
<a class="right carousel-control"
href="#carousel-desktop" data-slide="next">
<span class="icon-chevron-sign-right"></span>
</a>
</div>
<br />
</div>
</div>
</div>
<br />
Unfortunately this does not work am I missing something? I get the
following error:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: image
Filename: pages/home.php
-AND-
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: pages/home.php
Or is it completly wrong I should point out that this site will not have
access to a database.
When I have the array in the view file it works.
What I am trying to do is have a different model/helper file for each page
as each page of this controller will have a slideshow but each with
different images. So rather than recreating the view part countless times
I want to simply have a new array for each page. I was thinking of using
something like:
$this->load->helper('pages/'.$page);
-or-
$this->load->model('pages/'.$page.'.php');
so each page has its own model/helper filer but obvioulsy as I cannot get
the first bit working I have no idea if this second bit will work.
Any help will be very much appriciated
No comments:
Post a Comment