PHPページング機能の作り方

コード下記:

<?php

$conn = mysql_connect(“localhost","root","startnews24″);

$maxnum = 2;

mysql_select_db(“startnews24_test", $conn);
$query1 = “SELECT COUNT(*) AS totalrows FROM startnews24_test “;
$result1 = mysql_query($query1, $conn) or die(mysql_error());
$row1 = mysql_fetch_assoc($result1);
$totalRows1 = $row1['totalrows’];
$totalpages = ceil($totalRows1/$maxnum);

if(!isset($_GET['page’]) || !intval($_GET['page’]) || $_GET['page’] > $totalpages) $page = 1;
/
else $page = $_GET['page’];

$startnum = ($page – 1)*$maxnum;

$query = “SELECT * FROM startnews24_test LIMIT $startnum,$maxnum";
$result = mysql_query($query, $conn) or die(mysql_error());
$row = mysql_fetch_assoc($result);

?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8″>
<title>ページングのサンプル</title>
<script language="JavaScript" type="text/JavaScript">
<!–
function MM_jumpMenu(targ,selObj,restore){ //v3.0
eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
if (restore) selObj.selectedIndex=0;
}
//–>
</script>
<style type="text/css">
a{text-decoration:none;}
a:hover{text-decoration:underline}
table{font-size:12px;}
.tb{background-color:#73BB95}
.tr{background-color:#FFFFFF}
</style>
</head>

<body>
<table width="30%" border="0″ align="center" cellpadding="0″ cellspacing="1″ class="tb">
<tr>
<td height="24″><div align="left">ページングのサンプル</div></td>
</tr>
<?php if($totalRows1) {
do {
?>
<tr class="tr">
<td height="24″><div align="center"><?php echo $row['id’];?></div></td>
</tr>
<?php }while($row = mysql_fetch_assoc($result));?>
</table>

<table width="95%" border="0″ align="center" cellpadding="0″ cellspacing="0″>
<tr><form name="form1″>
<td height="27″><div align="center">
<?php
echo “総計<font color=\"#ff0000\">$totalRows1</font>個記録";
echo “<font color=\"#ff0000\">".$page."</font>"."/".$totalpages."ページ “;
$pre = $page – 1;
$next = $page + 1;
$maxpages = 4;
$pagepre = 1;

if($page != 1) { echo “<a href='".$_SERVER['PHP_SELF’]."'><<</a> “;
echo “<a href='".$_SERVER['PHP_SELF’].’?page=’.$pre."'><</a> “;}

if($maxpages>=$totalpages)
{$pgstart = 1;$pgend = $totalpages;}
elseif(($page-$pagepre-1+$maxpages)>$totalpages)
{$pgstart = $totalpages – $maxpages + 1;$pgend = $totalpages;}
else{
$pgstart=(($page<=$pagepre)?1:($page-$pagepre));
$pgend=(($pgstart==1)?$maxpages:($pgstart+$maxpages-1));
}

for($pg=$pgstart;$pg<=$pgend;$pg++){
if($pg == $page) echo “<a href=\"".$_SERVER['PHP_SELF’]."?page=$pg\"><font color=\"#ff0000\">$pg</font></a> “;
else echo “<a href=\"".$_SERVER['PHP_SELF’]."?page=$pg\">$pg</a> “;
}
if($page != $totalpages)
{echo “<a href='".$_SERVER['PHP_SELF’].’?page=’.$next."'>></a> “;
echo “<a href='".$_SERVER['PHP_SELF’].’?page=’.$totalpages."'>>></a> “;}
?>
<select name="menu1″ onChange="MM_jumpMenu('parent’,this,0)">
<option value="">選択</option>
<?php for($pg1=1;$pg1<=$totalpages;$pg1++) {
echo “<option value=\"".$_SERVER['PHP_SELF’]."?page=$pg1\">".$pg1."</option>";
}?>
</select>
</td></form>
</tr>
</table>
<?php } else {?>
<tr class="tr">
<td height="24″><div align="center">記録が存在しません</div></td>
</tr>
</table>
<?php }?>
</body>
</html>
<?php
mysql_free_result($result1);
mysql_free_result($result);
?>

Source

Posted by arkgame