標籤:mit host image localhost etc 標籤 技術分享 set ota
1 <?php 2 @mysql_connect("localhost","root","")or die; 3 @mysql_select_db("test1")or die; 4 $query = @mysql_query("select * from yonghu")or die; 5 $pagesize = 15; //設定每頁記錄數 6 $sum = mysql_numrows($query); //計算總記錄數 7 if($sum%$pagesize == 0) 8 $total = (int)($sum/$pagesize); 9 else 10 $total = (int)($sum/$pagesize)+1;11 12 if(isset($_GET[‘page‘]))13 {14 $p = (int)$_GET[‘page‘];15 }16 else 17 {18 $p = 1;19 }20 21 $start = $pagesize * ($p-1);22 $query = @mysql_query("select * from yonghu limit $start,$pagesize")or die;23 echo "<table border=1><tr align=center><th>使用者名稱</th><th>性別</th><th>出生日期</th><th>郵箱</th></tr>";24 while ($row = mysql_fetch_array($query))25 {26 $username = $row[‘username‘];27 $sex = $row[‘sex‘];28 $birth = $row[‘birth‘];29 $email = $row[‘email‘];30 echo "<tr>";31 echo "<td>{$username}</td>";32 echo "<td>{$sex}</td>";33 echo "<td>{$birth}</td>";34 echo "<td>{$email}</td>";35 echo "</tr>";36 }37 echo "<table>";38 if ($p>1)39 {40 $prev = $p-1;41 echo "<a href = ‘?page=$prev‘>上一頁</a>";42 }43 if($p<$total)44 {45 $next = $p+1;46 echo "<a href = ‘?page=$next‘>下一頁</a>";47 }48 ?>
該分頁使用的是地址欄的$_GET方式來將值賦給下一頁的<a>標籤裡的連結變數,從而實現了將該值通過
$start = $pagesize * ($p-1);
的關係,成為limit新的位移量。
1 if(isset($_GET[‘page‘])) 2 { 3 $p = (int)$_GET[‘page‘]; 4 } 5 else 6 { 7 $p = 1; 8 } 9 10 $start = $pagesize * ($p-1);
mysql資料匯出並分頁