首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Php中中断For循环并仅在用户请求时才继续?

如何在Php中中断For循环并仅在用户请求时才继续?
EN

Stack Overflow用户
提问于 2015-05-02 12:01:24
回答 3查看 179关注 0票数 0

考虑一个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

我做了一个循环

代码语言:javascript
复制
$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
   }

}

如何执行这种类型的中断循环和继续?

EN

Stack Overflow用户

发布于 2015-05-02 17:05:06

代码语言:javascript
复制
<?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;
    }

}

?>
票数 0
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29998240

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档