I have a foreach loop in a view page of my module which retrieves a grid of images (the number is determined by the "items_top") from a controller. The problem is that all items are loaded at the same time because there is no pagination set at the moment.
This is the code of my view:
<div class="row row-sm padder-lg ">
<?php
foreach ($top->tracks->track as $key => $value)
{
if($key >= $this->config->item("items_top"))
return false;
$image = $value->image[3]->text;
if($image == '')
$image = $value->image[2]->text;
if($image == '')
$image = base_url()."assets/images/no-cover.png";
?>
<div class="col-xs-6 col-sm-4 col-md-3 col-lg-2">
<div class="item">
<div class="pos-rlt">
<a href="#">
<div class="item-overlay opacity r r-2x bg-black">
<div class="center text-center m-t-n">
<i class="icon-control-play i-2x"></i>
</div>
</div>
<a href="#">
<div class="r r-2x img-full" style="background:url('<?php echo $image; ?>') no-repeat top center; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover;">
<div style="height:180px;overflow:hidden;"></div>
</div>
</a>
</div>
<div class="padder-v">
<a href="#" class="text-ellipsis"><?php echo $value->name; ?></a>
<a href="#" class="text-ellipsis text-xs text-muted"><?php echo $value->artist->name; ?></a>
</div>
</div>
</div>
<?php
}
?>
<script>
$(".nav-sidebar li").removeClass("active");
$("#topTrack").addClass('active');
</script>
</div>
this is my function helper:
function _curl($url) {
$CI =& get_instance();
$ch = curl_init();
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,15);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
if(strtolower(parse_url($url, PHP_URL_SCHEME)) == 'https')
{
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,1);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,1);
}
if($CI->config->item("proxy") != '')
{
curl_setopt($ch, CURLOPT_PROXY, $CI->config->item("proxy"));
}
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
function getTopTracks($artist = false)
{
$CI =& get_instance();
$artist = econde($artist);
$url = "http://ws.audioscrobbler.com/2.0/?method=chart.gettoptracks&api_key=".$CI->config->item("lastfm")."&format=json&limit=60&page=3";
$file_cache = 'cache/tracks_'.sha1("toptracks").".cache";
if($cache)
{
$json = $cache;
if (time()-filemtime($file_cache) > 24 * 3600) {
// file older than 24 hours
@unlink($file_cache);
}
}
else
{
$json = _curl($url);
if($CI->config->item("use_cache") == "1")
write_file($file_cache, $json);
}
if(!$artist)
$json = str_ireplace("toptracks","tracks",$json);
else
$json = remove_banned($json,true) ;
$json = str_ireplace("#text","text",$json);
$json = str_ireplace(":totalResults","",$json);
return remove_banned($json) ;
}
and this is my controller:
public function getTopTracks($return = false,$page = false)
{
$data['page'] = $page;
$data['top'] = json_decode(getTopTracks());
if(count($data['top']->tracks->track) <=1)
{
$this->config->set_item("auto_country", '0');
$data['top'] = json_decode(getTopTracks());
}
return $this->load->view(getTemplate('topTracks'),$data,$return);
}
I need to slice the loop to make a pagination of these items, I wanna be able to add a button "load more" at the bottom, but the problem is Ajax, I cannot use $page = (int)getFromExternalInput();
because the loop call a public function not a url.
What's the correct way to slice this loop?