|
|
|
|
备忘:建立一个简单的数据表,示范如何分页,方法比较简单,大家给给意见 1
create table news_categories2 (3 news_categories_id tinyint(3) UNSIGNED not null AUTO_INCREMENT,4 categories varchar(20) not null,5 PRIMARY KEY (news_categories_id)6 );分页代码: 1 <?php2 ![]() 3 /**4 * @author Karl Tam5 * @copyright 20076 */7 ![]() 8 $Page_title='View The Current Categories';9 ![]() 10 echo '<h1>新闻分类</h1>';11 ![]() 12 require_once('connection/conn.php');13 ![]() 14 //每页的记录数15 $display=10;16 ![]() 17 //限定页数18 if (isset($_GET['np'])){//已经限定19 20 $num_pages=$_GET['np'];21 }else{22 $query="select count(*) from news_categories order by news_categories_id DESC";23 $result=mysql_query($query);24 $row=mysql_fetch_array($result,MYSQL_NUM);25 $num_records=$row[0];26 ![]() 27 //计算页数28 if ($num_records>$display){29 $num_pages=ceil($num_records/$display);//Ceil返回$num_records/$display返回的下一个整数,这里的作用是取得最大页数30 }else{31 $num_pages=1;32 }33 }34 ![]() 35 //下面定义数据库用哪里开始返回记录36 if(isset($_GET['s'])){37 $start=$_GET['s'];38 }else{39 $start=0;40 }41 ![]() 42 //查询43 $query="select categories,news_categories_id from news_categories order by news_categories_id DESC Limit $start,$display";44 $result=@mysql_query($query);45 ![]() 46 //表头47 echo '<table>48 <tr>49 <td>edit</td>50 <td>delete</td>51 <td>Categories ID</td>52 <td>Categories Name</td>53 </tr>';54 ![]() 55 //数据56 while($row=mysql_fetch_array($result,MYSQL_ASSOC)){57 echo '<tr>58 <td><a href="edit_news_categories.php?id='.$row['news_categories_id'].'">编辑</a></td>59 <td><a href="delete_news_categories.php?id='.$row['news_categories_id'].'">删除</a></td>60 <td>'.$row['news_categories_id'].'</td>61 <td>'.$row['categories'].'</td></tr>';//创建编辑页,删除页链接62 }63 echo '</table>';64 ![]() 65 mysql_free_result($result);//释放资源66 mysql_close();//关闭数据库67 ![]() 68 //建立页码数导航链接69 ![]() 70 ///*71 if ($num_pages>1){72 echo '<br />';73 $current_page=($start/$display)+1;74 75 //判断76 //如果它不是第一页,创建“前一页”按钮77 if ($current_page!=1){78 echo '<a href="view_news_categories.php?s='.($start-$display).'&np='.$num_pages.'">前一页</a>';79 }80 81 //创建所有页码82 for($i=1;$i<=$num_pages;$i++){83 if ($i !=$current_page){84 echo '<a href="view_news_categories.php?s='.(($display*($i-1))).'&np='.$num_pages.'">'.$i.' </a>';85 }else{86 echo $i.' ';87 }88 }89 90 //判断91 //如果它不是最后一页,创建“下一页”按钮92 if ($current_page!=$num_pages){93 echo '<a href="view_news_categories.php?s='.($start+$display).'&np='.$num_pages.'">下一页</a>';94 }95 }96 //*/97 ![]() 98 ?> |


