[MySqli扩展]①⑤--显示评论内容

comment.class.php

data = $data;
    }

    /**
     * 检测用户输入的数据
     * @param $arr
     * @return bool
     */
    public static function validate(&$arr)
    {
        if (!($data['email'] = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL))) {
            $errors['email'] = '请输入合法邮箱';
        }
        if (!($data['url'] = filter_input(INPUT_POST, 'url', FILTER_VALIDATE_URL))) {
            $errors['url'] = '';
        }
        if (!($data['content'] = filter_input(INPUT_POST, 'content', FILTER_CALLBACK, array('options' => 'Comment::validate_str')))) {
            $errors['content'] = "请输入合法内容";
        }
        if (!($data['username'] = filter_input(INPUT_POST, 'username', FILTER_CALLBACK, array('options' => 'Comment::validate_str')))) {
            $errors['username'] = "请输入合法用户名";
        }
        $options = array(
            'min_range' => 1,
            'max_range' => 5
        );
        if (!($data['face'] = filter_input(INPUT_POST, 'face', FILTER_VALIDATE_INT, $options))) {
            $errors['face'] = "请输入合法头像";
        }
        if (!empty($errors)) {
            $arr = $errors;
            return false;
        }
        $arr = $data;
        $arr['email'] = strtolower(trim($arr['email']));
        return true;

    }

    /**
     * 过滤用户输入的特殊字符
     * @param $str
     * @return bool|string
     */
    public static function validate_str($str)
    {
        if (mb_strlen($str, 'UTF8') < 1) {
            return false;
        }
        //nl2br 将\n转换成br
        //htmlspecialchars 把一些预定义的字符转换为 HTML 实体
        //ENT_QUOTES单引号也转义
        $str = nl2br(htmlspecialchars($str, ENT_QUOTES));
        return $str;
    }

    /**
     * 显示评论内容
     * @return string
     */
    public function output()
    {
//        if ($this->data['url']) {
            $link_start = "";

            $link_end = "";
//        }
        $dateStr = date("Y年m月d日 H:i:s", $this->data['pubTime']);
        $res = <<
            
{$link_start} {$link_end}
{$link_start} {$this->data['username']} {$link_end}
{$dateStr}

{$this->data['content']}

EOF; return $res; } }

doAction.php

prepare($sql);
    $arr['pubTime'] = time();
    $mysqli_stmt->bind_param('sssssi', $arr['username'], $arr['email'], $arr['url'], $arr['face'], $arr['content'], $arr['pubTime']);
    $mysqli_stmt->execute();
    $comment = new Comment($arr);
    echo json_encode(array('status' => 1, 'html' => $comment->output()));
} else {
    echo '{"status":0,"errors":' . json_encode($arr) . '}';
}

?>

connect.php

errno) {
    die('CONNECT ERROR ' . $mysqli->error);
} else {
    $mysqli->set_charset('UTF8');
}

index.php

query($sql);
if($mysqli_result&& $mysqli_result->num_rows>0){
    while($row=$mysqli_result->fetch_assoc()){
        $comments[]=new Comment($row);
    }
}
?>



    
    Document
    


慕课网评论系统

output(); } ?>
![](img/1.jpg)    ![](img/2.jpg)    ![](img/3.jpg)    ![](img/4.jpg)    ![](img/5.jpg)   
[MySqli扩展]①⑤--显示评论内容_第1张图片
Paste_Image.png

你可能感兴趣的:([MySqli扩展]①⑤--显示评论内容)