限制按页面获取结果

时间:2013-07-31 09:58:59

标签: php mysql

这个文件包含在我的索引文件中,我想在页面上和帖子1 2 3 4 ...等下面不超过5个帖子(链接到下一页),它看起来像这个index.php ?页= 2 抱歉我的语法不好。

    <?php
    if(isset($_GET['post_edit'])) {
    $p_id = $_GET['post_edit'];

    $p_query = mysql_query("SELECT title, post FROM posts WHERE id='$p_id'");
    $p_array = mysql_fetch_array($p_query);
    $title = $p_array['title'];
    $post = $p_array['post'];
    }
    ?>

    <?php
    if(isset($_POST['edit'])){
        $title_edit = $_POST['titleedit'];
        $post_edit = $_POST['postedit'];

    if(empty($title) or empty($post)){
        echo "<p>Fields empty!</p>";
        } else {
        mysql_query("UPDATE posts SET title='$title_edit', post='$post_edit' WHERE id='$p_id'");
        echo "Edit succesful!</br>";
        header('location: index.php');
    }
    }
    ?>

    <?php
    if(isset($_GET['post_edit']) && !empty($_GET['post_edit'])){
    include 'edit_post.php';
    } else {

    ?>
    <?php

    $query = mysql_query("SELECT * FROM posts ORDER BY date DESC");
        while($row = mysql_fetch_array($query)){


    echo "<div class='poststitle'>";
        echo "<div style='font-weight: bold;'>";
            echo $row['title'];
        echo "</div>";
    echo "</div>";

    echo "<div class='posts'>";
        echo $row['post'];
        echo "</br>";
        echo "<hr>";

        $user_name = mysql_query("SELECT username FROM users WHERE id = '".$row['user']."' ");
        $user_name_array = mysql_fetch_array($user_name);
            $post_id = $row['id'];

        echo "Posted by: <b>";
        echo $user_name_array['username'];
        echo "</b> | ";

        echo "Views: <b>";
        echo $row['views'];
        echo "</b> | ";

        echo "Posted on: ";
        echo "<b>";
        echo $row['date'];
        echo "</b><hr>";

    echo '</div>';

    if (loggedin()){
    if($user_level == 1){
        echo "<div class='postoptions'>";
        echo "<a href='index.php?post_edit=$post_id'><img src='img/optionicons/edit.png' width='15' height='15' alt='edit' /></a>";
        echo "<a href='del_post.php?del=$post_id'><img src='img/optionicons/cancel.png' width='15' height='15' alt='Delete' /></a>";
        echo "</div>";
        } else {
        echo "";
        }
    }
}
}
?>

2 个答案:

答案 0 :(得分:0)

您应该在MySQL查询中使用LIMIT子句,请参阅the docs

为了计算numbe rof页面,您还需要总行数,可以使用SQL_CALC_FOUND_ROWS找到,请参阅the docsthis question

答案 1 :(得分:0)

SELECT * FROM table_name LIMIT N,M

其中N - 有限行数和M(偏移)= N *(PAGE -1)