[php]マルチデータソースシリアルページングアルゴリズム

1187 ワード

<?php
/**
 *           
 * @param arr $arrSourceCount      =>    
 * @param int $offset     
 * @param int $length   
 * @return arr      =>array('offset'=>    , 'length'=>  )
 * @author flynetcn
 */
function multiSerialPaging($arrSourceCount, $offset=0, $length=10)
{
	$start = 0;
	$end = $offset + $length;
	$scopeList = array();
	foreach ($arrSourceCount as $k => $count) {
		$start += $count;
		if ($offset<$start && $offset>=$start-$count) {
			$scopeList[$k] = array(
				'offset'=>$offset-($start-$count),
			);
			if ($end > $start) {
				$scopeList[$k]['length'] = $count - $scopeList[$k]['offset'];
			}
		}
		if ($end<=$start && $end>$start-$count) {
			if ($offset >= $start-$count) {
				$scopeList[$k]['length'] = $length;
			} else {
				$scopeList[$k]['offset'] = 0;
				$scopeList[$k]['length'] = $length-($start-$count-$offset);
			}
		}
		if ($start<$end && $start-$count>$offset) {
			$scopeList[$k]['offset'] = 0;
			$scopeList[$k]['length'] = $count;
		}
	}
	return $scopeList;
}