1

因此,我有一个由 Frost 慷慨提供的查询示例,它帮助我学到了很多东西(我是个新手),但我一直在尝试添加分页。我已经在网上搜索了一个星期了,有很多不同的例子,我尝试了很多,但收效甚微或没有成功。我终于能够构建您在下面看到的内容,问题是当您点击搜索时,它确实只返回 20 条记录,但页面不起作用。他们不起作用的方式是,当您单击“下一步”时,它将转到第二页但没有结果显示,如果您再次单击期待第 3 页,它不会去那里,它会停留在第 2 页。我真的不想接受任何人的宝贵时间,但我非常迷失。这是“我的”代码:

    <?php

    //we select all the License records
    $req_limit = mysql_query("Select License from OPLR");
    $result = mysql_numrows($req_limit);//mysql_numrows() give us the result of the     request

    // now we will use the result to limit the displayed messages
    $page_limit = '20'; //we chose the number of messages by page
    // here we divide the total by the number of messages we choose
    $page_number = $result / $page_limit; 

    // we round the number of pages to avoid commas.
    $total_number = ceil($page_number); 

    // here we take of one page because the first page will be displyed is number one
    $number = $total_number - 1; 

    // if the variable number_page is equal or defferent to 0
    if(isset($_GET['page_number']) || $_GET['page_number'] != '0' ) 

    {
    // multiplies the page limit with the current number page on the url
    $mysql_limit = $page_limit * $_GET['page_number']; 

    }
    else{ // no variable number_page

    $mysql_limit = '0'; // the limit is 0

    }

    ?>

    <?php
    /*****************************
     *  Simple SQL Search Tutorial by Frost
     *  of Slunked.com
     ******************************/



    // Set up our error check and result check array
    $error = array();
    $results = array();

    // First check if a form was submitted. 
    // Since this is a search we will use $_GET
    if (isset($_GET['search'])) {
       $searchTerms = trim($_GET['search']);
       $searchTerms = strip_tags($searchTerms); // remove any html/javascript.

       if (strlen($searchTerms) < 0) {
  $error[] = "Search terms must be longer than 3 characters.";
       }else {
          $searchTermDB = mysql_real_escape_string($searchTerms); // prevent sql         injection.
       }

       // If there are no errors, lets get the search going.
               if (count($error) < 1) {

          $searchSQL = "SELECT License, Pet_Name, Owner_Name, Species, Notes, Unowned FROM OPLR WHERE ";

  // grab the search types.
  $types = array();
  $types[] = isset($_GET['License'])?"`License` LIKE '%{$searchTermDB}%'":'';
  $types[] = isset($_GET['Pet_Name'])?"`Pet_Name` LIKE '%{$searchTermDB}%'":'';
  $types[] = isset($_GET['Owner_Name'])?"`Owner_Name` LIKE '%{$searchTermDB}%'":'';
  $types[] = isset($_GET['Species'])?"`Species` LIKE '%{$searchTermDB}%'":'';
  $types[] = isset($_GET['Unowned'])?"`Unowned` LIKE 'y'":'';

   $types = array_filter($types, "removeEmpty"); // removes any item that was empty (not checked)

  if (count($types) < 1)
     $types[] = "`Pet_Name` LIKE '%{$searchTermDB}%'"; // use the Pet_Name as a default search if none are checked

      $andOr = isset($_GET['matchall'])?'AND':'OR';


  $searchSQL .= implode(" {$andOr} ", $types) . " ORDER BY `Pet_Name`"; // order by Pet Name.

  $searchResult = mysql_query($searchSQL) or trigger_error("There was an error.<br/>"         . mysql_error() . "<br 

    />SQL Was: {$searchSQL}");

  if (mysql_num_rows($searchResult) < 1) {
     $error[] = "The search term provided {$searchTerms} yielded no results.";
  }else {
     $results = array(); // the result array
     $i = 1;
     while ($row = mysql_fetch_assoc($searchResult)) {
        $results[] = "<B>License Number: {$row['License']}</B><br />Pet Name: {$row['Pet_Name']}<br /> Owner Name: {$row['Owner_Name']}<br />Species: {$row['Species']}<br />Notes: {$row['Notes']}<br /><br />";
        $i++;
     }
  }
       }
    }


    ?>
    <?php echo (count($error) > 0)?"The following had errors:<br /><span         id=\"error\">" . implode("<br />", 

    $error) . "</span><br /><br />":""; ?>
       <form method="GET" action="<?php echo $_SERVER['PHP_SELF'];?>"         name="searchForm" id="searchform">

           <fieldset>
     <center><p>Search For:
     <input type="text" name="search" value="<?php echo         isset($searchTerms)?htmlspecialchars

    ($searchTerms):''; ?>" /></p></center>
   </fieldset>




         <fieldset>


     <div id="checks"><center>
       License Number: 
       <input type="checkbox" name="License" value="on" <?php echo         isset($_GET['License'])?"checked":''; ?> />      
       |    


     Species: 
       <input type="checkbox" name="Species" value="on" <?php echo         isset($_GET['Species'])?"checked":''; ?> /> 
        |  &nbsp;     
     Unowned: 
     <input type="checkbox" name="Unowned" value="on" <?php echo         isset($_GET['Unowned'])?"checked":''; ?> />
     <br />
             Match All Selected Fields? <input type="checkbox" name="matchall"         value="on" <?php echo isset($_GET['matchall'])?"checked":''; ?> />
 <br /><br />

     <input type="submit" name="submit" value="Search!" /></center></div>
 </fieldset>
       </form>
       <?php echo (count($error) >         0)?"The following had errors:<br /><span         id=\"error\">" .   implode("<br />", 

    $error) . "</span><br /><br />":""; ?>

      <?php echo (count($results) > 0)?"Your search: {$searchTerms} <br> <b>Returned:        </b><br /><br />" . implode("", 

    $results):""; 
    ?>
    <?php
    // If the page number different of 0 and if the page_number is unset
    if( $number != '0' && empty($_GET['page_number']))
    {
    print '<a href="registry.php?page_number=1">Next page</a>'; // we set the         page_number to 1
    }

    // in this condition, the variable page_number is set and its value is less than         $number
    elseif($number !='0' && isset($_GET['page_number']) && $_GET['page_number']         <             $number)
    {
           $next = $_GET['page_number'] + 1; // add 1 to the current page number
    print '<a href="registry.php?page_number='.$suivant.'">next page</a>'; //The link         for the next pages
    // go back to the precedent page, we used a java-script code to do it print         '&nbsp;&nbsp;<a href="javascript: history.back();">Previous page</a>';
    }

    // here, the link that will be displayed when the page number is reched
    elseif( $number !='0' && isset($_GET['page_number']) && $_GET['page_number'] >= $number )
    {
    print '<a href="javascript: history.back();">Previous page</a>';
    }
    function removeEmpty($var) {
       return (!empty($var)); 
    }
    ?>
4

2 回答 2

0

你有多个问题。第一个是您使用表单提交从选项中获取值(这很好),但是“下一页”链接只给出了 page_id 而不是所有其他选项。(您可以将它们放在下一个链接中(就像您使用 _GET 一样)或将提交的值存储在 sessionn 中。我更喜欢后者)。

另一个问题是,你做了一些分页计算,但你从不使用你计算的数字。您必须输入一个 mysql LIMIT 语句。(正确的一点是在“ORDER BY”之后。对于那个替换

" ORDER BY `Pet_Name`";

经过

" ORDER BY `Pet_Name` LIMIT ". $mysql_limit .",". $page_limit;

但是请记住,这只会解决问题 2,您仍然必须将那些 _GET 变量放入“下一页”

于 2012-10-23T07:46:57.297 回答
0

改变

print '<a href="registry.php?page_number='.$suivant.'">next page</a>'; //The link for the next pages

print '<a href="registry.php?page_number='.$next.'">next page</a>'; //The link         for the next pages
于 2012-10-23T08:05:43.253 回答