Heim  >  Artikel  >  Backend-Entwicklung  >  PHP之分页显示数据-资讯系统

PHP之分页显示数据-资讯系统

WBOY
WBOYOriginal
2016-06-13 12:11:301023Durchsuche

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 &#39;config.php&#39;;		$conn=mysql_connect(HOST,USERNAME,PASSWORD);		if(!$conn)			die(&#39;Coucld not connect:&#39;.mysql_error());		$result=mysql_query("CREATE DATABASE news default character set utf8");		if($result)		{			echo "Database create successful!";		}else		{			die(&#39;Could not connect:&#39;.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 &#39;config.php&#39;;			$conn = mysql_connect(HOST,USERNAME, PASSWORD);			mysql_query("set names utf8");			if (!$conn){				die(&#39;Could not connect:&#39;.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,&#39;true&#39;)";			#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,				&#39;http://img1.cache.netease.com/catchpic/9/9E/9E3EC2A48299CBD7AD007DFF54060EE9.jpg&#39;,				&#39;人民日报梳理习近平这一年:累并快乐着&#39;,				&#39;http://news.163.com/14/1211/08/AD5URVP800014SEH.html&#39;)";			$sql6 = "insert into news_content(news_id,image,title,content) values(				1002,				&#39;http://img1.cache.netease.com/catchpic/8/86/86394708E37A7B759771855337792EED.jpg&#39;,				&#39;香港警方今日将全面清障 \"占中\"者内部发生分歧&#39;,				&#39;http://news.163.com/14/1211/00/AD53FEM900014JB6.html&#39;)";			$sql7 = "insert into news_content(news_id,image,title,content) values(				1003,				&#39;http://img4.cache.netease.com/cnews/2014/12/10/2014121019041607f5e.jpg&#39;,				&#39;太原市委组织部长李志江被调查 曾\"失踪\"14天&#39;,				&#39;http://news.163.com/14/1211/02/AD5BFC5700014AED.html&#39;)";			$sql8="insert into news_content(news_id,image,title,content) values(				1004,				&#39;http://img1.cache.netease.com/catchpic/B/BB/BB09886F9B84329626E13115159CB164.jpg&#39;,				&#39;媒体揭秘中情局八大酷刑 花8000万美元请人设计&#39;,				&#39;http://news.163.com/14/1211/02/AD5BFJJP00014AED.html&#39;)";			#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(&#39;zhangsan&#39;,($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 &#39;content_table.php&#39;;?></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="./pic/baner1.jpg"    style="max-width:90%" alt="PHP之分页显示数据-资讯系统" ><br>	<img  src="./pic/baner2.jpg"    style="max-width:90%" alt="PHP之分页显示数据-资讯系统" ><br>	<img  src="./pic/baner3.jpg"    style="max-width:90%" alt="PHP之分页显示数据-资讯系统" ><br>	<img  src="./pic/baner4.jpg"    style="max-width:90%" alt="PHP之分页显示数据-资讯系统" ><br>	<img  src="./pic/baner1.jpg"    style="max-width:90%" alt="PHP之分页显示数据-资讯系统" ><br>	<img  src="./pic/baner2.jpg"    style="max-width:90%" alt="PHP之分页显示数据-资讯系统" ><br>	<img  src="./pic/baner3.jpg"    style="max-width:90%" alt="PHP之分页显示数据-资讯系统" ><br>	<img  src="./pic/baner4.jpg"    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="./pic/header1.jpg"    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 &#39;config.php&#39;;	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 &#39;mysqlhelper.php&#39;;?>		<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=&#39;&#39;;					if($current_page ==1){	//当前页是首页						$key.=&#39;首页&#39;;							$key.=&#39;  上一页&#39;;					}else{						$key.="<a href=\"".$_SERVER[&#39;PHP_SELF&#39;]."?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[&#39;number&#39;];						}						//次数+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 &#39;mysqlhelper.php&#39;;	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="<?php%20echo%20%24row%5B'image'%5D;?>"    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[&#39;title&#39;];?></a>	</td>	</tr>
<?php }?><?php $key="";		$key.="总页数:".$current_page."/".$page_number."  ";	if($current_page == 1){		$key.="首页 ";		$key.="上一页 ";	}else{		$key.="<a href=\"".$_SERVER[&#39;PHP_SELF&#39;]."?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 &#39;mysqlhelper.php&#39;;		$helper = new MySQLHelper("news");		//print_r($_GET);					$news_id =$_GET[&#39;news_id&#39;];	//获取新闻的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,&#39;true&#39;)";			#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[&#39;number&#39;]+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=".%24row%5B'image'%5D." alt="PHP之分页显示数据-资讯系统" ></center>";				//显示浏览次数		echo "页面浏览次数:".$number."     ";		//点击超链接,跳转到详情页面		$st =$row['content'];		echo "<a href="'javascript:void(0)'" onclick="jump('$st')">跳转到详情页面</a>";	?></span>
三、界面效果


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn