未显示编码后仍显示博客文章

时间:2019-01-19 16:11:44

标签: php mysqli

我有一个用PHP编写的博客网站。在该网站中,所有博客帖子均来自名为blogs的表,并且具有名为blog_status的行,该行设置为onoff

如果将其定义为on,则意味着该博客文章可以显示在网站上,如果off,则该博客文章将无法查看。

function get_blogposts($id_number,$result_num){
$hr = "";
$get_blogposts = "SELECT * FROM blogs LIMIT " . $id_number . ", " . $result_num;
$run_blogposts = mysqli_query($GLOBALS['con'],$get_blogposts);
$row_count = mysqli_num_rows($run_blogposts);
if($row_count == 0){
    header('Location: blogs.php?page=1');
}
if($row_count > 1){
    $hr = "<hr>";
}
while($row_blogposts = mysqli_fetch_array($run_blogposts)){
    $blo_tbl = $row_blogposts['table_id'];
    $blo_id = $row_blogposts['blog_id'];
    $blo_title = $row_blogposts['blog_title'];
    $blo_body = $row_blogposts['blog_body'];
    $blo_cat = $row_blogposts['blog_category'];
    $blo_tags = $row_blogposts['blog_tags'];
    $blo_date = $row_blogposts['date_posting'];
    if(!empty($row_blogposts['blog_status'])){
        $blo_status = $row_blogposts['blog_status'];
    }
    $blo_views = $row_blogposts['views'];
    $blo_insertion = $row_blogposts['insertion'];

    if(strlen($blo_body) > 140)
    {
        $blo_body = substr($blo_body, 0, 1000);
        $blo_body = explode(' ', $blo_body);
        array_pop($blo_body); 
        $blo_body = implode(' ', $blo_body);
        $blo_body = $blo_body . ' ...';
    }

    if($blo_status != "off"){
        echo "
            <div class='row'>
                <div class='col-md-2'></div>
                <div class='col-md-8'>
                    <h1 class='BTitrBold'>$blo_title <sup><a class='category' href='blogs.php?blog_cat=$blo_cat'>".blog_cat($blo_cat)."</a></sup></h1>
                    <p class='BHoma para'></br>
                    $blo_body </br></br>
                    </p>
                    <a href='blog_post.php?blog_id=$blo_id' class='bishtar BJadidBold'>Read more</a></br></br></br></br>$hr
                </div>
                <div class='col-md-2'></div>
            </div>
        ";
    }
}

}

现在的问题是,当我将博客状态设置为off时,当我编写以下语句时,它仍显示在页面上:

if($blo_status != "off"){
        echo "
            <div class='row'>
                <div class='col-md-2'></div>
                <div class='col-md-8'>
                    <h1 class='BTitrBold'>$blo_title <sup><a class='category' href='blogs.php?blog_cat=$blo_cat'>".blog_cat($blo_cat)."</a></sup></h1>
                    <p class='BHoma para'></br>
                    $blo_body </br></br>
                    </p>
                    <a href='blog_post.php?blog_id=$blo_id' class='bishtar BJadidBold'>Read more</a></br></br></br></br>$hr
                </div>
                <div class='col-md-2'></div>
            </div>
        ";
    }

1 个答案:

答案 0 :(得分:-1)

如果只需要blog_status处于启用状态的数据,则选择全部然后进行过滤不是正确的方法。您应该从数据库中获取过滤的数据。 更改此行:

$get_blogposts = "SELECT * FROM blogs LIMIT " . $id_number . ", " . $result_num;

具有:

$get_blogposts = "SELECT * FROM blogs WHERE blog_status='on' LIMIT " . $id_number . ", " . $result_num;