php pagination of an array with previous and next tab -
i have array contain around 1000 records. want display 20 array records per page.
$list=array( array([title]=>"sony", [description]=>"camera"), array([title]=>"sony", [description]=>"mobiles"), array([title]=>"lenovo", [description]=>"laptop"), array([title]=>"lenovo", [description]=>"mobiles") );
i have used following code pagination. giving me long row pagination.
can me include previous , next code existing code pagination good.
$page = isset($_request['page']) && $_request['page'] > 0 ? $_request['page'] : 1; function display($list, $page = 1) { $start = ($page - 1) * 2; $list = array_slice($list, $start, 15); foreach ($list $key => $val) { echo $val['title'] . '<br/>'; echo $val['description'] . '<br/>'; echo "<br>"; }} $len = count($list); $pages = ceil($len / 2); if ($page > $pages or $page < 1) { echo 'page not found'; } else { display($list, $page); ($i = 1 ; $i <= $pages ; $i++) { $current = ($i == $page) ? true : false; if ($current) { echo '<b>' . $i . '</b>'; } else { ?> <a href="http://localhost/flipkart-api/fkt_offer.php?offer=alloffers&page=<?php echo $i;?>"><?php echo $i;?></a> <?php } } }
here's example data array question.
the example
- the page size assumed 2 (20 in question).
- the size of data array not matter.
- the start parameter provided (as in example) thru parameter http://localhost/flipkart-api/fkt_offer.php?…start=index_or_page.
this parameter available in script $_get['start']. - the previous , next start indices calculated ($start +/- $maxpage, etc.).
- to keep example simple, took start index, not page number, parameter. use page number , calculate index, of course.
- for reason of brevity omitted error checking ("what if no more items", etc.).
code:
<?php // data array $list=array( array('title'=>"sony", 'description'=>"camera"), array('title'=>"sony", 'description'=>"mobiles"), array('title'=>"lenovo", 'description'=>"laptop"), array('title'=>"lenovo", 'description'=>"mobiles") ); // evaluate url $proto = ((isset($_server["https"])) && (strtoupper($_server["https"]) == 'on')) ? "https://" : "http://"; $hname = getenv("server_name"); $port = getenv("server_port"); if ( (($port==80)&&($proto=='http://')) || (($port==443)&&($proto=='https://')) ) { $port = ''; } $params = ''; foreach ($_get $key=>$value) { if (strtolower($key)=='start') continue; $params .= (empty($params)) ? "$key=$value" : "&$key=$value"; } $url = $proto . $hname . $port. $_server['script_name'] . '?' . $params; // page contents $last = count($list)-1; $start = (isset($_get['start'])) ? intval($_get['start']) : 0; if ($start<0) $start = 0; if ($start > $last) $start = $last; $maxpage = 2; echo "<p>start index = $start</p>" . php_eol; $curpage = 0; for($xi=$start; $xi<=$last; $xi++) { if ($curpage >= $maxpage) break; $curpage++; echo 'entry ' . $curpage . ': ' . $list[$xi]['title'] . ' - ' . $list[$xi]['description'] . '<br />' . php_eol; } // navigation $prev = $start - $maxpage; if ($prev<0) $prev = 0; $next = ( ($start+$maxpage) > $last) ? $start : $start + $maxpage; $prev = ( ($start-$maxpage) < 0) ? 0 : $start - $maxpage; echo '<p><a href="'.$url.'&start='.$prev.'">previous</a> '; echo '<a href="'.$url.'&start='.$next.'">next</a></p>'; ?>
result (e.g)
start index = 2 entry 1: lenovo - laptop entry 2: lenovo - mobiles previous next
Comments
Post a Comment