dongmeng4742 2014-10-19 02:19
浏览 31

Paginate阵列的组合不会提前产生整个组合

I have the following loop that combines two arrays and displays the results in an ordered list:

$list1 = array("make","break","buy");
$list2 = array("home","car","bike");

echo "<ol>";
for($a=0; $a<3; $a++){
    for($b=0; $b<3; $b++){
        echo "<li".($list1[$a].$list2[$b])."</li>";
    }
}
echo "</ol>";

The actual arrays that I have include about 1500 words each, so the list is more than 2 Million combinations long.

Is there a way to display the results with pagination, without generating the entire result set in advance? For example 500 items per page?

FYI, I don't necessarily need to display the results in an ordered list, if that's going to mess up the pagination.

  • 写回答

1条回答 默认 最新

  • douyi1899 2014-10-19 16:29
    关注

    First, you'll need to be able to start looping from any position in the arrays.
    Of course you can use for loops, but I think while loops fit better here.

    $length1 = count($list1);
    $length2 = count($list2);
    
    //now indexes are initialized to variable values
    $a = $var1; //start position $var1 is an integer variable between 0 and ($length1 - 1)
    $b = $var2; //start position $var2 is an integer variable between 0 and ($length2 - 1)
    
    while ($a < $length1) {
        while ($b < $length2) {
            echo '<li>', $list1[$a], ' ', $list2[$b], '</li>';
            $b++;
        }
        $b = 0; //reset inner loop each time it ends
        $a++;
    }
    

    Next, we need a way to stop both loops if the maximum number of results per page ($limit) is reached before the end of the combinations.

    $length1 = count($list1);
    $length2 = count($list2);
    $a = $var1;
    $b = $var2;
    
    $counter = 0;
    
    while ($a < $length1) {
        while ($b < $length2) {
            echo '<li>', $list1[$a], ' ', $list2[$b], '</li>';
    
            $counter++;
            if($counter === $limit) break 2;
    
            $b++;
        }
        $b = 0;
        $a++;
    }
    

    Finally, we must find the correct values for $var1 and $var2 above, based on the current $page (starting from page 1) and $limit. This is plain arithmetics that I won't explain here.
    Putting it all together:

    $length1 = count($list1);
    $length2 = count($list2);
    
    $offset = $limit * ($page - 1);
    $a = (int)($offset / $length2);
    $b = $offset % $length2;
    
    $counter = 0;
    while ($a < $length1) {
        while ($b < $length2) {
            echo '<li>', $list1[$a], ' ', $list2[$b], '</li>';
            $counter++;
            if($counter === $limit) break 2;
            $b++;
        }
        $b = 0;
        $a++;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”