如何获取我搜索的名称,并为存在的结果添加分页

时间:2012-04-14 20:10:43

标签: php phpmyadmin

  

这个列表只剩下十个没有分页的项目来获取剩下的其他列表,这很糟糕,但感谢伙计们,也许我需要更多练习我真的需要这个才能工作所以我可以从这个中学习,第一个代码工作的任何方式,第二个工作,但停止在十个项目,你说尝试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>

1 个答案:

答案 0 :(得分: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;