PHP &

5 Cevap php

Benim web sayfasında benim sayfalama güncellenen ve ben eklemek çalışıyorum yana bir süre oldu First ve benim sayfalandırmaya Last Linkler yanı sıra ... arama sonuçları uzun zaman. Örneğin ben aşağıdaki örnekte aşağıdaki ulaşmak için çalışıyorum. Ben sitemde güncelleme böylece bazı biri bana kodu düzeltmek yardımcı olabilir. Teşekkürler

First Previous 1 2 3 4 5 6 7 ... 199 200 Next Last 

Şu anda aşağıdaki kodumu kullanılarak görüntülenir var.

Previous 1 2 3 4 5 6 7 Next

İşte bağlantıları görüntüler benim Sayfalandırması kod parçasıdır.

if ($pages > 1) {

    echo '<br /><p>';

    $current_page = ($start/$display) + 1;

    if ($current_page != 1) {
        echo '<a href="index.php?s=' . ($start - $display) . '&p=' . $pages . '">Previous</a> ';
    }

    for ($i = 1; $i <= $pages; $i++) {
        if ($i != $current_page) {
            echo '<a href="index.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '">' . $i . '</a> ';
        } else {
            echo '<span>' . $i . '</span> ';
        }
    } 

    if ($current_page != $pages) {
        echo '<a href="index.php?s=' . ($start + $display) . '&p=' . $pages . '">Next</a>';
    }

    echo '</p>';

}

5 Cevap

Tekerleği yeniden icat değil ilgi olarak, bu basit bir PHP sayfalama sınıfını kontrol etmek isteyebilirsiniz:

http://www.catchmyfame.com/2007/07/28/finally-the-simple-pagination-class/

Muhtemelen kutunun dışında sizin için mükemmel olmayacak, ancak değiştirmek ve pagination çok daha ilginç şeyler kodlama geri almak oldukça kolay :)

The only tutorial you need. http://www.php-mysql-tutorial.com/wikis/php-tutorial/paging-using-php.aspx

Bu benim için çalıştı. Orada bir parçası 2 olduğunu öğretici de ama ihtiyacınız olmayacak. Bu varolan kodunuzu değiştirmek için nasıl söyler.

Sadece toplam almak ve geriye:

$result_count = 200; // hypothetical

// run your regular code.....

$end_pages_to_display = 3;

for($i = $result_count; $i <= ($result_count - $end_pages_to_display); $i--)
{
    echo "<a href='index.php?page={$i}'>{$i}</a>";
}

Yani ... bu siteniz için işe gidiyor kodu değil, ama aynı mantık.

Ben senin 's' ve 'p' $ _GET değişkenleri (tuz ve biber?) Için ne emin değilim, bu yüzden ben sadece sayfa için, 'p' ile çalışıyorum.

<?php
$max = '20';
if ($pages > 1) {

    echo '<br /><p>';

    $current_page = ($start/$display) + 1;

    //add this here... first will always be one
        echo '<a href="index.php?p=1">First</a>';
    if ($current_page != 1) {
        echo '<a href="index.php?s=' . ($start - $display) . '&p=' . $pages . '">Previous</a> ';
    }

    for ($i = 1; $i <= $pages; $i++) {
        if ($i != $current_page) {
            echo '<a href="index.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '">' . $i . '</a> ';
        } else {
            echo '<span>' . $i . '</span> ';
        }
        // add this here... 
        if ( $i == $max){
            // stop the for() loop
            break;
        // not so fancy way of displaying last two pages, use other example if you want to get fancy.
        echo '<a href="index.php?p=' . ($pages - 1) . '">'.($pages - 1).'</a> ';
        echo '<a href="index.php?p=' . ($pages) . '">'.($pages).'</a> ';

        }
    } 

    if ($current_page != $pages) {
        echo '<a href="index.php?s=' . ($start + $display) . '&p=' . $pages . '">Next</a>';
    }
        echo '<a href="index.php?p=1">Last</a>';
    echo '</p>';

}
?>

Daha kolay bir yolu olabilir: a pre-built kütüphane kullanımı ...

http://framework.zend.com/manual/en/zend.paginator.html