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>

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제











많은 사용자들이 스마트 시계를 선택할 때 Huawei 브랜드를 선택하게 됩니다. 그 중 Huawei GT3pro와 GT4가 가장 인기 있는 선택입니다. 두 제품의 차이점을 궁금해하는 사용자가 많습니다. Huawei GT3pro와 GT4의 차이점은 무엇입니까? 1. 외관 GT4: 46mm와 41mm, 재질은 유리 거울 + 스테인레스 스틸 본체 + 고해상도 섬유 후면 쉘입니다. GT3pro: 46.6mm 및 42.9mm, 재질은 사파이어 유리 + 티타늄 본체/세라믹 본체 + 세라믹 백 쉘입니다. 2. 건강한 GT4: 최신 Huawei Truseen5.5+ 알고리즘을 사용하면 결과가 더 정확해집니다. GT3pro: ECG 심전도, 혈관 및 안전성 추가

Windows 11에서 캡처 도구가 작동하지 않는 이유 문제의 근본 원인을 이해하면 올바른 솔루션을 찾는 데 도움이 될 수 있습니다. 캡처 도구가 제대로 작동하지 않는 주요 이유는 다음과 같습니다. 초점 도우미가 켜져 있습니다. 이렇게 하면 캡처 도구가 열리지 않습니다. 손상된 응용 프로그램: 캡처 도구가 실행 시 충돌하는 경우 응용 프로그램이 손상되었을 수 있습니다. 오래된 그래픽 드라이버: 호환되지 않는 드라이버가 캡처 도구를 방해할 수 있습니다. 다른 응용 프로그램의 간섭: 실행 중인 다른 응용 프로그램이 캡처 도구와 충돌할 수 있습니다. 인증서가 만료되었습니다. 업그레이드 프로세스 중 오류로 인해 이 문제가 발생할 수 있습니다. 이 문제는 대부분의 사용자에게 적합하며 특별한 기술 지식이 필요하지 않습니다. 1. Windows 및 Microsoft Store 앱 업데이트

1부: 초기 문제 해결 단계 Apple 시스템 상태 확인: 복잡한 솔루션을 살펴보기 전에 기본 사항부터 시작해 보겠습니다. 문제는 귀하의 기기에 있는 것이 아닐 수도 있습니다. Apple 서버가 다운되었을 수도 있습니다. Apple의 시스템 상태 페이지를 방문하여 AppStore가 제대로 작동하는지 확인하세요. 문제가 있는 경우 Apple이 문제를 해결하기를 기다리는 것뿐입니다. 인터넷 연결 확인: "AppStore에 연결할 수 없음" 문제는 때때로 연결 불량으로 인해 발생할 수 있으므로 인터넷 연결이 안정적인지 확인하십시오. Wi-Fi와 모바일 데이터 간을 전환하거나 네트워크 설정을 재설정해 보세요(일반 > 재설정 > 네트워크 설정 재설정 > 설정). iOS 버전을 업데이트하세요.

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

Watch4pro와 gt는 각각 서로 다른 기능과 적용 가능한 시나리오를 가지고 있습니다. 포괄적인 기능, 고성능, 세련된 외관에 중점을 두고 더 높은 가격을 감수할 의향이 있다면 Watch 4 Pro가 더 적합할 수 있습니다. 높은 기능 요구 사항이 없고 배터리 수명과 합리적인 가격에 더 많은 관심을 기울이는 경우 GT 시리즈가 더 적합할 수 있습니다. 최종 선택은 개인의 필요와 예산, 선호도에 따라 결정되어야 합니다. 자신의 필요를 잘 고려한 후 구매하고, 다양한 제품에 대한 리뷰와 비교를 참고하여 보다 현명한 선택을 하는 것이 좋습니다.

iPadOS 17.4로 iPad 배터리 수명을 최적화하는 방법 배터리 수명 연장은 모바일 장치 경험의 핵심이며 iPad가 좋은 예입니다. iPad의 배터리가 너무 빨리 소모된다고 생각되더라도 걱정하지 마세요. iPadOS 17.4에는 기기의 실행 시간을 크게 연장할 수 있는 여러 가지 요령과 조정 사항이 있습니다. 이 심층 가이드의 목표는 단순히 정보를 제공하는 것이 아니라 iPad 사용 방식을 바꾸고 전반적인 배터리 관리를 향상하며 기기를 충전하지 않고도 기기를 더 오랫동안 사용할 수 있도록 하는 것입니다. 여기에 설명된 사례를 채택함으로써 귀하는 개인의 요구 사항과 사용 패턴에 맞는 기술을 보다 효율적이고 주의 깊게 사용하는 방향으로 나아갈 수 있습니다. 주요 에너지 소비자 식별

모두가 오늘의 Windows 1123H2 릴리스를 기대하고 있습니다. 실제로 마이크로소프트는 공식 출시 단계 전 가장 가까운 채널인 릴리즈 프리뷰에 대한 업데이트를 막 출시했다. 빌드 22631로 알려진 마이크로소프트는 지난 몇 달 동안 다른 내부 채널에서 테스트를 거친 새로운 브랜드의 채팅 앱, 전화 링크, 플레이 투게더 위젯을 출시할 예정이라고 밝혔습니다. "이 새로운 업데이트는 Windows 11 버전 22H2와 동일한 서비스 분기 및 코드 기반을 가지며 Windows의 Copilot(미리 보기)을 포함하여 새로 발표된 모든 기능과 함께 누적될 것입니다."라고 Microsoft는 약속합니다. 레드몬드 관계자 추가

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