php - How do I build multiple CodeIgniter image galleries that have the same setup and different content? -
i'm new codeigniter, mvc, , oop. i'm trying make 2 image galleries rely on different content, same setup , functionality. what's best way set up? should create library containing functionality , call in controllers?
in mvc, want make sure data gathering (model), logic (controller), , display (view) logically separated. in order this, you'll need have common method of putting image gallery data create in model , pass view through controller. let's consider hyper-simplistic model function this:
function getimages($param) { if ($param) { return array( array('id'=>1, 'caption'=>'image 1', 'url'=>'/images/image1.jpg'), array('id'=>2, 'caption'=>'image 2', 'url'=>'/images/image2.jpg') ) } else { return array( array('id'=>3, 'caption'=>'image 3', 'url'=>'/images/image3.jpg'), array('id'=>4, 'caption'=>'image 4', 'url'=>'/images/image4.jpg') ) } }
i'm using $param
here either true or false fulfill requirement "different content" formatted in same way. have function in images model, can call controller this:
$images = $this->images->getimages($param);
now have data in consistent format, know can pass array of images images view expects format (in case, array or arrays have basic image data in them).
now you'll want create image_gallery view looks this:
<div id="image_gallery"> <?php foreach($images $image) { ?> <img src="<?php echo $image['url'] ?>" /> <div class="image_caption"><?php echo $image['caption'] ?></div> <?php } ?> </div>
of course actual gallery going different, idea here that, if image information consistent, shouldn't have troubles creating image gallery. last piece of puzzle sending images data view, , done controller:
$data = array(); $data['images'] = $images; //this var above when called $this->images->getimages($param) $this->load->view('image_gallery', $data);
now have slot in $data
array called "images", able access $images
in our image_gallery view.
of course, don't need call views controllers. indeed, makes sense call views inside views, depends entirely on requirements. makes sense call views , return them strings library, fair warning: doing can lead away simplicity of mvc approach.
Comments
Post a Comment