2012-08-13 43 views
0

我在PHP和MySQL,但是當沒有記錄它給錯誤mysql_fetch_array() expects parameter 1 to be resource 在此代碼爲我的網站編碼分頁代碼當數據庫表比空它將Page 0 of 0 Back -1 0
,如果條記錄是在數據庫表中比它工作得很好。 我的PHP分頁代碼如下分頁的PHP和MySQL

$sql = mysql_query("SELECT id, firstname, country FROM myTable ORDER BY id ASC"); 
$nr = mysql_num_rows($sql); 
if (isset($_GET['pn'])) { 
$pn = preg_replace('#[^0-9]#i', '', $_GET['pn']); 
} else { 
$pn = 1; 
} 
$itemsPerPage = 10; 
$lastPage = ceil($nr/$itemsPerPage); 
if ($pn < 1) { // If it is less than 1 
$pn = 1; // force if to be 1 
} else if ($pn > $lastPage) { // if it is greater than $lastpage 
$pn = $lastPage; // force it to be $lastpage's value 
} 
$centerPages = ""; 
$sub1 = $pn - 1; 
    $sub2 = $pn - 2; 
    $add1 = $pn + 1; 
    $add2 = $pn + 2; 
if ($pn == 1) { 
$centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;'; 
$centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' .    $add1 . '</a> &nbsp;'; 
} else if ($pn == $lastPage) { 
$centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' .  $sub1 . '</a> &nbsp;'; 
$centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;'; 
} else if ($pn > 2 && $pn < ($lastPage - 1)) { 
$centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub2 . '">' . $sub2 . '</a> &nbsp;'; 
$centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;'; 
$centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;'; 
$centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;'; 
$centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add2 . '">' . $add2 . '</a> &nbsp;'; 
} else if ($pn > 1 && $pn < $lastPage) { 
$centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;'; 
$centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;'; 
$centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;'; 
} 
$limit = 'LIMIT ' .($pn - 1) * $itemsPerPage .',' .$itemsPerPage; 
$sql2 = mysql_query("SELECT id, firstname, country FROM myTable ORDER BY id ASC $limit"); 

if ($lastPage != "1"){ 
// This shows the user what page they are on, and the total number of pages 
$paginationDisplay .= 'Page <strong>' . $pn . '</strong> of ' . $lastPage. '&nbsp; &nbsp; &nbsp; '; 
// If we are not on page 1 we can place the Back button 
if ($pn != 1) { 
    $previous = $pn - 1; 
    $paginationDisplay .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $previous . '"> Back</a> '; 
} 
// Lay in the clickable numbers display here between the Back and Next links 
$paginationDisplay .= '<span class="paginationNumbers">' . $centerPages . '</span>'; 
// If we are not on the very last page we can place the Next button 
if ($pn != $lastPage) { 
    $nextPage = $pn + 1; 
    $paginationDisplay .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $nextPage . '"> Next</a> '; 
} 
} 
$outputList = ''; 
    while($row = mysql_fetch_array($sql2)){ 

$id = $row["id"]; 
$firstname = $row["firstname"]; 
$country = $row["country"]; 

$outputList .= '<h1>' . $firstname . '</h1><h2>' . $country . ' </h2><hr />'; 

} 

<h2>Total Items: <?php echo $nr; ?></h2> 

    <div style="margin-left:58px; margin-right:58px; padding:6px; background-color:#FFF; border:#999 1px solid;"><?php echo $paginationDisplay; ?></div> 
    <div style="margin-left:64px; margin-right:64px;"><?php print "$outputList"; ?></div> 
    <div style="margin-left:58px; margin-right:58px; padding:6px; background-color:#FFF; border:#999 1px solid;"><?php echo $paginationDisplay; ?></div> 

能否有人建議好分頁與page 1 of 1 from 1選項?

+0

縮進你的代碼怎麼樣? – ThiefMaster 2012-08-13 11:48:31

+1

你做錯了。把它放在紙上,然後重試。整個分頁代碼應該在最多15行。 – 2012-08-13 11:55:44

+0

我把$ pn = 0;如果沒有獲得pn,所以錯誤得到解決,但下一頁和第二頁的選項即將到來。 – Mehuldabhi 2012-08-13 11:56:12

回答

1

試試看看這個代碼。

'下一步' 和 '上一個' 鏈接

echo "<h5>Showing ".$start." to ".($start+$limit)." Records of ".$count." Records</h5>"; 
if($start<=($count-$limit)) 
{ 
    echo '<a style="float:right" href="'.$_SERVER['PHP_SELF'].'?start='.($start+$limit).'&limit='.$limit.'&column='.$column.'&order='.$order.'&status='.$status.'&b='.$b.'&type='.$type.'&search='.$search.'"><t1>Next</t1></a>'; 
} 


$prev = $start-$limit; 
if ($prev >= 0) 
{ 
    echo '<a style="float:left" href="'.$_SERVER['PHP_SELF'].'?start='.$prev.'&limit='.$limit.'&column='.$column.'&order='.$order.'&status='.$status.'&b='.$b.'&type='.$type.'&search='.$search.'"><t2>Previous</t2></a>'; 
} 

頁碼

$i=0; 
$l=1; 
echo "<p align='center'>"; 
for($i=0;$i < $count;$i=$i+$limit) 
{ 
    if($i <> $start) 
    { 
     echo "<a href='listing_test.php?start=$i&limit=$limit'><font face='Verdana' size='2'><b>&nbsp;$l&nbsp;</b></font></a> "; 
    } 
    else 
    { 
     echo "<font face='Verdana' size='4' color=#2E9AFE ><b>&nbsp;$l&nbsp;</b></font>"; 
    }   
$l=$l+1; 
} 
echo "</p>"; 

這完全爲我工作!

這裏$count是在表中的行總數,$limit是要顯示在每一頁上

+0

我要'第2頁共10頁'選項... – Mehuldabhi 2012-08-13 11:59:06

0

您必須檢查結果集是空的
這段代碼可以幫助你..帶環繞你的分頁代碼LIMIT
if ($nr==1){ //code if no records are available}else{//if data is available}