PHP之分页显示数据-资讯系统
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/imghw/default1.png" data-src="./pic/baner1.jpg" class="lazy" style="max-width:90%" alt="PHP之分页显示数据-资讯系统" ><br> <img src="/static/imghw/default1.png" data-src="./pic/baner2.jpg" class="lazy" style="max-width:90%" alt="PHP之分页显示数据-资讯系统" ><br> <img src="/static/imghw/default1.png" data-src="./pic/baner3.jpg" class="lazy" style="max-width:90%" alt="PHP之分页显示数据-资讯系统" ><br> <img src="/static/imghw/default1.png" data-src="./pic/baner4.jpg" class="lazy" style="max-width:90%" alt="PHP之分页显示数据-资讯系统" ><br> <img src="/static/imghw/default1.png" data-src="./pic/baner1.jpg" class="lazy" style="max-width:90%" alt="PHP之分页显示数据-资讯系统" ><br> <img src="/static/imghw/default1.png" data-src="./pic/baner2.jpg" class="lazy" style="max-width:90%" alt="PHP之分页显示数据-资讯系统" ><br> <img src="/static/imghw/default1.png" data-src="./pic/baner3.jpg" class="lazy" style="max-width:90%" alt="PHP之分页显示数据-资讯系统" ><br> <img src="/static/imghw/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/imghw/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/imghw/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/imghw/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>

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

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

Watch4pro and gt each have different features and applicable scenarios. If you focus on comprehensive functions, high performance and stylish appearance, and are willing to bear a higher price, then Watch 4 Pro may be more suitable. If you don’t have high functional requirements and pay more attention to battery life and reasonable price, then the GT series may be more suitable. The final choice should be decided based on personal needs, budget and preferences. It is recommended to carefully consider your own needs before purchasing and refer to the reviews and comparisons of various products to make a more informed choice.

How to Optimize iPad Battery Life with iPadOS 17.4 Extending battery life is key to the mobile device experience, and the iPad is a good example. If you feel like your iPad's battery is draining too quickly, don't worry, there are a number of tricks and tweaks in iPadOS 17.4 that can significantly extend the run time of your device. The goal of this in-depth guide is not just to provide information, but to change the way you use your iPad, enhance your overall battery management, and ensure you can rely on your device for longer without having to charge it. By adopting the practices outlined here, you take a step toward more efficient and mindful use of technology that is tailored to your individual needs and usage patterns. Identify major energy consumers

Everyone is looking forward to today's Windows 1123H2 release. In fact, Microsoft has just launched updates to the release preview, which is the closest channel before the official release stage. Known as Build 22631, Microsoft said they are rolling out the new rebranded chat app, phone link, and play together widgets that have been tested on other internal channels over the past few months. "This new update will have the same servicing branch and codebase as Windows 11 version 22H2 and will be cumulative with all newly announced features, including Copilot in Windows (preview)," Microsoft promises. Redmond officials further

请问如何修改url某一参数的参数值呢?是要拆开了再拼回去吗?那么请问如何修改url某一参数的参数值呢?是要拆开了再拼回去吗?http://127.0.0.1/myo/newuser.php?mod=search&type=fastone比如现在我要修改mod=new要怎么做呢?------解决方案--------------------发送了请求
