php5 mysql分頁執行個體代碼_php技巧

來源:互聯網
上載者:User

複製代碼 代碼如下:

<?php    
  //串連資料庫    
  $db=mysql_connect("localhost","root","");    
  mysql_select_db("hy",$db);    
  //設定每一頁顯示的記錄數    
  $pagesize=25;    
  //取得記錄總數,計算總頁數用    
  $res=mysql_query("select   count(*)   from   main   "   ,$db);    
  $myrow   =   mysql_fetch_array($res);    
  $numrows=$myrow[0];    
  //計算總頁數    
  $pages=intval($numrows/$pagesize);    
  if   ($numrows%$pagesize)    
  $pages++;    
  //判斷頁數設定與否,如無則定義為首頁    
  if   (!isset($page))    
  $page=1;    
  //判斷轉到頁數    
  if   (isset($ys))    
  if   ($ys>$pages)    
  $page=$pages;    
  else    
  $page=$ys;    
  //計算記錄位移量    
  $offset=$pagesize*($page-1);    
  //取記錄    
  $res=mysql_query("select   id,title   from   main   order   by   id   desc   limit   $offset,$pagesize"   ,$db);    
  //迴圈顯示記錄    
  if   ($myrow   =   mysql_fetch_array($res))    
  {    
  $i=0;    
  ?>      
  <table   width="101%"   border="0"   cellspacing="0"   cellpadding="0">    
  <tr>    
  <td   width="5%"   bgcolor="#E1E9FB"></td>    
  <td   width="95%"   bgcolor="#E1E9FB"><font   color="#FF6666"   size="2">內容</font></td>    
  </tr>    
  <?php    
  do   {    
  $i++;    
  ?>    
  <tr>    
  <td   width="5%"   bgcolor="#E6F2FF"><?php   echo   $i;?></td>    
  <td   width="95%"   bgcolor="#E6F2FF"><font   size="2">    
  <a   href="javascript:popwin('view.php?id=<?php   echo   $myrow[0];?>')"   ><?php   echo   $myrow[1];?></a></font></td>    
  </tr>    
  <?php    
  }    
  while   ($myrow   =   mysql_fetch_array($res));    
  echo   "</table>"   ;    
  }    
  //顯示總頁數    
  echo   "<div   align='center'>共有".$pages."頁(".$page."/".$pages.")<br>";    
  //顯示分頁數    
  for   ($i=1;$i<$page;$i++)    
  echo   "<a   href='fenye.php?page=".$i."'>第".$i   ."頁</a>     ";    
  echo   "第".$page."頁     ";    
  for   ($i=$page+1;$i<=$pages;$i++)    
  echo   "<a   href='fenye.php?page=".$i."'>第".$i   ."頁</a>     ";    

  echo   "<br>";    
  //顯示轉到頁數    
  echo   "<form   action='fenye.php'   method='post'>   ";    
  //計算首頁、上一頁、下一頁、尾頁的頁數值    
  $first=1;    
  $prev=$page-1;    
  $next=$page+1;    
  $last=$pages;    
  if   ($page>1)    
  {    
  echo   "<a   href='fenye.php?page=".$first."'>首頁</a>     ";    
  echo   "<a   href='fenye.php?page=".$prev."'>上一頁</a>     ";    
  }    
  if   ($page<$pages)    
  {    
  echo   "<a   href='fenye.php?page=".$next."'>下一頁</a>     ";    
  echo   "<a   href='fenye.php?page=".$last."'>尾頁</a>     ";    
  }    
  echo   "轉到<input   type=text   name='ys'   size='2'   value=".$page.">頁";    
  echo   "<input   type=submit   name='Submit'   value='Go'>";    
  echo   "</form>";    
  echo   "</div>";    

  ?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.