2012-04-14 73 views
0

該列表只停留在十個項目中,沒有分頁來獲得其他列表的剩餘部分,這很糟糕,但是謝謝夥計們,也許我需要更多的練習,我真的需要這一個工作,所以我可以從這個學習,任何方式的第一個代碼工作,第二個作品,但停在十個項目,你說試試我用一些代碼我已經,下一個,後面和頁數出現,但沒有列表,再次嘗試沒有下一個,但後來有10個項目,我開始有點沮喪在這裏。我如何獲得我搜索的名稱,並將分頁添加到存在的結果中

<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

如何使用PHP是分頁這裏經常提出問題。你可能會發現[PHP中的分頁](http://stackoverflow.com/questions/6963766/pagination-in-php)有用。 – 2012-04-14 20:35:15

回答

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; 
+0

indeeed that is true,updated the example – 2012-04-14 20:18:39

+0

Zolex謝謝你是一個很快回答的工廠,救生員兄弟,它的第一部分工作,現在我嘗試了第二部分,但似乎我可能把它放在錯誤的地方,在那裏我確切地把分頁代碼。謝謝 - – JACrypto 2012-04-14 20:25:43

+0

就在查詢之前。究竟是不是與它合作? – 2012-04-14 20:28:04

相關問題