2011-09-06 7 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

있어 당신이 코드 예제에서''떠났기 때문에 다행히 진단 할 수 있습니다. 정확하게. – alex

+0

많은 코드가 필요합니다. 귀하의 질문을 * 많이 * 더 간단하게 만드십시오. – willdanceforfun

+0

코드 스 니펫을 상당히 줄이십시오. 그렇지 않으면이 질문은 지나치게 광범위하게 닫힙니다. –

답변

2

세상에, 프레임 워크를 사용하십시오. 나는 당신의 코드를 많이 보지 못했지만 당신의 데이터베이스에 어떻게 질의를하는지, SQL Injection을 통해 악의적 인 코드를 생성하게 될 것이라고 확신한다. (질의는 분리되지 않으므로 매번 위생 처리가 필요하다.) .

또한 프레임 워크에는 페이징 기능이 내장되어 있습니다. 여기

는 PHP에 대한 목록 (I 루비와 nodejs에 dev에, 그래서 더있을 수 있습니다)

+1

Amen amen amen amen! 지옥, 프레임 워크를 사용하지 않더라도 페이지 매김을위한 기존의 잘 테스트 된 드롭 인 클래스가 많이 있습니다. http://pear.php.net/package/Pager – ceejayoz

+1

제 경우에는 배우려고 해요? 라이브 사이트를 만드는 중이라면 프레임 워크를 사용하고 있지만, 함수를 호출하기 전에 그 프레임 워크의 작동을 이해해서는 안됩니까? –

+0

어떻게 프레임 워크 코드를 볼 수 있습니다. 모든 것을 이해할 필요는 없습니다. 배우고 싶다면 원하는 프레임 워크를 사용하여 웹 앱을 만들고 실험 해보는 것이 좋습니다. 당신은 훨씬 더 많은 지식을 얻을 것입니다. –

관련 문제