PHP中分页列和行的问题
我想在5列4行中显示20条记录,并在底部显示“下一条”/“上一条”按钮 由于某些原因,PHP中分页列和行的问题,php,pagination,rows,Php,Pagination,Rows,我想在5列4行中显示20条记录,并在底部显示“下一条”/“上一条”按钮 由于某些原因,$rowsperpage=5会在1行5列中结束。如果我增加这个数字,它只会增加另一列 实际分页工作正常,但列和行不配合 你知道我怎样才能达到预期的效果吗 以下是我使用的代码: <?php // find out how many rows are in the table $sql = "SELECT COUNT(*) FROM users"; $result = mysql_que
$rowsperpage=5
会在1行5列中结束。如果我增加这个数字,它只会增加另一列
实际分页工作正常,但列和行不配合
你知道我怎样才能达到预期的效果吗
以下是我使用的代码:
<?php
// find out how many rows are in the table
$sql = "SELECT COUNT(*) FROM users";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);
$numrows = $r[0];
// number of rows to show per page
$rowsperpage = 5;
// find out total pages
$totalpages = ceil($numrows / $rowsperpage);
// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
// cast var as int
$currentpage = (int) $_GET['currentpage'];
} else {
// default page num
$currentpage = 1;
} // end if
// if current page is greater than total pages...
if ($currentpage > $totalpages) {
// set current page to last page
$currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
// set current page to first page
$currentpage = 1;
} // end if
// the offset of the list, based on current page
$offset = ($currentpage - 1) * $rowsperpage;
// get the info from the db
$sql = "SELECT id, name, picture, phone, address1 FROM users LIMIT $offset, $rowsperpage";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
// while there are rows to be fetched...
echo "<table style='width: 800px; height: 250px'>";
echo "<tr style='width: 800px;'>";
while ($list = mysql_fetch_assoc($result)) {
// echo data
echo "<td align='center' style='width: 800px; height: 250px'>" . $list ['name'] . "<br /><a href='http://www.snarrf.com/onsale/detail.php?id=" . $list ['id'] . "'><img src='http://www.snarrf.com/onsale/nooks/" . $list['picture'] . "' height='100' width='100' border='0' /></a><br />" . $list['address1'] ."</td>" ;
} // end while
echo "</tr>";
echo "</table>";
/****** build the pagination links ******/
echo "<table align='center' >";
echo "<tr >";
echo "<td align='center' valign='top'><img src='images/sn1.gif'><br></td>";
// range of num links to show
$range = 3;
// if not on page 1, don't show back links
if ($currentpage > 1) {
// show << link to go back to page 1
//echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
// get previous page num
$prevpage = $currentpage - 1;
// show < link to go back to 1 page
//echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
} // end if
// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
// if it's a valid page number...
if (($x > 0) && ($x <= $totalpages)) {
// if we're on current page...
if ($x == $currentpage) {
// 'highlight' it but don't make a link
echo "<td align='center'> <img src='images/sn2.gif' border='0'><br>$x </td>";
// if not current page...
} else {
// make it a link
echo " <td align='center' valign='top'><a href='{$_SERVER['PHP_SELF']}? currentpage=$x'><img src='images/sn2.gif' border='0'><br>$x</a> </td>";
} // end else
} // end if
} // end for
// if not on last page, show forward and last page links
if ($currentpage != $totalpages) {
// get next page
$nextpage = $currentpage + 1;
// echo forward link for next page
//echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> ";
// echo forward link for lastpage
//echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> ";
} // end if
echo "<td valign='top'><img src='images/sn3.gif'><br></td>";
echo "</tr>";
echo "</table>";
echo "<table align='center' width='100' >";
echo "<tr width='100' >";
echo "<td >";
// range of num links to show
$range = 3;
// if not on page 1, don't show back links
if ($currentpage > 1) {
// show << link to go back to page 1
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
// get previous page num
$prevpage = $currentpage - 1;
// show < link to go back to 1 page
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
} // end if
// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
// if it's a valid page number...
if (($x > 0) && ($x <= $totalpages)) {
// if we're on current page...
if ($x == $currentpage) {
// 'highlight' it but don't make a link
// if not current page...
} else {
// make it a link
} // end else
} // end if
} // end for
// if not on last page, show forward and last page links
if ($currentpage != $totalpages) {
// get next page
$nextpage = $currentpage + 1;
// echo forward link for next page
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> ";
// echo forward link for lastpage
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> ";
} // end if
echo "</td>";
echo "</tr>";
echo "</table>";
/****** end build pagination links ******/
?>
我相信
while ($list = mysql_fetch_assoc($result)) {
echo "<td align='center' style='width: 800px; height: 250px'>" . $list ['name'] . "<br /><a href='http://www.snarrf.com/onsale/detail.php?id=" . $list ['id'] . "'><img src='http://www.snarrf.com/onsale/nooks/" . $list['picture'] . "' height='100' width='100' border='0' /></a><br />" . $list['address1'] ."</td>" ;
}
while($list=mysql\u fetch\u assoc($result)){
回显“$list['name']”。
“$list['address1']”;
}
与
$rows\u计数器=0;
而($list=mysql\u fetch\u assoc($result)){
回显“$list['name']”。
“$list['address1']”;
如果(!(++$rows\u计数器%5)和$rowsperpage!=$rows\u计数器)
回声“;
}
应该做你想做的事。太棒了,谢谢-我不得不稍微修改一下号码,但我得到的只是供参考的$行\计数器=20;结束时必须与$rowsperpage=20的值相同;在我的例子中,20(表示我想要的总记录数)和if(!(++$rows\u计数器%5)应反映所需的行数,因此5行20条记录,产生4列=成功!感谢帮助!!!Ranty-我如何直接与您联系?-如果可能,我想发言。您可能对我正在进行的项目感兴趣。请随时发送邮件给我:skylight@list.ru
$rows_counter = 0;
while ($list = mysql_fetch_assoc($result)) {
echo "<td align='center' style='height: 250px'>" . $list['name'] . "<br /><a href='http://www.snarrf.com/onsale/detail.php?id=" . $list['id'] . "'><img src='http://www.snarrf.com/onsale/nooks/" . $list['picture'] . "' height='100' width='100' border='0' /></a><br />" . $list['address1'] ."</td>" ;
if (!(++$rows_counter % 5) and $rowsperpage != $rows_counter)
echo "</tr><tr>";
}