2012-01-18 26 views
0

我已經看過Stacker的幾十個問題,但也許我在查看分頁的補救措施時錯過了一些東西。PHP Pagaination列和行問題

我想顯示5行4行20條記錄,並在底部有「Next」/「Prvious」按鈕。

由於某種原因,我的$ rowsperpage = 5捲起1行5列,如果我增加數量,它只是增加了另一列 - 拉伸表。

實際的分頁工作正常(至頁1,2,3,4 ...),但列和行不合作。

關於如何達到我想要的效果的任何想法?

這裏是我使用的代碼:

<?php 
DB connection info 

// 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 ******/ 
?> 

感謝您的幫助,您可以!

回答

0

我相信,隨着

$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>"; 
} 

更換

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>" ; 
} 

應該做你在找什麼。

+0

真棒謝謝 - 我不得不修改數字有點,但我知道了 – user1155488 2012-01-18 19:10:43

+0

只是一個fyi ... $ rows_counter = 20; 清盤必須與 $ rowsperpage = 20; 在我的情況下,20(代表總記錄我所要的號碼) 如果(!(++ $ rows_counter%5) 應該反映的行數在5行得到4列=成功所需 因此20條記錄! 感謝您的幫助!!! – user1155488 2012-01-18 19:16:01

+0

Ranty - 如何直接與您聯繫? - 如果可能,我想發言。您可能對我正在開展的項目感興趣 – user1155488 2012-01-18 20:03:59