【发布时间】:2015-04-07 11:57:24
【问题描述】:
我的模块的视图页面中有一个 foreach 循环,它从控制器检索图像网格(数量由“items_top”确定)。问题是所有项目同时加载,因为此时没有设置分页。
这是我的视图的代码:
<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>
这是我的函数助手:
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) ;
}
这是我的控制器:
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);
}
我需要对循环进行切片以对这些项目进行分页,我希望能够在底部添加一个“加载更多”按钮,但问题是 Ajax,我不能使用 $page = (int)getFromExternalInput();,因为循环调用公共功能不是网址。
分割这个循环的正确方法是什么?
【问题讨论】:
-
控制器是否从数据库中返回图像?如果是这样,您可以使用原始限制和偏移方法,而不是对图像进行分页。
-
@junkystu 确实这是我的主要问题,我从外部 url 获取数据
-
你试过用你的图片构建一个数组吗?然后,您可以使用PHP's slice 取回图像块,类似于您限制和偏移数据库的方式。
-
@junkystu 我是 codeigniter 的新手,所以很难继续这样做。
-
关于“空白页”,请阅读:stackoverflow.com/questions/9587413/…
标签: javascript php ajax function codeigniter