PHP更优雅地将组拆分成页面?

时间:2017-06-16 00:30:01

标签: php

我有一组项目(例如,有计数[3,2,0,4,1,0,1,1,1,0,5,2,3]),我希望有一个最小数量的计数每页(最后一页除外)。 array_slice当前使用计数数组作为输入,但最终它将使用相应的数据作为输入。对于当前查看的页面,最终版本仅使用array_slice一次。我想知道代码是否可以更优雅,例如使用特殊的PHP函数等。

输出:

每页项目数:1个数页:10个

3; 2; 0,4; 1; 0,1; 1; 1; 0,5; 2; 3;

每页项目数:2个数页:8个

3; 2; 0,4; 1,0,1; 1,1; 0,5; 2; 3;

每页项目数:3个数页:5个

3; 2,0,4; 1,0,1,1; 1,0,5; 2,3;

每页项目数:4个数页:5个

3,2; 0,4; 1,0,1,1,1; 0,5; 2,3;

每页项目数:5个数页:4个

3,2; 0,4,1; 0,1,1,1,0,5; 2,3;

每页项目数:6个数页:3个

3,2,0,4; 1,0,1,1,1,0,5; 2,3;

<?php
    $group_counts = [3,2,0,4,1,0,1,1,1,0,5,2,3];
    splitOntoPages($group_counts, 1);
    splitOntoPages($group_counts, 2);
    splitOntoPages($group_counts, 3);
    splitOntoPages($group_counts, 4);
    splitOntoPages($group_counts, 5);
    splitOntoPages($group_counts, 6);

function splitOntoPages($item_group_counts, $items_per_page) {
    $page_cumul_totals = [0 => 0];
    $page_first_index = [0 => 0];
    $page = 0;

    foreach ($item_group_counts as $group_index => $group_total) {
        $page_cumul_totals[$page] += $group_total;
        if ($page_cumul_totals[$page] >= $items_per_page) {
            $page++;
            $page_first_index[$page] = $group_index + 1;
            $page_cumul_totals[$page] = 0;
        }
    }
    $page_first_index[$page + 1] = $group_index + 1;

    $numPages = count($page_cumul_totals);
    if ($page_cumul_totals[$numPages - 1] == 0) {
        $numPages--;
    }
    echo 'Items per page: '.$items_per_page.' num pages: '.$numPages.'<br>';
    for ($p = 0; $p < $numPages; $p++) {
        $fromIndex = $page_first_index[$p];
        $countOnPage = $page_first_index[$p + 1] - $fromIndex;
        $page_items = array_slice($item_group_counts, $fromIndex, $countOnPage);
        echo implode(',', $page_items).'; ';
    }
    echo '<br><br>';
}

我的目标是根据给定的页码确定array_slice的正确参数...并确定页数。

$ page_items = array_slice($ item_groups,$ fromIndex,$ countOnPage);

1 个答案:

答案 0 :(得分:0)

我最终想到了我正在寻找的东西:它更简洁,更清晰,但效率可能稍低......

function splitOntoPages($item_group_counts, $min_count_per_page) {
    $total_groups = count($item_group_counts);
    $page = 0;    
    $i = 0;
    $page_details = [];
    while ($i < $total_groups) {
        for ($n = 1; $n < ($total_groups - $i); $n++) {
            $page_total = array_sum(array_slice($item_group_counts, $i, $n));
            if ($page_total >= $min_count_per_page) {
                $page_details[$page] = ['offset' => $i, 'length' => $n];
                $page++;
                $i += $n;
                continue 2;
            }
        }
        $page_details[$page] = ['offset' => $i, 'length' => $n];
        break;
    }    
    echo "Items per page: $min_count_per_page num pages: ".($page+1)."<br>";
    foreach ($page_details as $page => $details) {
       echo implode(',', array_slice($item_group_counts, $details['offset'], $details['length'])).'; ';
    }    
    echo '<br><br>';
}

获取第x页上的项目:

array_slice($item_group_info, $page_details[$page_index]['offset'], $page_details[$page_index]['length']));