Mysql
 sql >> Cơ Sở Dữ Liệu >  >> RDS >> Mysql

Giới hạn số trang phân trang

Ok nếu bạn muốn hiển thị một cái gì đó như

Prev 1 2 3 4 5 6 .. 40 41 Next 
Prev 1 2 .. 6 7 8 9 10 .. 40 41 Next

Điều đầu tiên chúng ta cần là tạo một hàm có thể xử lý những gì chúng ta cần để xuất ra phân trang. Đây là một chức năng tôi sử dụng và nó hoạt động tốt.

function get_paging_info($tot_rows,$pp,$curr_page)
{
    $pages = ceil($tot_rows / $pp); // calc pages

    $data = array(); // start out array
    $data['si']        = ($curr_page * $pp) - $pp; // what row to start at
    $data['pages']     = $pages;                   // add the pages
    $data['curr_page'] = $curr_page;               // Whats the current page

    return $data; //return the paging data

}

Bây giờ chức năng này khá vững chắc và hoạt động rất tốt đối với tôi.

Vì vậy, bạn vượt qua chức năng này

  • $ tot_rows =các hàng được tính cho truy vấn
  • $ pp =các mục trên mỗi trang
  • $ curr_page =số trang hiện tại

Được rồi, bây giờ bạn đã có dữ liệu cần thiết, bạn sẽ cần hiển thị dữ liệu đó.

Đây là những gì tôi sử dụng và vui lòng đọc nó trước khi bạn nghĩ, 'à, nó quá dài'. Nó thực sự rất đơn giản.

Đây là ảnh chụp nhanh về những gì nó sẽ trả về

    <!-- Create the query -->
    <?php $count = mysql_fetch_assoc( mysql_query ( "SELECT COUNT( rows ) as count FROM table" ) ) ;

    <?php $count = $count[0]['count']; ?>

<!-- Call our function from above -->
<?php $paging_info = get_paging_info($count,5,34); ?>


<p>
    <!-- If the current page is more than 1, show the First and Previous links -->
    <?php if($paging_info['curr_page'] > 1) : ?>
        <a href='' title='Page 1'>First</a>
        <a href='' title='Page <?php echo ($paging_info['curr_page'] - 1); ?>'>Prev</a>
    <?php endif; ?>



    <?php
        //setup starting point

        //$max is equal to number of links shown
        $max = 7;
        if($paging_info['curr_page'] < $max)
            $sp = 1;
        elseif($paging_info['curr_page'] >= ($paging_info['pages'] - floor($max / 2)) )
            $sp = $paging_info['pages'] - $max + 1;
        elseif($paging_info['curr_page'] >= $max)
            $sp = $paging_info['curr_page']  - floor($max/2);
    ?>

    <!-- If the current page >= $max then show link to 1st page -->
    <?php if($paging_info['curr_page'] >= $max) : ?>

        <a href='' title='Page 1'>1</a>
        ..

    <?php endif; ?>

    <!-- Loop though max number of pages shown and show links either side equal to $max / 2 -->
    <?php for($i = $sp; $i <= ($sp + $max -1);$i++) : ?>

        <?php
            if($i > $paging_info['pages'])
                continue;
        ?>

        <?php if($paging_info['curr_page'] == $i) : ?>

            <span class='bold'><?php echo $i; ?></span>

        <?php else : ?>

            <a href='' title='Page <?php echo $i; ?>'><?php echo $i; ?></a>

        <?php endif; ?>

    <?php endfor; ?>


    <!-- If the current page is less than say the last page minus $max pages divided by 2-->
    <?php if($paging_info['curr_page'] < ($paging_info['pages'] - floor($max / 2))) : ?>

        ..
        <a href='' title='Page <?php echo $paging_info['pages']; ?>'><?php echo $paging_info['pages']; ?></a>

    <?php endif; ?>

    <!-- Show last two pages if we're not near them -->
    <?php if($paging_info['curr_page'] < $paging_info['pages']) : ?>

        <a href='<?php echo str_replace('/page'.$paging_info['curr_page'], '', $paging_info['curr_url']) . '/page'.($paging_info['curr_page'] + 1); ?>' title='Page <?php echo ($paging_info['curr_page'] + 1); ?>'>Next</a>

        <a href='<?php echo str_replace('/page'.$paging_info['curr_page'], '', $paging_info['curr_url']) . '/page'.$paging_info['pages']; ?>' title='Page <?php echo $paging_info['pages']; ?>'>Last</a>

    <?php endif; ?>
</p>


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Định dạng ngày chèn mysql PHP

  2. Ví dụ về WEEKOFYEAR () - MySQL

  3. Sự cố LAG / LEAD của MySQL

  4. Kết nối mysql từ xa qua máy chủ miễn phí

  5. Làm cách nào để tạo và tải cơ sở dữ liệu thứ hai trong ddev?