2011-09-06 47 views
-4

因此,基本上所有內容都顯示在search.php上。但是,一旦它進入search.php?pn = 2,表格和分頁導航消失。這裏的代碼第2頁上的分頁功能將無法工作PHP

<?php require('includes/header.php'); ?> 
<?php 

/* Array values 
* ------------ 
* $_POST['search']; // the search field 
* $_POST['submitsearch']; // the search button 
* 
*/ 

if(isset($_POST['submitsearch'])) { 
    $search = mysql_real_escape_string(htmlentities(trim($_POST['search']))); 
    $search = preg_split('/[\s]+/', $search); 
    $total_words = count($search); 
    $postsearch = $_POST['search']; 

    if(empty($_POST['search'])) { 
     echo "Enter something to search"; 
    }else{ 
     if(strlen($_POST['search']) < 4) { 
      echo "Search must be 3 or more characters long"; 
     }else{ 
      $where = ""; 

      foreach ($search as $key=>$searches) { 
       $where .= "description like '%{$searches}%'"; 
       if($key != $total_words - 1) { 
        $where.= " and "; 
       } 
      } 

      $results = mysql_query("select name, left(description, 100) as description, logo, userid, number_sold, price from ns_products where $where") or die(mysql_error()); 

      echo "<table border=1>"; 
      if(mysql_num_rows($results)==0) { 
       echo "<tr><td>No results found for <strong>$postsearch</strong>.</td></tr>"; 
      }else{ 

      //originial query 
$sql = mysql_query("select id, name, left(description, 100) as description, logo, userid, number_sold, price from ns_products where $where"); 
//end of original query 


$nr = mysql_num_rows($sql); 
if (isset($_GET['pn'])) { 
    $pn = preg_replace('#[^0-9]#i', '', $_GET['pn']); 
} else { 
    $pn = 1; 
} 

$itemsPerPage = 3; 
$lastPage = ceil($nr/$itemsPerPage); 
if ($pn < 1) { 
    $pn = 1; 
} else if ($pn > $lastPage) { 
    $pn = $lastPage; // force it to be $lastpage's value 
} 
// This creates the numbers to click in between the next and back buttons 
$centerPages = ""; // Initialize this variable 
$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="search.php?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;'; 
} else if ($pn == $lastPage) { 
    $centerPages .= '&nbsp; <a href="search.php?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;'; 
$centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;'; 
} else if ($pn > 2 && $pn < ($lastPage - 1)) { 
    $centerPages .= '&nbsp; <a href="search.php?pn=' . $sub2 . '">' . $sub2 . '</a> &nbsp;'; 
$centerPages .= '&nbsp; <a href="search.php?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;'; 
$centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;'; 
$centerPages .= '&nbsp; <a href="search.php?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;'; 
$centerPages .= '&nbsp; <a href="search.php?pn=' . $add2 . '">' . $add2 . '</a> &nbsp;'; 
} else if ($pn > 1 && $pn < $lastPage) { 
$centerPages .= '&nbsp; <a href="search.php?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;'; 
$centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;'; 
$centerPages .= '&nbsp; <a href="search.php?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;'; 
} 

$limit = 'LIMIT ' .($pn - 1) * $itemsPerPage .',' .$itemsPerPage; 


$sql2 = mysql_query("select id, name, left(description, 100) as description, logo, userid, number_sold, price from ns_products where $where $limit"); 


$paginationDisplay = ""; 
if ($lastPage != "1"){ 
    $paginationDisplay .= 'Page <strong>' . $pn . '</strong> of ' . $lastPage. '<img src="images/clearImage.gif" width="48" height="1"/>'; 
    if ($pn != 1) { 
     $previous = $pn - 1; 
     $paginationDisplay .= '&nbsp; <a href="search.php?pn=' . $previous . '"> Back</a>  '; 
    } 
    $paginationDisplay .= '<span class="paginationNumbers">' . $centerPages . '</span>'; 
    if ($pn != $lastPage) { 
    $nextPage = $pn + 1; 
    $paginationDisplay .= '&nbsp; <a href="search.php?pn=' . $nextPage . '"> Next</a>  '; 
    } 
} 

$num_results = mysql_num_rows($results); 

if($num_results == 1) { 
    echo "Search has returned 1 result."; 
}else{ 
    echo "Search has returned $num_results results."; 
} 

echo "<tr><td></td><td>Name</td><td>Description</td><td>Sold</td><td>Price</td><td>Owner</td></tr>"; 
//while loop here 
while($row8 = mysql_fetch_array($sql2)) { 
    $prodname = $row8['name']; 
    $prodid = $row8['id']; 
    $proddesc = $row8['description']; 
    $prodprice = $row8['price']; 
    $prodlogo = $row8['logo']; 
    $prodsold = $row8['number_sold']; 
    $userid = $row8['userid']; 
    $prodownerquery = mysql_fetch_assoc(mysql_query("select * from users where id='{$userid}'")); 
    $prodowner = $prodownerquery['username']; 

    echo "<tr><td><img src=images/productpics/$prodlogo></td><td><a href=viewproduct.php?pid=$prodid>$prodname</a></td><td width=200>$proddesc...</td><td>$prodsold</td><td>$$prodprice</td><td><a href=viewuser.php?pid=$userid>$prodowner</a></td></tr>"; 

     } 
     } 

    } 
    } 
} 
echo "</table>"; 
echo $centerPages; 

?> 
<?php require('includes/footer.php') ?> 

謝謝!

+1

我很高興你在代碼示例中留下了<?php require('includes/footer.php')?>',這樣我就可以診斷問題了準確地說。 – alex

+0

這就需要很多代碼。讓你的問題*更簡單。 – willdanceforfun

+0

請大幅縮小您的代碼片段,否則此問題可能會過於寬泛地關閉。 –

回答

2

看在上帝的份上,使用一個框架。我沒有看過你的代碼,只是看你如何查詢數據庫,我相信你最終會通過SQL Injections創建可利用的代碼(你的查詢不是分離的,所以你需要每次都進行清理) 。

另外,框架有內置的頁面調度。

下面是PHP(我dev的在Ruby和的NodeJS,所以有可能是更多)的列表

+1

阿門阿門阿門!地獄,即使他不使用框架,也有大量現有的,經過充分測試的,用於分頁的入門課程。 http://pear.php.net/package/Pager – ceejayoz

+1

在我的情況下,我試圖學習?如果我正在創建一個活動網站,我將使用一個框架,但是在我爲它調用一個函數之前,我不應該理解它的作品嗎? –

+0

你可以瀏覽框架代碼,看看它是如何做到的。您不需要了解所有內容,如果您想學習,我建議您使用您選擇的框架創建一個Web應用程序並對其進行實驗。這樣你將獲得更多的知識 –