php 实现 一页显示10条记录

    

现在已经在实习了,抱着学习的态度,当初选择了一点都没有概念的php和mysql管理。一切从零开始吧,幸亏办公室里面有大牛在,所以又问题就直接可以请教他。

     大牛布置任务说,做一个新闻管理平台,哎……才学了10多天,而且只看看书的人,看了html,xhtml,css,xml,javascrip,jquery,php,mysql,你们应该就知道我是什么基础了,好不容易写了点东西,然后大牛要有提高的,显示的时候,不能再一个网页里面打印全部的东西,要每页打印一定的条数,所以……

代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--
Design by Free CSS Templates
http://www.freecsstemplates.org
Released for free under a Creative Commons Attribution 2.5 License

Name       : Natures Charm
Description: A two-column, fixed-width design.
Version    : 1.0
Released   : 20080125

-->

<html xmlns="http://www.w3.org/1999/xhtml"><!-- 使用命名空间 -->
<head>
<meta http-equiv="content-type" content="text/html; charset=gbk" />
<title>新闻管理平台</title>
<meta name="keywords" content="" />
<meta name="description" content="" />
<link href="default.css" rel="stylesheet" type="text/css" media="screen" />
<script type="text/javascript" src="../jquery/jquery.js" tppabs="http://www.w3school.com.cn/jquery/jquery.js"></script>
<script type="text/javascript"> 
	$(document).ready(function(){	//这是为了防止文档在完全加载(就绪)之前运行 jQuery 代码
	$(".flip").click(function(){
	    $(".panel").slideToggle("slow");
	  });
	});
</script>
</head>
<body>
<!-- start header -->
<div id="header">
	<div id="logo">
		<h1>新闻管理平台 </h1>			 <!-- 锚 URL - 指向页面中的锚(href="#top") -->
	</div>
	<div id="menu">
		<ul>
			<li ><a href="./index.html" accesskey="1" title="点击回到首页">首页</a></li>
			<li><a href="./create_news.html" accesskey="2" title="">创建新闻</a></li>
			<li><a href="./search_news.html" accesskey="3" title="">查找新闻</a></li>
			<li><a href="#" accesskey="4" title="">编辑新闻</a></li>
			<li class="active"><a href="./browse_news.php" accesskey="5" title="">浏览新闻</a></li>
		</ul>
	</div>
</div>

<div id=search_news_3>
 <?php
  // create short variable names
  
 function page_select($num,$page_num)
 {
  echo '    <select οnchange="location.href=\'browse_news.php?page=\'+this.value" name="page_select" size=1 style="font-size:12px">';
  for($i=1;$i<=$num;$i++)
  {
  	if($i==$page_num)
  		echo "<option value='".$i."' selected>$i</option>";
  	else 
  		echo "<option value='".$i."'>$i</option>";
  }
  echo '</select>';
 }
  $page_num=$_GET['page'];
  if(!$page_num)
  {
  	$page_num = 1;
  }
  
  @ $db = new mysqli('localhost', 'news_author01', '123456',  'manage_news');
  $db->set_charset('GBK');//关键呀

  if (mysqli_connect_errno()) 
  {
     echo 'Error: Could not connect to database.  Please try again later.';
  }
  $query = "select count(*) from manage_news";
  $result = $db->query($query);
  $row = $result->fetch_array();
  $num_results = $row[0];
  $page_total=(integer)(($num_results+9)/10);
  
  echo '<p>共'.$page_num.'页/每页10条   共'.$num_results.'个条新闻。</p>';
   if($page_total>$page_num )
   {
	  	if($page_num>1)
	  	{
		  	echo '<a href="./browse_news.php?page='.($page_num-1).'">上一页</a>';
		  	echo " "."$page_num".'/'."$page_total"." ";
		  	echo '<a href="./browse_news.php?page='.($page_num+1).'">下一页</a>';
		  	if (($num_results-($page_num-1)*10)<10)
		  	{
		  		$print_line = $num_results-$page_num*10;
		  	}
		  	else 
		  	{
		  		$print_line = 10;
		  	}
	  	}
	  	else 
	  	{
	  		echo '上一页';
		  	echo " "."$page_num".'/'."$page_total"." ";
		  	echo '<a href="./browse_news.php?page='.($page_num+1).'">下一页</a>';
	  		$print_line = 10;
	  	}
   }
   else
   {
   		if($page_total==1)
  		{
	  		echo '上一页';
		  	echo " "."$page_num".'/'."$page_total"." ";
		  	echo '下一页';
		    $print_line = $num_results;
  		}
  		else
  		{
  			echo '<a href="./browse_news.php?page='.($page_num-1).'">上一页</a>';
	  		echo " "."$page_num".'/'."$page_total"." ";
	  		echo '下一页';
	  	  
	  		$print_line = $num_results-($page_num-1)*10;
  		}
   }
   page_select($page_total,$page_num);
 
  echo '<div id=search_news_4>';
  echo '<table width="100%">';
 
  $query = "select * from manage_news limit ".(($page_num-1)*10).",10";
  $result = $db->query($query);
  
  for ($i=($page_num-1)*10; $i <$print_line+($page_num-1)*10; $i++)
  {
    $row = $result->fetch_assoc();//将每一行的相关
	 $temp = $row['id'];
	 echo '<tr width="100%" valign="baseline"><td align="left" width="75%">';
     echo "<font size='5'  color='aqua'>".'<p><strong>'.($i+1).'. ';
     echo "<a href='./show_detail.php?id=$temp'>";
     echo htmlspecialchars(stripslashes($row['title']));
     //$_REQUEST['']
     echo "</a>";
     echo "<font size='4'  color='DarkGray'>".'      ID:';
     echo stripslashes($row['id']);
     echo '<br />作者:';
     echo stripslashes($row['author']);
     echo '<font size=2>';
     echo '<br />发布时间:';
     echo stripslashes($row['time']).'   最后修改时间:'.stripslashes($row['last_time']).'</font></font></font>';
     echo "</td>";
     echo '<td width="25%" align="right" valign="baseline">';//与基线对齐
     echo "<a href='./delete_news.php?id=".stripslashes($row['id'])."'>[删除]</a> ";
	 echo "<a href='./edit_news.php?id=".stripslashes($row['id'])."'>[编辑]</a>";
	 echo "</td></tr>";
  }
  echo '</table>';
  
  echo '</div>';
  $result->free();
  $db->close();

 ?>
</div>

<!--  <div class="panel">
	<p>Because time is valuable, we deliver quick and easy learning.</p>
	<p>At W3School, you can study everything you need to learn, in an accessible and handy format.</p>
</div>
	<p class="flip">详情 >></p> -->
	
<div style="clear: both; height: 30px"> </div>
<!-- end page -->
<div id="footer">
	<p>©2007 All Rights Reserved.  •  Designed by <a href="http://www.freecsstemplates.org/">Free CSS Templates</a></p>
</div>
</body>
</html>
































  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值