正確的是,第8天學習php,我覺得我試圖按錯誤順序做事情....直到昨天,我還沒有做好。我已經設定了創建一個假網上書店的任務(似乎是所有學習php書籍的流行)。首先,我創建了我的產品,並將它們全部列在數據庫的一個頁面上..很簡單,然後我使用下拉框來排列結果...很難但最終做到了。然後嘗試分頁,簡單,然後嘗試加入我的訂購代碼和分頁代碼:我生命中最痛苦的幾個小時,但完成。我利用了一個搜索欄..更容易,然後想到了,現在我試圖將這個搜索欄與我的分頁和訂購代碼混合在一起,我的頭髮正在一小撮掉下來!我真的希望我曾試圖一次學習所有三個,但會是一小撮/頭腦。PHP - 搜索數據庫中的記錄,同時對它們進行排序並對它們進行分頁
所以我的問題是...它分頁...耶,它命令...然而,當我第一次看到搜索的結果...它分頁確定(顯示我有多少頁是哪總是正確的值),但是當我點擊查看第二頁時,停止向我顯示搜索結果,而不是將我帶到數據庫中所有產品的第2頁。如果我嘗試訂購清單,它也會發生同樣的情況,它會更改爲所有產品的有序清單,並告訴我搜索條件爲空!
對不起,這麼長時間的問題,我只是無法找出我滑倒的地方!
非常感謝任何有幫助的人!
繼承人的代碼:
booksearch.php:
(已經採取了所有無關緊要的代碼!)
<?php $query1 = "SELECT *
FROM booktable";
// execute the query
$results1 = mysql_query($query1)
or die(mysql_error());
// count the number of rows that are selected from the table
$field = mysql_num_fields($results1);
// echo $field;
for ($i = 0; $i < $field; $i++)
{
$names[$i] = mysql_field_name($results1, $i);
}
?>
<form name="searchbook" action="productssearch.php" method="get">
<br />
Got For It:<input type="text" id="searchbar" name="searchterm" value="Enter anything and we will search on title, author, publisher and ISBN"></input>
<br /><br />
<input type="submit" name="Search" value=" - Search - "></input>
</form>
productssearch.php:
require "dbconn.php";
$term = $_GET['searchterm'];
$records_per_page = 10;
//If user set the sort order, save to session var - else set default
if(@$_POST['order'])
{
$_SESSION['order'] = trim($_POST['order']);
}
elseif(!isset($_SESSION['order']))
{
$_SESSION['order'] = 'bookname';
}
//Determine the total records and pages
$query = mysql_query ("SELECT COUNT(bookname)
FROM booktable
WHERE bookname LIKE '%".$term."%'
OR bookisbn LIKE '%".$term."%'
OR bookpub LIKE '%".$term."%'
OR bookauthor LIKE '%".$term."%'");
$total_records = mysql_result($query, 0);
$total_pages = ceil($total_records/$records_per_page);
//Set the page to load
$page = (isset($_GET['page'])) ? (int) $_GET['page'] : 1;
if($page<1 || $page>$total_pages)
{
$page = 1;
}
//Create limit var for query
$limit_start = ($page-1) * $records_per_page;
//Create and run query for records on current page
$query = mysql_query ("SELECT bookname, bookauthor, bookpub, bookisbn
FROM booktable
WHERE bookname LIKE '%".$term."%'
OR bookisbn LIKE '%".$term."%'
OR bookpub LIKE '%".$term."%'
OR bookauthor LIKE '%".$term."%'
ORDER BY ".mysql_real_escape_string($_SESSION['order'])." ASC
LIMIT $limit_start, $records_per_page");
//Prepare output
//**************************************************************
$numrow = mysql_num_rows($query);
//Create pagination links
$navLinks = '';
//Prev page
if($page > 1)
{
$prevPage = $page - 1;
$navLinks .= "<a href='productssearch.php?page=$prevPage'>Prev</a> ";
}
else
{
$navLinks .= "Prev ";
}
//Individual pages
for($p=1; $p<=$total_pages; $p++)
{
$pageNo = ($p == $page) ? "<b>{$p}</b>" : $p;
$navLinks .= "<a class='pagin' href='productssearch.php?page=$p'>$pageNo</a> ";
}
//next page
if($page < $total_pages)
{
$nextPage = $page + 1;
$navLinks .= "<a href='productssearch.php?page=$nextPage'>Next</a>";
}
else
{
$navLinks .= "Next";
}
?>
(無關從中間去除的東西)
<div id="mid">
<?php
echo "<table>";
echo "<tr>";
echo "<td>";
echo "</td>";
echo "<td class='toprow'>";
echo "Book Title";
echo "</td>";
echo "<td class='toprow'>";
echo "Book Author";
echo "</td>";
echo "<td class='toprow'>";
echo "Book Publisher";
echo "</td>";
echo "<td class='toprow'>";
echo "Book ISBN";
echo "</td>";
echo "<td>";
echo "</th>";
echo "</tr>";
while ($row = mysql_fetch_assoc($query))
{
$bookname = $row['bookname'];
$bookauthor = $row['bookauthor'];
$bookpub = $row['bookpub'];
$bookisbn = $row['bookisbn'];
// send the values to the browser as a row in a html table
echo "<tr>";
echo "<tr>";
echo "<td>";
echo "<a href='addtolist.php?bookname=".$bookname."&bookauthor=".$bookauthor."&bookpub=".$bookpub."&bookisbn=".$bookisbn."'>Add to basket</a>";
echo "</td>";
echo "<td>";
echo $bookname;
echo "</td>";
echo "<td>";
echo $bookauthor;
echo "</td>";
echo "<td>";
echo $bookpub;
echo "</td>";
echo "<td>";
echo $bookisbn;
echo "</td>";
echo "</tr>";
}
?>
</table>
<?php
echo "<br />";
echo $navLinks; ?>
請注意我是一個真正的初學者,所以我沒有學習如何製作安全或安全的代碼。..我甚至還沒有加密用戶和管理員密碼......只是想學習腳本的機制!
再次感謝你花時間閱讀我的「風」問題!
我讀過上面的評論,只是......解決了它,但你確認了它!謝謝你的精彩和快速的回應!並且感謝您對初學者的友善......很多Q&A論壇的人都不太瞭解。 [= – Phil 2012-02-24 23:34:16
很高興你解決了它。感謝您的評論! – 2012-02-24 23:35:43
對不起,訂單位仍然無法正常工作,但是當我選擇一個字段進行重新排序時,它會返回到刪除searchterm以顯示所有產品!你有什麼想法嗎?謝謝 – Phil 2012-02-24 23:50:22