IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> PHP知识库 -> 原生php生成静态页面 -> 正文阅读

[PHP知识库]原生php生成静态页面

目录结构

?

MyPdo.php类

<?php


class MyPdo
{
    private static $_instance = null;
    protected $dbName = '';
    protected $dsn;
    protected $dbh;

    public function __construct($dbHost, $dbUser, $dbPasswd, $dbName, $dbCharset = 'utf8')
    {
        try {
            $this->dsn = 'mysql:host=' . $dbHost . ';dbname=' . $dbName;
            $this->dbh = new \PDO($this->dsn, $dbUser, $dbPasswd);
            $this->dbh->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
            $this->dbh->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
            $this->dbh->exec('SET character_set_connection=' . $dbCharset . ';SET character_set_client=' . $dbCharset . ';SET character_set_results=' . $dbCharset);
        } catch (Exception $e) {
            $this->outputError($e->getMessage());
        }
    }

    public static function getInstance($dbHost, $dbUser, $dbPasswd, $dbName, $dbCharset = 'utf8mb4')
    {
        if (self::$_instance === null) {
            self::$_instance = new self($dbHost, $dbUser, $dbPasswd, $dbName, $dbCharset);
        }
        return self::$_instance;
    }

    public function fetchAll($sql, $params = array())
    {
        try {
            $stm = $this->dbh->prepare($sql);
            if ($stm && $stm->execute($params)) {
                return $stm->fetchAll(\PDO::FETCH_ASSOC);
            }
        } catch (Exception $e) {
            $this->outputError($e->getMessage());
        }
    }

    public function fetchOne($sql, $params = array())
    {
        try {
            $result = false;
            $stm = $this->dbh->prepare($sql);
            if ($stm && $stm->execute($params)) {
                $result = $stm->fetch(\PDO::FETCH_ASSOC);
            }
            return $result;
        } catch (Exception $e) {
            $this->outputError($e->getMessage());
        }
    }

    public function fetchColumn($sql, $params = array())
    {
        $result = '';
        try {
            $stm = $this->dbh->prepare($sql);
            if ($stm && $stm->execute($params)) {
                $result = $stm->fetchColumn();
            }
            return $result;
        } catch (Exception $e) {
            $this->outputError($e->getMessage());
        }
    }

    public function insert($table, $params = array(), $returnLastId = true)
    {
        $_implode_field = '';
        $fields = array_keys($params);
        $_implode_field = implode(',', $fields);
        $_implode_value = '';
        foreach ($fields as $value) {
            $_implode_value .= ':' . $value . ',';
        }
        $_implode_value = trim($_implode_value, ',');
        $sql = 'INSERT INTO ' . $table . '(' . $_implode_field . ') VALUES (' . $_implode_value . ')';
        try {
            $stm = $this->dbh->prepare($sql);
            $result = $stm->execute($params);
            if ($returnLastId) {
                $result = $this->dbh->lastInsertId();
            }
            return $result;
        } catch (Exception $e) {
            $this->outputError($e->getMessage());
        }
    }

    public function update($sql)
    {

        try {
            $stm = $this->dbh->exec($sql);
            return $stm;
        } catch (Exception $e) {
            $this->outputError($e->getMessage());
        }
    }

    public function delete($sql, $params = array())
    {
        try {
            $stm = $this->dbh->prepare($sql);
            $result = $stm->execute($params);
            return $result;
        } catch (Exception $e) {
            $this->outputError($e->getMessage());
        }
    }

    public function exec($sql, $params = array())
    {
        try {
            $stm = $this->dbh->prepare($sql);
            $result = $stm->execute($params);
            return $result;
        } catch (Exception $e) {
            $this->outputError($e->getMessage());
        }
    }

    private function outputError($strErrMsg)
    {
        throw new Exception("MySQL Error: " . $strErrMsg);
    }

    public function __destruct()
    {
        $this->dbh = null;
    }
}


展示页面? index.php

<?php
include_once './cla/MyPdo.php';
$pdo = MyPdo::getInstance('localhost', 'root', 'root', 'test');
$sql = "select * from news";
$res = $pdo->fetchAll($sql);
include './view/index.html';

展示页面index.html

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Layui</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="//res.layui.com/layui/dist/css/layui.css"  media="all">
  <!-- 注意:如果你直接复制所有代码到本地,上述css路径需要改成你本地的 -->
</head>
<body>

<div class="layui-form">
  <table class="layui-table">
    <colgroup>
      <col width="150">
      <col width="150">
      <col width="200">
      <col>
    </colgroup>
    <thead>
      <tr>
        <th>ID</th>
        <th>标题</th>
        <th>操作</th>
      </tr>
    </thead>
    <tbody>

    <?php
    foreach($res as $v){
    echo "<tr>";
    echo "<td>$v[id]</td>";
    echo "<td>$v[name]</td>";
    echo "<td><a href='descs.php?id=$v[id]'>详情</a></td>";
    echo "</tr>";
    }
    ?>



    </tbody>
  </table>
</div>




<script src="//res.layui.com/layui/dist/layui.js" charset="utf-8"></script>
<script>

</script>

</body>
</html>

descs.php

<?php

$id = $_GET['id'];
include './cla/MyPdo.php';
$pdo = MyPdo::getInstance('localhost', 'root', 'root', 'test');
$sql = "select * from news where id = $id";
$res = $pdo->fetchOne($sql);
$html_static = './html/descs' . $id . '.html';
if (file_exists($html_static)) {
    include $html_static;
}else{
    if ($res['static'] == 1) {
        //开启缓存
        ob_start();
        include './view/descs.html';
        //存入文件后取出文件
        $file = ob_get_contents();
        //生成静态文件
        file_put_contents($html_static, $file);
        ob_flush();
    } else {
        include './view/descs.html';
    }
}

descs.html文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js">
    </script>
</head>
<body>
<input type="hidden" value="<?php echo $res['id']; ?>" class="ids">
<div>标题:<?php echo $res['name'];  ?></div>
<div>内容:<?php echo $res['desc'];  ?></div>

访问量:<span class="num"></span>

</body>
</html>
<script>
    $.ajax({
        url:"looknum.php",
        data:{
            id:$(".ids").val()
        },
        dataType:"json",
        success:res=>{
            console.log(res)
            $(".num").html(res.num)
        }
    })

</script>

浏览量增加页面 looknum.php

<?php
$id = $_GET['id'];
include_once './cla/MyPdo.php';
$pdo = MyPdo::getInstance('localhost', 'root', 'root', 'test');
$sql = "select num from news where id = $id";
$res = $pdo->fetchOne($sql);
$sql1 = "update news set num= num+1 where id = $id";
 $res1=  $pdo->update($sql1);

echo json_encode($res);

  PHP知识库 最新文章
Laravel 下实现 Google 2fa 验证
UUCTF WP
DASCTF10月 web
XAMPP任意命令执行提升权限漏洞(CVE-2020-
[GYCTF2020]Easyphp
iwebsec靶场 代码执行关卡通关笔记
多个线程同步执行,多个线程依次执行,多个
php 没事记录下常用方法 (TP5.1)
php之jwt
2021-09-18
上一篇文章      下一篇文章      查看所有文章
加:2021-08-01 14:18:04  更:2021-08-01 14:19:08 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/5 1:41:35-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码