0

列表只停留在十个项目,没有分页来获取其他列表的其余部分,这很糟糕,但是谢谢伙计们,也许我需要更多的练习,我真的需要这个来工作,所以我可以从这个中学习,无论如何第一个代码有效,第二个有效,但停在十个项目,你说尝试我用一些我已经拥有的代码做的 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>
4

1 回答 1

0

您的查询中有通配符,删除它们以仅获取完全匹配

$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;
于 2012-04-14T20:14:30.820 回答