使用mysql拆分页码选项

时间:2015-08-20 07:43:03

标签: php mysql

<?php 
if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; } 
$start_from = ($page-1) * 20; 
$sql = "SELECT * FROM students ORDER BY name ASC LIMIT $start_from, 20"; 
$rs_result = mysql_query ($sql,$connection); 
?> 
<table>
<tr><td>Name</td><td>Phone</td></tr>
<?php 
while ($row = mysql_fetch_assoc($rs_result)) { 
?> 
            <tr>
            <td><? echo $row["Name"]; ?></td>
            <td><? echo $row["PhoneNumber"]; ?></td>
            </tr>
<?php 
}; 
?> 
</table>
<?php 
$sql = "SELECT COUNT(Name) FROM students"; 
$rs_result = mysql_query($sql,$connection); 
$row = mysql_fetch_row($rs_result); 
$total_records = $row[0]; 
$total_pages = ceil($total_records / 20); 
  
for ($i=1; $i<=$total_pages; $i++) { 
            echo "<a href='pagination.php?page=".$i."'>".$i."</a> "; 
}; 
?>

一切正常但我想要的结果就像stackoverflow,即第1,2,3,4,5页......最后一页......和下​​一页

2 个答案:

答案 0 :(得分:0)

在你的速记标签中,即

`<td><? echo $row["Name"]; ?></td>`

使用

<?php echo 'text'

<?='text'

其次,你不需要';'在你的循环结束时,即for和while

答案 1 :(得分:0)

试试这个!它对我来说很完美。

$page = $_REQUEST["page"];
if(!$page) $page = 1;
$limit = ($page - 1) * 20;

$sql = "SELECT * FROM students ORDER BY name ASC"; 
$rs_result = mysql_query ($sql,$connection); 
$rows = mysql_num_rows($rs_result);
$a = $rows/20;

if ($a == floor($rows/20 * -1)) 
    $page_= $a;
else 
    $page_ = floor($rows/20 * -1);

$page_ = $page_ * -1;

$sql = "SELECT * FROM students ORDER BY name ASC LIMIT $limit, 20"; 
$rs_result = mysql_query ($sql,$connection); 
<table>
    <tr><td>Name</td><td>Phone</td></tr>
    <?php 
    while ($row = mysql_fetch_assoc($rs_result)) { 
        ?> 
        <tr>
            <td><?php echo $row["Name"]; ?></td>
            <td><?php echo $row["PhoneNumber"]; ?></td>
        </tr>
        <?php 
    }
    ?> 
</table>

<table>
    <tr>
        <?php
        if($page_==0){
        }else{
            $b=$page+9;
            $prev = $page-1;
            $next = $page+1;
            $first= 1;
            $last= $page_;
            echo "<td align=center>";

            if ($page != 1)
                echo " <a href='pagination.php?page=$first' style='text-decoration:NONE;'> First </a> ";
            else
                echo " First "; 

            if ($page != 1)
                echo " <a href='pagination.php?page=$prev' style='text-decoration:NONE;'>Prev </a> ";
            else
                echo " Prev ";

            echo "</td>";

            for ($i=$page;$i<=$b;$i++) {
                if ($page==$i){
                    echo "<td align=center>";
                    echo "$i";
                    echo"&nbsp &nbsp";
                    echo "</td>"; 
                } else {  
                    echo "<td align=center>";
                    echo "<a href='pagination.php?page=$i'>$i </a>"; 
                    echo"&nbsp &nbsp";
                    echo "</td>"; 
                }

                if($i==$page_)
                    break;
            }

            echo "<td align=center>";
            echo"&nbsp &nbsp";

            if ($page != $page_)
               echo "<a href='pagination.php?page=$next' style='text-decoration:NONE;'>Next </a>";
            else
               echo "Next";

            echo"&nbsp &nbsp";

            if ($page != $page_)
                echo "<a href='pagination.php?page=$last' style='text-decoration:NONE;'> Last </a>";
            else
                echo "Last";

            echo "</td>";
        }
        ?>
    </tr>
</table>