繁体   English   中英

我如何获取要搜索的名称,以及如何在出现的结果中添加分页

[英]How can i get the names that i search for, and add a pagination to the results that are present

该列表仅停留在十个项目中,没有分页显示,可以得到其他列表的其余内容,这很糟糕,但是,谢谢你们,也许我需要更多的练习,我确实需要这个项目才能正常工作,以便我可以从任何一个项目中学习第一个代码起作用了,第二个起作用了,但是停在了十个项目上,您说尝试使用我已经使用的一些代码来执行ceil,下一个和后一个以及页面数出现了,但是没有列表,再次尝试了下一个和后一个,但是出现了十个项目,我在这里开始有些沮丧。

<DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"  />
    <title>Residential</title>
</head>
<body>
    <section>
        <form method="post" action="index.php?go" id="searchform">
            <input type="text" id="sf" name="name" class="inputtext" >
            <input type="submit" name="submit" id="sb" alt="Btn" value="GO">
        </form>
    </section>
    <div class="results">
    <?php
        error_reporting(0);
        if(isset($_POST['submit'])) {
            if(isset($_GET['go'])) {
                if(preg_match("/[A-Z | a-z]+/", $_POST['name'])){    //"/[A-Z | a-z]+/",
$name=$_POST['name'];

                //connect to the database
                $db=mysql_connect ("localhost", "root", "") or die ('I cannot connect to the database    because: ' . mysql_error()); 

                //-select the database to use
                $mydb=mysql_select_db("residential");

这是更改代码的开始

//////////pagination////////////

$sql=mysql_query("SELECT userId, FirstName, LastName FROM residentialbackup WHERE     FirstName ='" . $name . "' OR LastName = '" . $name ."'");

//-count results
$numrows=mysql_num_rows($sql);

// Get the value of the last page in the pagination result set
$lastPage = ceil($numrows / $itemsPerPage);

$itemsPerPage = 10;

$page = $_GET['page'];
if (!$page) $page = 1;
$offset = ($page - 1) * $itemsPerPage;

$sql2=mysql_query("SELECT userId, FirstName, LastName FROM residentialbackup WHERE    FirstName = '" . $name . "' OR LastName = '" . $name ."' LIMIT ". $offset . ", ".   $itemsPerPage);

$paginationDisplay = ""; // Initialize the pagination output variable
// This code runs only if the last page variable is ot equal to 1, if it is only 1 page     we require no paginated links to display
if ($lastPage != "1") {
    // This shows the user what page they are on, and the total number of pages
    $paginationDisplay .= 'Page <strong>' . $page . '</strong> of ' . $lastPage. '&nbsp;      &nbsp;  &nbsp; ';
    // If we are not on page 1 we can place the Back button
    if ($page != 1) {
        $previous = $page - 1;
        $paginationDisplay .=  '&nbsp;  <a href="' . $_SERVER['PHP_SELF'] . '?page=' . $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 ($page != $lastPage) {
        $nextPage = $page + 1;
        $paginationDisplay .=  '&nbsp;  <a href="' . $_SERVER['PHP_SELF'] . '?page=' .   $nextPage . '"> Next</a> ';
    } 
}

echo $paginationDisplay;
///////////////pagination end////////////

我坚持到这里!

//echo "<p>" .$numrows . " results found for " . stripslashes($name) . "</p>"; 
//-create while loop and loop through result set
        while($row=mysql_fetch_array($sql2)) {
            $FirstName =$row['FirstName'];
            $LastName=$row['LastName'];
            $userId=$row['userId']; 

            //-display the result of the array
            echo "<ul>\n"; 
            echo "<li>" . "<a href=\"index.php?id=$userId\">"  .$FirstName . " " . $LastName . "     </a></li>\n";
            echo "</ul>";
        }
    }
    else { 
        echo "<p>Please enter a search query</p>";
    }
}
/*
  if(isset($_GET['by'])){
  $name=$_GET['by'];
  include "connectres.php";
  //-query the database table
  $sql="SELECT userId, FirstName, LastName FROM residentialbackup WHERE FirstName LIKE '%" . $name . "%' OR LastName LIKE '%" . $name ."%'";
  //-run the query against the mysql query function
  $result=mysql_query($sql); 
  //-count results
  $numrows=mysql_num_rows($result);
  echo "<p>" .$numrows . " results found for " . $name . "</p>"; 
  //-create while loop and loop through result set
  while($row=mysql_fetch_array($result)){
  $FirstName =$row['FirstName'];
$LastName=$row['LastName'];
$userId=$row['userId']; 
  //-display the result of the array
  echo "<ul>\n"; 
  echo "<li>" . "<a href=\"index.php?id=$userId\"> " . $LastName . " " . $FirstName . "     </a></li>\n";
  echo "</ul>";
  }
  }
*/
if(isset($_GET['id'])) {
    $userId=$_GET['id'];
    //connect to the database
    include "connectres.php";
    //-query the database table
    $sql="SELECT * FROM residentialbackup WHERE userId=" . $userId;
    //-run the query against the mysql query function
$result=mysql_query($sql); 
    //-create while loop and loop through result set
    while($row=mysql_fetch_array($result)) {
        $FirstName =$row['FirstName'];
        $LastName=$row['LastName'];
        $PhoneNumber=$row['PhoneNumber'];
        $Address=$row['Address'];
        //-display the result of the array
        echo "<ul>\n"; 
        echo "<li>" . "<img src=\"../images/person.png\"/>" . $FirstName . " " . $LastName . "   </li>\n";
        echo "<li>" . "<img src=\"../images/tell.png\"/>" . "&nbsp;" . "<a href=tel:" .    $PhoneNumber .  ">" . $PhoneNumber . "</a></li>\n";
        echo "<li>" . "<img src=\"../images/house.png\"/>" . "&nbsp;" . $Address . "</li>\n";
        echo "</ul>";
    }
}
?>
</div>
</div>
</body>
</html>

您的查询中包含通配符,请将其删除以仅获取完全匹配项

$sql="SELECT userId, FirstName, LastName FROM residentialbackup WHERE FirstName ='" . $name . "' OR LastName = '" . $name ."'";

您可以像这样进行分页:

$itemsPerPage = 10;
$page = $_GET['page'];
if (!$page) $page = 1;
$offset = ($page - 1) * $itemsPerPage;

$sql="SELECT userId, FirstName, LastName FROM residentialbackup WHERE FirstName = '" . $name . "' OR LastName = '" . $name ."' LIMIT ". $offset . ", ". $itemsPerPage;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM