PHP之分页显示数据-新闻系统
一、项目设计
1.项目要求2.主页面使用frame框架进行设计,内容如下:
index.php
<span style="font-family:SimSun;font-size:18px;"><frameset rows="80%,20%" frameborder="no" border="0" cols="900px"> <frameset cols="10%,80%,10%"> <frame src="sidebar.php" name="sidebar" noresize scrolling="no"> <frameset rows="20%,80%"> <frame src="navigationbar.php" name="navigation" noresize scrolling="no"> <frame src="home.php" name="content" noresize scrolling="yes"> </frameset> <frame src="sidebar.php" name="sidebar" noresize scrolling="no"> </frameset> <frame src="footer.php" name="footer"> </frameset></span>二、准备工作
1.创建项目文件夹,定义配信息
文件夹名称:news
配置文件名:config.php
<span style="font-size:18px;"><?php define("HOST","localhost"); define("USERNAME","root"); define("PASSWORD",""); define("CHARSET","utf8");?></span>
2..创建项目数据库
create_db.php
<span style="font-size:18px;"><!--执行该页面用于创建一个数据库--> <meta http-equiv="content-Type" content="text/html;charset=utf-8"> <title>创建数据库</title> <?php require_once 'config.php'; $conn=mysql_connect(HOST,USERNAME,PASSWORD); if(!$conn) die('Coucld not connect:'.mysql_error()); $result=mysql_query("CREATE DATABASE news default character set utf8"); if($result) { echo "Database create successful!"; }else { die('Could not connect:'.mysql_error()); } mysql_close($conn); ?> </span>
3.设计数据表,并创建数据表
强烈建议,在创建数据表之前,一定要先设计好数据表,并反复思量,是否有问题。
create_table.php
<span style="font-size:18px;"><!--执行该页面用于创建数据表--> <meta http-equiv="content-Type" content="text/html;charset=utf-8"> <title>创建数据表</title> <?php require_once 'config.php'; $conn = mysql_connect(HOST,USERNAME, PASSWORD); mysql_query("set names utf8"); if (!$conn){ die('Could not connect:'.mysql_error()); } mysql_select_db("news"); #分页表格-数据表 $sql = "CREATE TABLE content( id int primary key auto_increment, name varchar(30), age varchar(12) )"; #统计页面浏览次数-数据表 $sql2 ="CREATE TABLE count_number( id int primary key auto_increment, content_id int, number int, is_first varchar(10) )"; $sql3="insert into count_number(content_id,number,is_first) values(100,0,'true')"; #mysql_query($sql2); #mysql_query($sql3); /* 创建新闻主体表,字段解释 id 主键 news_id 新闻的标识,通过该id可以找到新闻的详情 image 新闻的图片 title 新闻的标题 */ $sql4 = "create table news_content( id int primary key auto_increment, news_id int , image varchar(150), title varchar(150), content varchar(150) )ENGINE=MyISAM DEFAULT CHARSET=utf8"; /* 插入新闻内容 */ $sql5 = "insert into news_content(news_id,image,title,content) values( 1001, 'http://img1.cache.netease.com/catchpic/9/9E/9E3EC2A48299CBD7AD007DFF54060EE9.jpg', '人民日报梳理习近平这一年:累并快乐着', 'http://news.163.com/14/1211/08/AD5URVP800014SEH.html')"; $sql6 = "insert into news_content(news_id,image,title,content) values( 1002, 'http://img1.cache.netease.com/catchpic/8/86/86394708E37A7B759771855337792EED.jpg', '香港警方今日将全面清障 \"占中\"者内部发生分歧', 'http://news.163.com/14/1211/00/AD53FEM900014JB6.html')"; $sql7 = "insert into news_content(news_id,image,title,content) values( 1003, 'http://img4.cache.netease.com/cnews/2014/12/10/2014121019041607f5e.jpg', '太原市委组织部长李志江被调查 曾\"失踪\"14天', 'http://news.163.com/14/1211/02/AD5BFC5700014AED.html')"; $sql8="insert into news_content(news_id,image,title,content) values( 1004, 'http://img1.cache.netease.com/catchpic/B/BB/BB09886F9B84329626E13115159CB164.jpg', '媒体揭秘中情局八大酷刑 花8000万美元请人设计', 'http://news.163.com/14/1211/02/AD5BFJJP00014AED.html')"; #mysql_query($sql4); #mysql_query($sql5); #mysql_query($sql6); #mysql_query($sql7); #mysql_query($sql8); /* 插入测试数据 */ for($x = 0; $x<100;$x++){ $sql ="Insert into content(name,age) values('zhangsan',($x+5))"; #mysql_query($sql); } mysql_close($conn); ?> </span>
4.设计各个辅助页面
home.php
打开http://localhost/news/index.php时,显示给用户的信息
<span style="font-size:18px;"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>content</title> <table border="1" width="100%" rules="none"> <tr><td><?php include 'content_table.php';?></td></tr> </table></span>
sidebar.php
左右侧边栏的广告信息
<span style="font-size:18px;"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>sidebar</title> <img src="/static/imghwm/default1.png" data-src="./pic/baner1.jpg" class="lazy" style="max-width:90%" alt="PHP之分页显示数据-资讯系统" ><br> <img src="/static/imghwm/default1.png" data-src="./pic/baner2.jpg" class="lazy" style="max-width:90%" alt="PHP之分页显示数据-资讯系统" ><br> <img src="/static/imghwm/default1.png" data-src="./pic/baner3.jpg" class="lazy" style="max-width:90%" alt="PHP之分页显示数据-资讯系统" ><br> <img src="/static/imghwm/default1.png" data-src="./pic/baner4.jpg" class="lazy" style="max-width:90%" alt="PHP之分页显示数据-资讯系统" ><br> <img src="/static/imghwm/default1.png" data-src="./pic/baner1.jpg" class="lazy" style="max-width:90%" alt="PHP之分页显示数据-资讯系统" ><br> <img src="/static/imghwm/default1.png" data-src="./pic/baner2.jpg" class="lazy" style="max-width:90%" alt="PHP之分页显示数据-资讯系统" ><br> <img src="/static/imghwm/default1.png" data-src="./pic/baner3.jpg" class="lazy" style="max-width:90%" alt="PHP之分页显示数据-资讯系统" ><br> <img src="/static/imghwm/default1.png" data-src="./pic/baner4.jpg" class="lazy" style="max-width:90%" alt="PHP之分页显示数据-资讯系统" ><br></span>
navigationbar.php
页面导航信息
<span style="font-size:18px;"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>navigatiobar</title> <center width="100%" height="150px"> <img src="/static/imghwm/default1.png" data-src="./pic/header1.jpg" class="lazy" style="max-width:90%" height="120px" alt="PHP之分页显示数据-资讯系统" ><br> <font size="5px"> <a href="http://news.163.com/" target="content">首页</a> <a href="content_news.php" target="content">新闻 </a> <a href="home.php" target="content">表格 </a> <!--下面的url--> <a href="http://tech.163.com/" target="content">科技 </a> <a href="http://ent.163.com/" target="content">娱乐 </a> <a href="http://sports.163.com/" target="content">体育 </a> <a href="http://news.163.com/" target="content">要闻 </a> <a href="http://money.163.com/" target="content">财经 </a> <a href="http://lady.163.com/" target="content">时尚 </a> <a href="http://book.163.com/" target="content">文化 </a> <a href="http://edu.163.com/" target="content">教育 </a> <a href="http://digi.163.com/" target="content">数码 </a> </font> </center> </span>
footer.php
页面底部信息
<span style="font-size:18px;"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>footer</title> <center> <hr> <font size="5px">页面底部信息</font> </center></span>
5.操作数据库的类
mysqlhelper.php
该类用于对数据库进行一些基本操作
<span style="font-size:18px;"> <meta http-equiv="content-type" content="text/html;charset=utf-8"> <?php require_once 'config.php'; class MySQLHelper{ private $table; /* 构造函数: 参数1:主机名 参数2:数据库用户名 参数3:数据库密码 参数4:要操作的数据库 参数5:操作数据库的字符编码 初始化对象的同时,进行连接数据库操作 */ function __construct($table){ $this->table=$table; $this->connect(); } function connect(){ $link=mysql_connect(HOST,USERNAME,PASSWORD) or die("连接数据库失败".mysql_error()); mysql_select_db($this->table,$link) or die ("没有该数据库:".$this->table); mysql_query("SET NAMES ".CHARSET); } //受影响的行数 function affected_rows(){ return mysql_affect_rows(); } //数据条数 function num_rows($query){ return $mysql_num_rows($query); } //插入的数据所在的id function insert_id(){ return mysql_insert_id(); } //处理返回的结果 function fetch_row($query){ return mysql_fetch_row($query); } //服务器版本 function version(){ return mysql_get_server_info(); } //增 function fn_insert($table,$name,$value){ //insert into[表名] values('','',......顺序排列的数据); mysql_query("insert into $table ($name) values ($value)"); } //删 function fn_del($table,$data){ //delete from [表名] where ([条件]); mysql_query("delete from $table where (id=$data)"); } //查询全部结果 function fn_search($table){ $res = mysql_query("select * from $table"); return $res; } //改 function fn_update($table,$name1,$value,$name2,$id){ //UPDATE [表名] SET [修改内容如name = 'Mary'] WHERE [条件]; mysql_query("update $table set $name1='$value' where $name2='$id'"); }}?></span>
6.设计分页数据主页面
content_table.php
分页显示数据,每一页的数据是一张表格。
<span style="font-size:18px;"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>page</title> <?php require_once 'mysqlhelper.php';?> <table align="center" border="1" width="500px" height="10px"> <tr bgcolor="silver" align="center"> <td width="30%">id</td> <td width="40%">name</td> <td width="30%">age</td> </tr> <tr> <?php $db = new MySQLHelper("news"); $res = $db->fn_search("content"); //var_dump($res); //声明一些变量 $page_size="10";//每一页的条数 $count=mysql_num_rows($res);//总的条数 $page_number=ceil($count/$page_size);//向上舍入,获得最大页数 //当前的页数 if(empty($_GET['page']) || $_GET['page'] <td align="left"><?php echo $row[0]?></td> <td align="center"><?php echo $row[1]?></td> <td align="center"><?php echo $row[2]?></td> </tr> <?php } ?> </table> <p></p> <table border="0" align="center" cellpadding="5px"> <tr> <?php $key=''; if($current_page ==1){ //当前页是首页 $key.='首页'; $key.=' 上一页'; }else{ $key.="<a href=\"".$_SERVER['PHP_SELF']."?page=1\">首页 "; $key.="<a href="%5C%22%22.%24_SERVER%5B'PHP_SELF'%5D.%22?page=%22.(%24current_page-1).%22%5C%22"> 上一页 </a>"; } /*for($x = $current_page ; $x $x "; }*/ //中间页码策略:中间页数左右偏移2个页码 for($x = 0 ; $x" ." ".($current_page+$x).""; }else{ $y =6+$x; $key.="<a href="%5C%22%22.%24_SERVER%5B'PHP_SELF'%5D.%22?page=%22.%24y.%22%5C%22">" ." ".$y."</a>"; } } if($current_page == $page_number){ //当前页是尾页 $key.=' 下一页'; $key.=' 尾页'; }else{ $key.="<a href="%5C%22%22.%24_SERVER%5B'PHP_SELF'%5D.%22?page=%22.(%24current_page+1).%22%5C%22"> 下一页 </a>"; $key.="<a href="%5C%22%22.%24_SERVER%5B'PHP_SELF'%5D.%22?page=%22.%24page_number.%22%5C%22">尾页</a>"; } ?> <td><?php echo $key;?></td> </tr> <tr> <td colspan="10">浏览次数: <?php //查询数据库的次数 $sql="SELECT * FROM count_number where content_id=100"; $res = mysql_query($sql); #echo mysql_num_rows($res); #var_dump($res); //获取数据库的浏览次数 while($row = mysql_fetch_array($res)){ $number = $row['number']; } //次数+1 $sql = "UPDATE count_number set number =".($number+1)." WHERE content_id=100"; mysql_query($sql); //显示次数 echo $number; ?> </td> <td> </td> </tr> </table> </span>
content_news.php
分页显示数据信息,每一页的数据是一个新闻列表,文字和图片都可以点击,并进入新闻详情页面。
<span style="font-size:18px;"><meta http-equiv="Content-Type" content="text/html;charset=utf-8"> <table border="1" rules="none" width="100%" cellpadding="5px" bgcolor="#F8F8F8"> <tr> <?php require_once 'mysqlhelper.php'; header("Content-type:text/html;charset=utf-8"); $helper = new MySQLHelper("news"); $res = $helper->fn_search("news_content"); //声明一些变量 $total_count = mysql_num_rows($res); //总的数据行数 $page_count=6; //每页显示的数据条数 $page_number=ceil($total_count / $page_count);//总的页数 //获取当前的页码 if(empty($_GET['page'])){ $current_page=1; }else{ $current_page=$_GET['page']; } //当前页的第一条数据应该是 $offset=$page_count*($current_page-1); $sql="select * from news_content limit $offset,$page_count"; $res = mysql_query($sql); //查询当前页的数据 while($row = mysql_fetch_array($res)){ #echo $row['id']."<br>"; #echo $row['image']."<br>"; #echo $row['title']."<br>";?> <td width="100px"> <a href="content.php?news_id=<?php%20echo%20%24row%5B'news_id'%5D;?>"> <img src="/static/imghwm/default1.png" data-src=".$row['image']." class="lazy" style="max-width:90%" height="80px" alt="PHP之分页显示数据-资讯系统" > </a> </td> <td> <a href="content.php?news_id=<?php%20echo%20%24row%5B'news_id'%5D;?>"> <?php echo $row['title'];?></a> </td> </tr> <?php }?><?php $key=""; $key.="总页数:".$current_page."/".$page_number." "; if($current_page == 1){ $key.="首页 "; $key.="上一页 "; }else{ $key.="<a href=\"".$_SERVER['PHP_SELF']."?page=1"."\">首页 "; $key.="<a href="%5C%22%22.%24_SERVER%5B'PHP_SELF'%5D.%22?page=%22.(%24current_page-1).%22%5C%22">上一页 </a>"; } if($current_page == $page_number){ $key.="下一页 "; $key.="尾页 "; }else{ $key.="<a href="%5C%22%22.%24_SERVER%5B'PHP_SELF'%5D.%22?page=%22.(%24current_page+1).%22%5C%22">下一页 </a>"; $key.="<a href="%5C%22%22.%24_SERVER%5B'PHP_SELF'%5D.%22?page=%22.%24page_number.%22%5C%22">尾页 </a>"; } $key.=" 总的数据条数:".$total_count;?></table> <p></p> <?php echo $key;?> </span>
7.设计单条数据详情页面
content.php
<span style="font-size:18px;"><meta http-equiv="content-type" content="text/html;charset=utf-8"> <script> function jump(url){ //alert(url); window.location.href=url;//页面重定向 }</script> <?php require_once 'mysqlhelper.php'; $helper = new MySQLHelper("news"); //print_r($_GET); $news_id =$_GET['news_id']; //获取新闻的id $sql = "select * from count_number where content_id=".$news_id; $res = mysql_query($sql); //判断是否是第一次进来 if(mysql_num_rows($res) == 0){ $sql = "insert into count_number(content_id,number,is_first) values($news_id,1,'true')"; #echo "第一次进来"; mysql_query($sql); $number=1; }else{ $sql="select number from count_number where content_id=".$news_id; #echo "不是第一次进来"; $res = mysql_query($sql); $row = mysql_fetch_array($res); $number = $row['number']+1; $sql="update count_number set number=".$number." where content_id=".$news_id; mysql_query($sql); } $sql = "select * from news_content where news_id=".$news_id.""; $res = mysql_query($sql); $row = mysql_fetch_array($res); echo "<p>"; echo "<center> <font size="5px" color="red">".$row['title']."</font><p></p> </center>"; echo "<br>"; echo "<center><img src="/static/imghwm/default1.png" data-src=".$row['image']." class="lazy" alt="PHP之分页显示数据-资讯系统" ></center>"; //显示浏览次数 echo "页面浏览次数:".$number." "; //点击超链接,跳转到详情页面 $st =$row['content']; echo "<a href="'javascript:void(0)'" onclick="jump('$st')">跳转到详情页面</a>"; ?></span>三、界面效果

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

Key players in HTTP cache headers include Cache-Control, ETag, and Last-Modified. 1.Cache-Control is used to control caching policies. Example: Cache-Control:max-age=3600,public. 2. ETag verifies resource changes through unique identifiers, example: ETag: "686897696a7c876b7e". 3.Last-Modified indicates the resource's last modification time, example: Last-Modified:Wed,21Oct201507:28:00GMT.

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

Notepad++7.3.1
Easy-to-use and free code editor

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version
Useful JavaScript development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),