Right, day 8 learning php and I feel that I have tried to do things in the wrong order…. I was fine up until yesterday. I have set myselft the task of creating a fake online book shop (seems popuar with all learn php books). First I created my products and listed them all on a page from the database.. easy, then I used a dropdown box to order the results… hard but did it eventually. Then tried to paginate, easy, then tried to join my ordering code and pagination code: most painful few hours of my life but complete. The I utilised a search bar.. easier then thought, an now I am trying to mix this search bar with my pagination and ordering code and my hair is falling out by the handful! I really wish I had attempted to learn all three at once but would have been a bit of a handful/headful.
So my problem is… It paginates… yay, it orders… yay however when I first see the results from a search… It paginates ok (shows me how many pages there are which is always the correct value) however when I click to view the 2nd page it the stops showing me the results of the search, instead takes me to page 2 of ALL products in the database. The same also happens if I try and order the list, it changes to an ordered list of all the products and tells me the search criteria is empty!
Sorry for such a long winded question, I just cant work out where I have slipped up!
Thank you soo much for anybody who can help!
Heres the code:
booksearch.php:
(have taken out all the irrelevent code!)
<?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";
}
?>
(IRRELEVANT STUFF FROM MIDDLE REMOVED)
<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; ?>
Please note I am a real beginner so I am not learning how to make secure or safe code.,.. I havnt even encrypted user and admin passwords yet… just trying to learn the mechanics of the script!
Thank you all again for taking the time to read my “windy” question!
To all who are reading this: I’m keeping it SIMPLE, as Phil has commented he’s a beginner. We all know there are considerations that are NOT being taken into account in this answer!
Phil, the answer lies in the construction of your navlinks code.
When you click one of the navlinks, it passes the limits for your pagination, but does NOT pass the search term.
You would need to modify your navlinks to be along the lines of:
and
and
etc.