2012-05-22 216 views
1

你好朋友我想知道如何在搜索引擎中添加分頁。我已經嘗試了很多次,在搜索引擎中添加分頁,但每次我都失敗了。是否有任何人誰修改我下面的代碼添加分頁:搜索引擎和分頁

的index.php

<?php include 'func.inc.php'; ?> 

<html> 

<head> 
<title>Search</title> 
</head> 

<body> 

<h2> Search </h2> 

<form action="" method="POST"> 
     <p> 
      <input type="text" autocomplete="off" name="keywords" /> <input type="submit" value="Search" /> 
     </p> 
</form> 
<?php 
if (isset($_POST['keywords'])) { 
$suffix = ""; 
$keywords = mysql_real_escape_string (htmlentities (trim($_POST['keywords']))); 

    $errors = array(); 

    if (empty($keywords)) { 
     $errors[] = 'Please enter a search term'; 
    } else if (strlen($keywords)<3) { 
     $errors[] = 'Your search term must be three or more character'; 
    } else if (search_results($keywords) === false) { 
     $errors[] = 'Your search for ' .$keywords. ' returned no result'; 
    } 

    if (empty($errors)) { 
     $results = search_results ($keywords); 
     $results_num = count ($results); 

     $suffix = ($results_num !=1) ? 's' : ''; 

     echo '<p> Your search for <strong>', $keywords,'</strong> returned <strong>',$results_num,'</strong> result',$suffix,'</p>'; 
     foreach($results as $result) { 
      echo '<p><strong><a href="',$result['url'],'">', $result['title'],'</a></strong> <br>', $result['description'],'...<br>', $result['url'],'</p>'; 
      } 

    } else { 
    foreach ($errors as $error) { 
     echo $error, '</br>'; 
    } 
     } 

    } 

?> 


</body> 
</html> 

func.inc.php

<?php 
include 'db.inc.php'; 

function search_results($keywords) { 
    $returned_results = array(); 
    $where =""; 

    $keywords = preg_split('/[\s]+/', $keywords); 
    $total_keywords = count($keywords); 

    foreach ($keywords as $key=>$keyword) { 
     $where .="`keywords` LIKE '%$keyword%'"; 
     if ($key != ($total_keywords - 1)) { 
     $where .= "AND"; 
     } 
    } 

    $results = "SELECT `title`, LEFT(`description`, 70) as `description`, `url` FROM `articles` WHERE $where"; 
    $results_num = ($results = mysql_query($results)) ? mysql_num_rows($results): 0; 

    if ($results_num === 0) { 
    return false; 
    } else { 

    while ($results_row = mysql_fetch_assoc($results)) { 
     $returned_results[] = array(
        'title' => $results_row['title'], 
        'description' => $results_row['title'], 
        'url' => $results_row['url'] 


     ); 
    } 

    return $returned_results; 
} 
} 

?> 

,我有一個問題,使關閉本部門頂部的關閉按鈕。

回答

3

我修改函數簽名,包括頁碼和每頁結果數:

function search_results($keywords,$pagenum,$resultsperpage) { 

,然後修改SQL拔出只爲有問題的頁面結果:

$startresult=($pagenum-1)*$resultsperpage; 
$results = "SELECT `title`, LEFT(`description`, 70) as `description`, `url` FROM `articles` WHERE $where LIMIT $startresult,$resultsperpage"; 

(假設你會編號從1開始頁)

然後,所有你需要的是一個鏈接,讓用戶選擇他們的頁面的列表。我通常會使用GET變量(因爲這可能是searchpage.php?p=1),所以您必須按照您的搜索條件進行清理。

0

請不記得喜歡

<?php 
    include 'func.php'; 
    include 'config.inc.php'; 
    ?> 

    <form action="" method="POST"> 
      <p> 
       <input type="text" autocomplete="off" name="keywords" > <input type="submit" value="Search" /> 
      </p> 
    </form> 
    <?php 

    $suffix = ""; 
    $v=''; 

    if(isset($_POST['keywords'])){$v=$_POST['keywords'];} 
    else{ 
     if(isset($_GET['q'])){$v=$_GET['q'];} 

     } 

    $keywords = mysql_real_escape_string (htmlentities (trim($v))); 

     $errors = array(); 

     if (empty($keywords)) { 
      $errors[] = 'Please enter a search term'; 
     } else if (strlen($keywords)<3) { 
      $errors[] = 'Your search term must be three or more character'; 
     } else if (search_results($keywords) === false) { 
      $errors[] = 'Your search for ' .$keywords. ' returned no result'; 
     } 
     if (!($errors)) { 
      $results = search_results($keywords); 

      $results_num = count ($results); 
      $suffix = ($results_num !=1) ? 's' : ''; 

      echo '<p> Your search for <strong>', $keywords,'</strong> returned <strong>',$results_num,'</strong> result',$nom,'</p>'; 

      foreach($results as $result) { 

       echo '<p><strong><a href="',$result['message'],'">', $result['name'],'</a></strong> <br>', $result['id'],'...<br>', $result['message'],'</p>'; 

        } 

    if($pages>=1 && $page<=$pages){ 
    for($x=1;$x<=$pages;$x++){ 

    echo ($x==$page) ? '<strong><a href="?page='.$x.'&q='.$v.'" >'.$x.'</a></strong> ' : '<a href="?page='.$x.'&q='.$v.'" >'.$x.'</a> '; 
    } 
    } 
     } else { 
     foreach ($errors as $error) { 
      echo $error, '</br>'; 
     } 
      } 
    ?> 

func.in.php

<?php 

    function search_results($keywords) { 
    global $page,$start,$pages_query,$per_page,$pages,$returned_results,$total_keywords,$nom; 
     $returned_results = array(); 
     $where =""; 

     $keywords = preg_split('/[\s]+/', $keywords); 
     $total_keywords = count($keywords); 

     foreach ($keywords as $key=>$keyword) { 
      $where .="`message` LIKE '%$keyword%'"; 
      if ($key != ($total_keywords - 1)) { 
      $where .= "AND"; 
      } 
     } 


    $per_page=6; 
    $pages_query = mysql_query(' SELECT COUNT(`id`) FROM `paginate` ') or die(mysql_error()); 

    //$pages=ceil(mysql_result($pages_query,0)/$per_page); 

     $resultt = "SELECT * FROM paginate WHERE $where "; 
     $queryt =mysql_query($resultt) ; 
     $nom=mysql_num_rows($queryt); 
    $pages=ceil($nom/$per_page);  


    $page= (isset($_GET['page'])) ? (int)$_GET['page'] :1; 

    $start=($page - 1) * $per_page; 


     $results = "SELECT * FROM paginate WHERE $where LIMIT $start,$per_page "; 
     $results_num = ($results = mysql_query($results)) ? mysql_num_rows($results): 0; 



     if ($results_num === 0) { 
     return false; 
     } else { 

     while ($results_row = mysql_fetch_assoc($results)) { 
      $returned_results[] = array(
         'id' => $results_row['id'], 
         'name' => $results_row['name'], 
         'message' => $results_row['message'] 


      ); 
     } 

     return $returned_results; 
    } 
    } 

    ?>