我希望每页显示2个数组记录。请帮助进行简单的分页。当用户点击下一页时,下一页将显示2个数组记录。
[dotdList] => Array
(
[0] => Array
(
[title] => Just at Rs. 799
[description] => Ambrane
)
[1] => Array
(
[title] => Flat Rs.249
[description] => Sarees & more
)
[2] => Array
(
[title] => Extra 10% off
[description] => Routers
)
[3] => Array
(
[title] => Just Rs.549
[description] => From Nova
)
)
这是我试过的,数组包含大约1500个Records.Pagination看起来不太好。它给出了非常大的结果,如果我想隐藏其中的一些数字只是为了减少它的宽度。或者可能是下一个选项或中间点。我想每页显示最少15-20条记录。
<?phpnamespace clusterdev;class Flipkart{private $affiliateId;private $token;private $response_type;private $api_base = 'https://affiliate-api.flipkart.net/affiliate/api/';private $verify_ssl = false;function __construct($affiliateId, $token, $response_type="json")
{
$this->affiliateId = $affiliateId;
$this->token = $token;
$this->response_type = $response_type;
$this->api_base.= $this->affiliateId.'.'.$this->response_type;
}
public function api_home(){
return $this->sendRequest($this->api_base);
}
public function call_url($url){
return $this->sendRequest($url);
}
private function sendRequest($url, $timeout=30){
if (function_exists('curl_init') && function_exists('curl_setopt')){
$headers = array(
'Cache-Control: no-cache',
'Fk-Affiliate-Id: '.$this->affiliateId,
'Fk-Affiliate-Token: '.$this->token
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-ClusterDev-Flipkart/0.1');
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->verify_ssl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);
return $result ? $result : false;
}else{
return false;
} }}$flipkart = new \clusterdev\Flipkart("shopXXonh", "4e88c0b31f3b45XXXa746fcfdae39af", "json");$alloffers_url ='https://affiliate-api.flipkart.net/affiliate/offers/v1/all/json';$url = isset($_GET['url'])?$_GET['url']:false;if($url){$hidden = isset($_GET['hidden'])?false:true;$details = $flipkart->call_url($url);if(!$details){echo 'Error: Could not retrieve products list.';exit();
}
$details = json_decode($details, TRUE);}$offer = isset($_GET['offer'])?$_GET['offer']:false;if($offer){if($offer == 'alloffers'){
$details = $flipkart->call_url($alloffers_url);if(!$details)
{
echo 'Error: Could not retrieve Top Offers.';
exit();
}
$details = json_decode($details, TRUE);
$list = $details['allOffersList'];
echo '<h2> All Offers</h2>';
echo "<table cellpadding=10 cellspacing=1 style='text-align:center'>";
$count = 0;
$end = 1;if(count($list) > 0){
//foreach ($list as $item)
//{
$count++;
/*echo"<pre/>";
print_r($list);
echo"<pre/>";*/
$page = isset($_REQUEST['page']) && $_REQUEST['page'] > 0 ? $_REQUEST['page'] : 1;
function display($list, $page = 1)
{
$start = ($page - 1) * 2;
$item = array_slice($list, $start, 2);
foreach ($list as $key => $val) {
/*echo"<pre/>";
print_r($list);
echo"<pre/>";*/
echo $val['title'] . '<br/>';
echo $val['description'] . '<br/>';
echo "<br>";}}$len = count($list);echo $len."<br/>";$pages = ceil($len /2);echo $pages;if ($page > $pages or $page < 1) {echo 'page not found';}else{display($list, $page);
for ($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 }}}}if($count==0){echo'<tr><td>No Top Offers returned.</td><tr>';}exit();}else{
echo 'Error: Invalid offer type.';
exit();}}echo '<h2> <a href="?offer=alloffers">All Offers</a></h2><br><br>';?>
答案 0 :(得分:0)
让我解释一下逻辑:
1)将数组计数到变量中。
2)从上面变量计算页数。
3)在记录下方显示分页href链接。
4)如果页码无效,则显示找不到页面错误。
<?php
$arr = array(
array('title' => 'Just at Rs. 799', 'description' => 'Ambrane'),
array('title' => 'Flat Rs.249', 'description' => 'Sarees & more'),
array('title' => ' Extra 10% off', 'description' => 'Routers'),
array('title' => 'Just Rs.549', 'description' => 'From Nova')
);
// display
$page = isset($_REQUEST['page']) && $_REQUEST['page'] > 0 ? $_REQUEST['page'] : 1;
function display($arr, $page = 1) {
$start = ($page - 1) * 2;
$arr = array_slice($arr, $start, 2);
foreach ($arr as $key => $val) {
echo $val['title'] . '<br/>';
echo $val['description'] . '<br/>';
echo "<br>";
}
}
$len = count($arr);
$pages = ceil($len / 2);
if ($page > $pages or $page < 1) {
echo 'page not found';
}
else {
display($arr, $page);
for ($i = 1 ; $i <= $pages ; $i++) {
$current = ($i == $page) ? TRUE : FALSE;
if ($current) {
echo '<b>' . $i . '</b>';
}
else {
?>
<a href="?page=<?php echo $i;?>"><?php echo $i;?></a>
<?php
}
}
}