PHP之分页显示数据-新闻系统

来源:互联网 发布:刻录dvd软件 编辑:程序博客网 时间:2024/05/17 23:27

一、项目设计

1.项目要求

2.主页面使用frame框架进行设计,内容如下:

   index.php

<html><frameset rows="80%,20%" frameborder="no" border="0" cols="900px"><frameset cols="10%,80%,10%" ><frame src="sidebar.php" name="sidebar" noresize="noresize" scrolling="no"><frameset rows="20%,80%"><frame src="navigationbar.php" name="navigation" noresize="noresize" scrolling="no"><frame src="home.php" name="content" noresize="noresize" scrolling="yes"></frameset><frame src="sidebar.php" name="sidebar" noresize="noresize" scrolling="no" ></frameset><frame src="footer.php" name="footer"></frameset></html>
二、准备工作

1.创建项目文件夹,定义配信息

   文件夹名称:news

   配置文件名:config.php

<?phpdefine("HOST","localhost");define("USERNAME","root");define("PASSWORD","");define("CHARSET","utf8");?>

2..创建项目数据库

   create_db.php

<!--执行该页面用于创建一个数据库--><html><head><meta http-equiv="content-Type" content="text/html;charset=utf-8"/><title>创建数据库</title><head><body><?phprequire_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);?></body></html>

3.设计数据表,并创建数据表

     强烈建议,在创建数据表之前,一定要先设计好数据表,并反复思量,是否有问题。

     create_table.php

<!--执行该页面用于创建数据表--><html><head><meta http-equiv="content-Type" content="text/html;charset=utf-8"/><title>创建数据表</title></head><body><?phprequire_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_idint,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);?></body></html>

4.设计各个辅助页面

    home.php

   打开http://localhost/news/index.php时,显示给用户的信息

<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>content</title></head><body leftmargin=0 topmargin=0><table  border="1" width="100%" rules="none" > <tr><td><?php include 'content_table.php';?></td></tr></table></body></html>

   sidebar.php

   左右侧边栏的广告信息

<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>sidebar</title></head><body  width="150px" leftmargin=0 topmargin=0><img src="./pic/baner1.jpg" width="150px"/><br/><img src="./pic/baner2.jpg" width="150px"/><br/><img src="./pic/baner3.jpg" width="150px"/><br/><img src="./pic/baner4.jpg" width="150px"/><br/><img src="./pic/baner1.jpg" width="150px"/><br/><img src="./pic/baner2.jpg" width="150px"/><br/><img src="./pic/baner3.jpg" width="150px"/><br/><img src="./pic/baner4.jpg" width="150px"/><br/></body></html>

    navigationbar.php

    页面导航信息

<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>navigatiobar</title></head><body leftmargin=0 topmargin="10px" ><center width="100%" height="150px"><img src="./pic/header1.jpg" width="100%" height="120px"/><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></body></html>

    footer.php

    页面底部信息

<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>footer</title></head><body><center> <hr/><font size="5px">页面底部信息</font></center></body></html>


5.操作数据库的类

   mysqlhelper.php

   该类用于对数据库进行一些基本操作

<html><head><meta http-equiv="content-type" content="text/html;charset=utf-8"/></head><?phprequire_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'"); }}?></html>

6.设计分页数据主页面

    content_table.php

    分页显示数据,每一页的数据是一张表格。

<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>page</title></head><body ><?php require_once 'mysqlhelper.php';?><tablealign="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'] <0){$current_page = 1;}else{$current_page = $_GET['page'];}$offset = $page_size*($current_page-1);//当前页的第一条数据$sql="SELECT * FROM content limit $offset,$page_size";//查询当前页数据,10条$res = mysql_query($sql);while($row = mysql_fetch_row($res)){//var_dump($row);?><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\">首页  </a>";$key.="<a href=\"".$_SERVER['PHP_SELF']."?page=".($current_page-1)."\">    上一页  </a>";}/*for($x = $current_page ; $x < $current_page+8,$x<$page_number; $x++){//$key.=$x;$key.="<a href=\"".$_SERVER['PHP_SELF']."?page=".$x."\">  $x </a>";}*///中间页码策略:中间页数左右偏移2个页码for($x = 0 ; $x<5;$x++){//中间只显示5个页码if($current_page <=6){$key.="<a href=\"".$_SERVER['PHP_SELF']."?page=".($current_page+$x)."\">"." ".($current_page+$x)."</a>";}else{$y =6+$x;$key.="<a href=\"".$_SERVER['PHP_SELF']."?page=".$y."\">"." ".$y."</a>";}}if($current_page == $page_number){//当前页是尾页$key.='  下一页';$key.='  尾页';}else{$key.="<a href=\"".$_SERVER['PHP_SELF']."?page=".($current_page+1)."\">     下一页  </a>";$key.="<a href=\"".$_SERVER['PHP_SELF']."?page=".$page_number."\">尾页</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></tr></table></body></html>

    content_news.php

    分页显示数据信息,每一页的数据是一个新闻列表,文字和图片都可以点击,并进入新闻详情页面。

<html><head><meta http-equiv="Content-Type" content="text/html;charset=utf-8"/></head><body margin=0><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 echo $row['news_id'];?>"><img src="<?php echo $row['image'];?>"  width="80px" height="80px"/></a></td><td><a href="content.php?news_id=<?php echo $row['news_id'];?>"><?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"."\">首页 </a>";$key.="<a href=\"".$_SERVER['PHP_SELF']."?page=".($current_page-1)."\">上一页 </a>";}if($current_page == $page_number){$key.="下一页 ";$key.="尾页 ";}else{$key.="<a href=\"".$_SERVER['PHP_SELF']."?page=".($current_page+1)."\">下一页 </a>";$key.="<a href=\"".$_SERVER['PHP_SELF']."?page=".$page_number."\">尾页 </a>";}$key.="  总的数据条数:".$total_count;?></table><p></p><?php echo $key;?></body></html>


7.设计单条数据详情页面

  content.php

<html><head><meta http-equiv="content-type" content="text/html;charset=utf-8"/><script>function jump(url){//alert(url);window.location.href=url;//页面重定向}</script></head><body><?phprequire_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></p>";echo "<center><font size=5px color=red>".$row['title']."</font><p></p></center>";echo "<br/>";echo "<center><img src=".$row['image']." /></center>";//显示浏览次数echo "页面浏览次数:".$number."     ";//点击超链接,跳转到详情页面$st =$row['content'];echo "<a href='javascript:void(0)' onclick=jump('$st')>跳转到详情页面</a>";?></body></html>
三、界面效果








1 0
原创粉丝点击