考虑一个API结果,每页提供10个产品。
例如:http://api.example.com/key=XXX&type=1&cat=abc给出了10个结果0到9
例如:http://api.example.com/key=XXX&type=1&cat=abc&page=2给出了另外10个结果0到9
我做了一个循环
$cnt = count($results) // 10
$page = 1;
$text = $_REQUEST['more'];
// I Want to break the loop to show 3 products only to user When the user clicks on the load more , then i have to show another 3 products..like this continues.
for($i=0; $i<$cnt; $i++)
{
if($i == 3) break;
/*
* My Stuffs.
*/
if($text == 'more')
{
// i have to show 3 to 6 , Then 6 to 9 , Then page 2 , 0 to 2
}
}如何执行这种类型的中断循环和继续?
发布于 2015-05-02 17:05:06
<?php
$results = array('abc', 'def', 'ghi', 'jkl', 'nmo', 'pqr', 'stu', 'vwx', 'yz1', '234', '567', '890', 'zaq' );
$cnt = count($results); // 12
$page = 1;
$text = $_REQUEST['more']; //'more';
// I Want to break the loop to show 3 products only to user When the user clicks on the load more ,
//then i have to show another 3 products..like this continues.
$i = 0;
//page 1
for($i; $i < 4; $i++){
//whatever you want to do
}
if($text == 'more'){
while($cnt > $i){
if($page == 1){
for($j=4; $j<10 && $j < $cnt; $j++){
for($k = $j; $k < $j+3; $k++){
//next three records
}
$j = $k-1;
}
$page++;
}else{
//new page
$val = $page*10;
for($j=($page-1)*10; $j< $val && $j < $cnt; $j++){
for($k = $j; $k < $j+3; $k++){
//next three records
}
$j = $k-1;
}
$page++;
}
$i = $j;
}
}
?>https://stackoverflow.com/questions/29998240
复制相似问题