<
PHP+Mysql搭建动态博客网站【上】
>
上一篇

C/S架构NIO文件管理系统【上】
下一篇

P2P:局域网内文件传输【带界面】
用户界面【思想解析及代码框架】

整体设计【需求分析+数据库】

感悟:做一个稍微复杂一点的项目才知道提前搞好需求分析有多重要,我是写到目前的大概三分之一才开始画思维导图的,写着写着就开始混乱不知道去哪写下一行代码了

点击查看大图 1.png

演示视频

环境

PHP 静态部分

使用的是Bootstrap3框架,下载的源码

用框架还是非常方便的能尽量快的出效果,可以内部CSS覆盖样式,也可以直接修改框架内的CSS文件

基本还是HTML+CSS,我理解PHP部分完全就是哪里需要往哪搬

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> 
    <title><?php echo "title";?></title>
    <?php include('./header.vis.php');?>
    <style>
    </style>
</head>
<body>
    <div></div>
    <?php include('footer.php');?>
</body>
</html>

header.vis.php 引用bootstrap框架

<!--防止乱码></!-->
<meta charset="uf-8">
<script  src="./theme/js/jquery-3.2.1.min.js"></script>
<link rel="stylesheet" href="./theme/bootstrap/css/bootstrap.css">
<script src="./theme/bootstrap/js/bootstrap.js"></script>

footer.php 可以不是完整的HTML架构

<style>
    .footer {
        position: relative;
        bottom: 0;
        width: 100%;
        /* Set the fixed height of the footer here */
        height: 50px;
    }
</style>

<footer class="footer">
    <div class="container" style="text-align: center;line-height:400%;">
        YULIA's Static Blog @ https://cguling.github.io
    </div>
</footer>

PHP 动态部分

input.class.php 可以设类,进行引用,继而成为全局函数

<?php
	class input{
		/*类方法get,以get方法获取页面写入的数据*/
		function get($key=false){
			if($key === false){
				return $_GET;
			}
			if(isset($_GET[ $key ])){
				return $_GET[ $key ];
			}else{
				return false;
			}
        }
    }
?>

引用类及其函数示例

    <?php 
        include('input.class.php');
        $input = new input();/*生成一个input对象*/

        if($input->get('do')=='show'){
            echo "<script> alert('show'); </script>";/*PHP+JS*/
        }
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> 
        <title></title>
        <style>
        </style>
    </head>
    <body>
        <div>
            <a href="?do=show"><?php echo "Show";?></a>
        </div>
    </body>
    </html>

HTML嵌套PHP+PHP嵌套HTML

    <?php 
        $pid=1;
        $judge=substr($_SERVER["QUERY_STRING"],0,2);/*获取参数,并截取前两个字符*/
        if($judge=='do'){
            $var=explode("&",$_SERVER["QUERY_STRING"]);/*以‘&’进行分割*/
            $do=substr($var[0],3);/*从第三位开始截取*/
            if($do=="like"){
                echo "<script> alert('like'); </script>";/*PHP+JS*/
            }
            if($do=="enshrine"){
                echo "<script> alert('enshrine'); </script>";/*PHP+JS*/
            }
            $url=$_SERVER['PHP_SELF'];/*当前url路径不含参数*/
            echo "<meta http-equiv='Refresh' content='0;URL=$url'>";/*刷新到无参数的页面*/
        }
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> 
        <title></title>
        <style>
        </style>
    </head>
    <body>
        <p style="text-align: right; font-size:large;">
            <?php
                $url=$_SERVER['PHP_SELF'];/*当前url路径不含参数*/
                echo '<a href="'.$url.'?do=like&pid='.$pid.'">';/*PHP中字符串的连接用'.'*/
            ?>
                <span class="glyphicon glyphicon-thumbs-up"></span>
            </a>
            &nbsp;&nbsp;<!--空格-->
            <?php
                if($session_aid === false){
                    echo '<a href="" data-toggle="modal" data-target="#login">';
                }
                else {
                    $url=$_SERVER['PHP_SELF'];
                    echo '<a href="'.$url.'?do=enshrine&pid='.$pid.'">';
                }
            ?>
                <span class="glyphicon glyphicon-heart"></span>
            </a>
        </p>
    </body>
    </html>

Mysql+PHP

db.class.php 连接数据库

<?php
	class db{
		/*一个类construct方法,注意前面是双下划线,此方法为自动执行*/
		function __construct(){
			/*连接数据库语句*/
			$this->mysqli=new mysqli('localhost','root','密码','数据库名');
			/*如果连接出现错误怎么办*/
			if ($this->mysqli->connect_error) {
	    		die('Connect Error (' . $this->mysqli->connect_errno . ') '
	            		. $this->mysqli->connect_error);
			}
			/*处理数据库传来的中文乱码的问题*/
			$this->query("SET NAMES UTF8");
		}
		/*一个类query方法*/
		function query( $sql ){
			/*在query类方法里执行数据库语句query*/
			return $this->mysqli->query( $sql );
		}

	}	
?>

使用数据库,打印数据示例

    <?php
        include('db.class.php');
        /*生成一个db的对象*/
        $db = new db();
        $sql="select * from admin limit 5";
        $result=$db->query($sql);
        $lists=array();
        while($row=$result->fetch_array(MYSQLI_ASSOC)){
            $lists[]=$row;
        }
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> 
        <title></title>
        <style>
        </style>
    </head>
    <body>
        <div>
            <ul class="list-group">
            <?php foreach($lists as $list):?><!--循环遍历$lists这个数组-->
                <li class="list-group-item">
                <?php 
                    if ($list === reset($lists)){/*判断是否为第一次循环*/
                        echo '<span class="glyphicon glyphicon-star"></span>';/*PHP+HTML*/
                    }
                ?>
                <?php/*HTML内执行mysql语句*/
                    $pid=$list['pid'];
                    $sql="select * from classify where pid={$pid}";
                    $count=$db->query($sql)->fetch_array(MYSQLI_ASSOC);
                ?>
                <span class="badge"><?php echo $count['like'];?></span><!--打印点赞数-->
                    <a href="?pid=<?php echo $pid;?>"><?php echo $list['title'];?></a><!--打印标题-->
                </li>
            <?php endforeach;?><!--结束循环-->
            </ul>
        </div>
    </body>
    </html>

小板块-日历

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> 
        <title></title>
	    <link rel="stylesheet" href="./theme/bootstrap/css/dcalendar.picker.css"/>
    </head>
    <body>
        <div>
            <h3>&emsp;&nbsp;<span class="glyphicon glyphicon-calendar"></span>&nbsp;日历-Calendar</h3>
            <table id='mycalendar' class='calendar'></table>
        </div>
        <script type="text/javascript" src="./theme/js/dcalendar.picker.js"></script>
        <script type="text/javascript">
            $('#mycalendar').dcalendar();
        </script>
    </body>
    </html>

小板块-分页

<?php
    include('input.class.php');
    $input = new input();/*生成一个input对象*/

	/*pagenum来定义一个分页的显示数量*/
	$pageNum=$setting['pagenum'];
	/*获取数据总量*/
	$sql="select count(*) AS total from page";
	$total=$db->query($sql)->fetch_array(MYSQLI_ASSOC)['total'];
	/*分页的页码总数*/
	$maxPage=ceil($total/$pageNum);
	/*获得当前page的参数*/
	$page=(int)$input->get('page');/*网页传参*/
	$page=$page <1 ? 1 : $page;
	/*当前页码的偏移量*/
	$offset=($page -1)*$pageNum;
	/*取出当前数据库列表中的信息并为实现分页效果*/
	$sql="select * from page order by pid asc limit {$offset},{$pageNum}";
	$result=$db->query($sql);
	$rows=array();
	while($row=$result->fetch_array(MYSQLI_ASSOC)){
		$rows[]=$row;
	}
 ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> 
    <title></title>
</head>
<body>
    <div>
        <div style="padding: 20px 10px 0 30px;">
        <table class="table table-hover">
            <thead>
            <tr>
                <th>PID</th>
                <th>标题</th>
                <th>作者</th>
            </tr>
            </thead>
            <tbody>
            <?php foreach($rows as $row) :?>
            <tr>
                <td><?php echo $row['pid'];?></td>
                <td><?php echo $row['title'];?></td>
                <td><?php echo $row['author'];?></td>
            </tr>
            <?php endforeach;?>
            </tbody>
        </table>
        </div>
        <!--底部的分页效果></!-->	
        <nav aria-label="Page navigation">
            <ul class="pagination" style="margin-left: 40px;">
            <li>
                <a href='#' aria-label="Previous">
                <span aria-hidden="true">&laquo;</span>
                </a>
            </li>			    
            <?php 
                $hrefTpl = "<li><a href='?page=%d'>%s</a></li>";
                for ($i=1; $i<=$maxPage; $i++){
                    echo sprintf( $hrefTpl,$i,"第{$i}页" );
                }
            ?>
            <li>
                <a href='#' aria-label="Next">
                <span aria-hidden="true">&raquo;</span>
                </a>
            </li>
            </ul>
        </nav>
    </div>
</body>
</html>

参考链接

推荐一下这个资源,不过于简单也不是很复杂非常适合直接上手练习

Html+Css+Mysql+Php的动态博客网站

PS:我的网站还没搭建完,搭建完成再放资源

Top
Foot