dongpai2754 2016-01-25 10:57
浏览 39
已采纳

变量的php分页包含数组

i want to display 2 array record per pages.Please help with the simple pagination.when user clicks on next page next 2 array record should display.

[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
                )
)

This is what i have tried and the array contains around 1500 Records.Pagination is not looking good. Its giving very big result and if i wanna hide some of the numbers in between just to reduce its width. or may be next option or dot in between. I wanna display minimum of 15-20 records per pages.

<?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>';?>
  • 写回答

1条回答 默认 最新

  • douyin8623 2016-01-25 11:12
    关注

    Let me explain you logic:

    1) Take count of array into a variable.

    2) Count number of page from above variable.

    3) Show pagination href links below the records.

    4) If the page number is invalid, show page not found error.

    <?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
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码