Pagination need help again
| From: | Scott McWhite | Date: | Sat, 13 Nov 2004 14:26:46 +0000 |
| Subject: | Pagination need help again | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-201914@lists.php.net to get a copy of this message | ||
Sorry,
Its me again. I tried all the recommendations from my first responses and
could not find the proper syntax on how to use it in my file. My original
email question,error, and my php code is listed below. Thank you in advance
for all your help, this is a great community.
Original Question? Previous email subject: Pagination need help.
Im using an HTML search form that passes the searchterm to a php file.
In my case the searchterm can have 1000s of records in my database, so I
implemented a limit which displays 20 records per page. The pagination
function works fine with one exception. Example of my problem: searchterm
typed in the html search form equals ford, in the database there are 2000
fords, the first page displays 20 fords and then the next page forgets that
we are searching fords and displays the equivalent to a wildcard %
search and displays accordingly.
Does anyone have sample code for using an HTML search form with pagination?
Generally what Im trying to do is type a searchterm on the html search
page, have the php file run the query and if it is an item that has many
results to display it 20 records per page without it displaying every record
in the database.
Error.
Notice: Undefined index: searchterm in
C:\Inetpub\cars\search\my_php_file.php on line 2
Notice: Undefined index: searchterm1 in
C:\Inetpub\cars\search\my_php_file.php on line 3
Notice: Undefined index: searchterm2 in
C:\Inetpub\cars\search\my_php_file.php on line 4
My php file. Attached also if not displayed in email properly.
<?php
$searchterm=$_POST['searchterm'];
$searchterm1=$_POST['searchterm1'];
$searchterm2=$_POST['searchterm2'];
$searchterm = addslashes($searchterm);
$searchterm1 = addslashes($searchterm1);
$searchterm2 = addslashes($searchterm2);
@mysql_pconnect("localhost", "root", "") or die("error --
can't connectto
server");
@mysql_select_db("list") or die ("error -- can't connect to DB");
if(!isset($_GET['page'])){
$page = 1;
} else {
$page = $_GET['page'];
}
// Define the number of results per page
$max_results = 20;
// Figure out the limit for the query based
// on the current page number.
$from = (($page * $max_results) - $max_results);
// Perform MySQL query on only the current page number's results
$sql = "select * from parts where ".'model'." like
'%".$searchterm."%' AND
".'seller'." like '%".$searchterm1."%' AND
".'description'." like
'%".$searchterm2."%' ORDER BY 'model', 'price',
'quantity' LIMIT $from,
$max_results";
echo '<body bgcolor="#dfefff">';
$result = mysql_query($sql) or die(mysql_error());
$numofrows = mysql_num_rows($result);
echo "<TABLE BORDER=\"0\">\n";
echo "<TR bgcolor=\"3366cc\"><TD align=left><font
color=\"FFFFFF\"
face=\"Verdana\"
size=\"1\"><b>Seller</TD></font></b></TD><TD
align=left><font color=\"FFFFFF\" face=\"Verdana\"
size=\"1\"><b>Model
Number</TD></font></b><TD align=left><font color=\"FFFFFF\"
face=\"Verdana\"
size=\"1\"><b>Manufacturer</TD></font></b><TD
align=left><font
color=\"FFFFFF\" face=\"Verdana\"
size=\"1\"><b>Description</TD></font></b><TD
align=left><font
color=\"FFFFFF\" face=\"Verdana\"
size=\"1\"><b>Quantity</TD></font></b><TD
align=left><font color=\"FFFFFF\" face=\"Verdana\"
size=\"1\"><b>Price</TD></font></b><TD
align=left><font color=\"FFFFFF\"
face=\"Verdana\"
size=\"1\"><b>Condition</TD></font></b><TD
align=left><font
color=\"FFFFFF\" face=\"Verdana\" size=\"1\"><b>Email
Address</TD></font></b><TD align=left><font color=\"FFFFFF\"
face=\"Verdana\"
size=\"1\"><b>AIM</TD></font></b></TR>\n";
for($i = 0; $i < $numofrows; $i++) {
$row = mysql_fetch_array($result); //get a row from our result set
if($i % 2) { //this means if there is a remainder
echo "<TR bgcolor=\"eaeaea\">\n";
} else { //if there isn't a remainder we will do the else
echo "<TR bgcolor=\"d5eaff\">\n";
}
echo "<TD align=left><font color=\"000080\" face=\"Verdana\"
size=\"1\"><b>".$row['seller']."</a></TD></font></b></a><TD
align=left><font
color=\"000080\" face=\"Verdana\"
size=\"1\"><b>".$row['model']."</TD></font></b><TD
align=left><font
color=\"000080\" face=\"Verdana\"
size=\"1\"><b>".$row['manufacturer']."</TD></font></b><TD
align=left><font
color=\"000080\" face=\"Verdana\"
size=\"1\"><b>".$row['description']."</TD></font></b><TD
align=left><font
color=\"000080\" face=\"Verdana\"
size=\"1\"><b>".$row['quantity']."</TD></font></b><TD
align=left><font
color=\"000080\" face=\"Verdana\"
size=\"1\"><b>".$row['price']."</TD></font></b><TD
align=left><font
color=\"000080\" face=\"Verdana\"
size=\"1\"><b>".$row['condition']."</TD></font></b><TD
align=left><a
href=\"mailto:".$row['email']."\"><font
color=\"000080\" face=\"Verdana\"
size=\"1\"><b>".$row['email']."</TD></font></b><TD
align=left><a
href=\"aim:goim?message=Hi.+Are+you+there?&screenname=".$row['aim'].."\"><fon
t color=\"000080\" face=\"Verdana\" size=\"1\"><b>AIM
Link</TD></font></a>\n";
echo "</TR>\n";
}
//now let's close the table and be done with it
echo "</TABLE>\n";
while($row = mysql_fetch_array($result)){
// Build your formatted results here.
$variable1=$row["manufacturer"];
$variable2=$row["seller"];
$variable3=$row["model"];
$variable4=$row["description"];
$variable5=$row["email"];
//results
print ("<tr>");
print ("<td class='body_1'>$variable1</td>");
print ("<td class='body_1'>$variable2</td>");
print ("<td class='body_1'>$variable3</td>");
print ("<td class='body_1'>$variable4</td>");
print ("<td class='body_1' style='border-right:
0px;'>$variable5</td>");
print ("</tr>");
}
// Figure out the total number of results in DB:
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM parts
where ".'model'." like '%".$searchterm."%' AND
".'seller'." like
'%".$searchterm1."%' AND ".'description'." like
'%".$searchterm2."%'"),0);
// Figure out the total number of pages. Always round up using ceil()
$total_pages = ceil($total_results / $max_results);
// Page Number Hyperlinks
echo "<tr><td colspan=4 align=center><font color=\"000080\"
face=\"verdana\"
size=\"1\">Select a Page</font><br />";
// Previous Link
if($page > 1){
$prev = ($page - 1);
echo "<a
href=\"".$_SERVER['PHP_SELF']."?page=$prev\"><<Previous</a> ";
}
for($i = 1; $i <= $total_pages; $i++){
if(($page) == $i){
echo "<strong><font color=#FF0000>$i </font></strong>";
} else {
echo "<a
href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> ";
}
if(($i + 30) % 30 == 0)
{
echo '</td></tr><tr><td colspan=4 align=center>';
}
}
// Next Link
if($page < $total_pages){
$next = ($page + 1);
echo "<a
href=\"".$_SERVER['PHP_SELF']."?page=$next\">Next>></a>";
}
echo "</center>";
?>
Thank you,
Scott