-2

我有 PHP 脚本,我编写了主题系统
,我想在一页中显示每 10 个主题,怎么做?
sql表:

ID
L_TITLE
L_URL
L_COMMENTS
L_VIEWS
L_STATE

php代码:

$sql = mysql_query("SELECT * FROM LESSONS WHERE L_STATE='0' ORDER BY ID DESC") or die($sql_error);
if(mysql_num_rows($sql) > 0){
    while($rs = mysql_fetch_assoc($sql)){
        $l_id = $rs['ID'];
        $l_title = $rs['L_TITLE'];
        $l_comments = $rs['L_COMMENTS'];
        $l_views = $rs['L_VIEWS'];
    }
}else{
        $l_id = $no_data;
        $l_title = $no_data;
        $l_comments = $no_data;
        $l_views = $no_data;
}
4

2 回答 2

1

您可以通过多种方式执行此操作。

我看到的一种常见方式是让一个页面查询整个数据库,然后将结果存储到一个数组中。一个变量用于决定每页有多少结果,一个变量用于保存活动页面。您可以使用 GET 或 POST 来管理当前页面。

例子:

如果您有 100 个项目,并且希望每页显示 10 个项目,那么您将有 10 个页面。(如果您有 91 个项目,它也应该显示 10 页!)

$sql = mysql_query("SELECT * FROM LESSONS WHERE L_STATE='0' ORDER BY ID DESC") or die($sql_error);

//DETERMINE PAGE SIZE/NUMBERS
$currentPage = $_GET["page"];
$itemPerPage = 10;
$itemTotal = mysql_num_rows($sql);
$lastPage = $itemTotal / $itemPerPage + 1; //+1, because you round up to whole pages
$startItem = $currentPage * $itemsPerPage;

if($itemTotal > 0){
    //PUT DATA IN ARRAYS
    while($rs = mysql_fetch_assoc($sql)){
        $id[] = $rs['ID'];
        $l_title[] = $rs['L_TITLE'];
        $l_comments[] = $rs['L_COMMENTS'];
        $l_views[] = $rs['L_VIEWS'];
    }

    //DETERMINE WHAT PAGE AND ITEMS TO SHOW
    if(0 < $currentPage && $currentPage <= $lastPage){
        for($i=$startItem;$i<=$startItem+$itemsPerPage;$i++){
            //EXIT LOOP IF END OF ITEMS
            if($i > $itemTotal){
                break;
            }
            //DISPLAY DATA
            echo "<table><tr><th>id</th><th>title</th><th>comments</th><th>views</th></tr><tr>";
            echo "<tr><td>" . $id[$i] . "</td><td>" . $l_title[$i] . "</td><td>" . $l_comments[$i] . "</td><td>" . $l_views[$i] . "</td></tr>";
            echo "</table>";
        }
        //DISPLAY PAGE NUMBERS
        echo "<br />";
        echo "<a href='/?page=0'>|&lt;</a>";
        echo "<a href='/?page=" . $currentPage - 1 . "'>&lt;</a>";
        for($i=0;$i<$lastPage;$i++){
            echo "<a href='/?page=" . $i . "'>" . $i . "</a>";
        }
        echo "<a href='/?page=" . $currentPage + 1 . "'>&gt;</a>";
        echo "<a href='/?page=" . $lastPage . "'>&gt;|</a>";
        echo "<br />";
    } else {
        //If invalid page number, send to page 0
        header("Location: /?page=0");
    }
}else{
        $l_id = $no_data;
        $l_title = $no_data;
        $l_comments = $no_data;
        $l_views = $no_data;

}
于 2013-02-01T01:46:24.157 回答
0

First, don't use mysql_* functions anymore. They aren't officially depreciated, but they might as well be. Second. I've just glanced over the code for a second but try making those variables in the while loop arrays i.e

$l_id[] = $rs['ID'];

Then call $l_id with a foreach loop

EDIT: I guess I'm wrong abotu the mysql_* functions as well, it appears the yare officially depreciated.

于 2013-02-01T01:06:42.623 回答