php分页应从1开始,而不是从0开始


php pagination should start from 1 not 0

这是我从xml文件下载数据并对其进行分页的函数。

<?php 
function get_feed_posts($link) {
        if(!isset($_GET['page'])) {
            $_GET['page'] = 0;
        }
        $startPage = $_GET['page'];
        $perPage = 13;
        $currentRecord = 0;
        $xml = new SimpleXMLElement($link, 0, true);
        foreach($xml->results->result as $item) {
            $currentRecord += 1;
            if($currentRecord > ($startPage * $perPage) && $currentRecord < ($startPage * $perPage + $perPage)) {
        ?>
            <li><a class="go-to" href="<?php echo $item->click_url;?>" title="<?php echo $item->name;?>">Go to store</a></li>
        <?php
            }
        }
        for ($i = 0; $i <= ($currentRecord / $perPage); $i++) {
            $n=$i+1;
            echo("<a href='?page=".$n."'>".$n."</a>");
        }
} ?>

这个代码工作得很好。但是我想让我的页面从?page=1开始,现在这个代码从?page=0 开始

这个怎么样?

<?php 
function get_feed_posts($link) {
    if(!isset($_GET['page'])) {
        $_GET['page'] = 1; // changed this line
    }
    $startPage = ($_GET['page'] < 1) ? 0 : $_GET['page'] - 1;  // changed this line
    $perPage = 13;
    $currentRecord = 0;
    $xml = new SimpleXMLElement($link, 0, true);
    foreach($xml->results->result as $item) {
        $currentRecord += 1;
        if($currentRecord > ($startPage * $perPage) && $currentRecord < ($startPage * $perPage + $perPage)) {
    ?>
        <li><a class="go-to" href="<?php echo $item->click_url;?>" title="<?php echo $item->name;?>">Go to store</a></li>
    <?php
        }
    }
    for ($i = 0; $i <= ($currentRecord / $perPage); $i++) {
        $n=$i+1;
        echo("<a href='?page=".$n."'>".$n."</a>");
    }
} ?>

请注意,?page=x中任何小于1的x都将被视为0,但您可以做得更好(抛出一个错误?重定向到?page=1?)。