[英]How can I equally divide values in two parts while keeping the total equal for pagination for all possible pages?
I am trying to fetch results based on two types of results. 我正在尝试根据两种结果来获取结果。 First top of search (TOS) and then non top of search (NTOS) so before this by using a single query pagination was easy but since now I am fetching records from two different queries and then showing them together makes it hard. 首先是搜索的顶部(TOS),然后是非搜索的顶部(NTOS),因此在此之前,通过使用单个查询分页是很容易的,但是从现在开始,我要从两个不同的查询中获取记录,然后将它们一起显示会很困难。 What I want to do is give priority to TOS results over NTOS results and keep total results per page to 39 results. 我要做的是将TOS结果优先于NTOS结果,并将每页的总结果保持为39个结果。 So I would like to make an array dividing the results equally into all possible pages. 因此,我想创建一个数组,将结果平均划分为所有可能的页面。
I tried to do this but when things become a bit hard I can't get my head around this. 我尝试这样做,但是当事情变得有点困难时,我无法解决这个问题。 So I am looking for answers. 所以我在寻找答案。
$tos = 23;
$ntos = 26;
$loop = true;
$page = 1;
$numPerPage = 39;
$pageData = array();
while ($loop) {
if ($page == 1) {
$caltos = (($numPerPage - $tos) > $numPerPage ? ($numPerPage - $tos) : $tos);
$calntos = (($numPerPage - $caltos) > $ntos ? $ntos : ($numPerPage - $caltos));
} else {
foreach ($pageData as $eachPage) {
$caltos = (($numPerPage - $eachPage['tos']) > ($page * $numPerPage) ? ($numPerPage - $tos) : $tos);
$calntos = (($numPerPage - $caltos) > $ntos ? $ntos : ($numPerPage - $caltos));
}
$loop = false;
}
$pageData[$page] = array('tos' => $caltos, 'ntos' => $calntos );
$page++;
}
print_r($pageData);
I want to expect the following result by giving it different inputs as: 我希望通过给它不同的输入作为以下结果:
1) TOS: 23 & NTOS: 26
Expected Result:
[
[1] => ['tos' => 23,'tosOffset' => 0, 'ntos' => 16, 'ntosOffset' => 0],
[2] => ['ntos' => 10, 'ntosOffset' => 16]
]
2) TOS: 49 & NTOS: 56
Expected Result:
[
[1] => ['tos' => 39, 'tosOffset' => 0],
[2] => ['tos' => 10, 'tosOffset' => 39, 'ntos' => 29, 'ntosOffset' => 0],
[3] => ['ntos' => 27, 'ntosOffset' => 29]
]
3) TOS: 0 & NTOS: 73
Expected Result:
[
[1] => ['ntos' => 39, 'ntosOffset' => 0],
[2] => ['ntos' => 34, 'ntosOffset' => 39]
]
It's a work for an usual loop 这是通常循环的工作
while ($tos or $ntos) {
$sum = min($tos, $numPerPage);
if($sum) {
$pageData[$page]['tos'] = $sum;
}
$tos -= $sum;
$rest = min($ntos, $numPerPage- $sum);
if($rest) {
$pageData[$page]['ntos'] = $rest;
}
$ntos -= $rest;
$page++;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.