Originally posted by Nate
Copy over the code in my original post again, that should fix those 2 errors. I can understand if you just want to keep your error logs clean, but don't expect this to fix your script if its broke, as those were just notices and shouldnt really change anything. 🙂
Nate, I'm staying at this and with your tips and suggestions, I've managed to not only solve many of the bugs that have plagued me, but I've even tweaked the code a bit to work in some things such as alternating row colors. I switched to _GET and now am able to pass the proper variables from page to page. Nice.
However I have a few remaining issues.
1) When my search returns only 1 hit, the word Previous is NOT linked correctly, but the word Previous is linking. When I click it I get zero results which I would expect. However I thought the code was setup to not link the word "next" unless there were additional pages.
2) I used the piece of code you provided to put the following information above the results:
Displaying results 0 - 20 of 813
This works great when I search for a large amount of records... however when I return one result or five results, could it say "Displaying results 1 of 813" ? or 5 of 813? (813 is the total amount of records in my database.)
I'm including here the current searchresults.php file. The class file is unchanged and exactly what you have in your (updated) original post.
<?php
include("conf.php");
mysql_connect('localhost',$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
include ("pages.class.php");
/* Instantiate the paging class! */
$paging = new PagedResults();
/*tell it how many results to page through */
$sql1 = "SELECT COUNT(*) as Total FROM Accounts";
$query = mysql_query($sql1);
$data = mysql_fetch_array($query);
$paging->TotalResults = $data['Total'];
$InfoArray = $paging->InfoArray();
/* The following two variables are being fed from a search page */
$username_query=$_GET['username_form'];
$lastname_query=$_GET['lastname_form'];
$sql2 = "select * from Accounts where UserName like '$username_query%' AND Last like '$lastname_query%' order by Last LIMIT ".$InfoArray["MYSQL_LIMIT1"].", ".$InfoArray["MYSQL_LIMIT2"];
$query2 = mysql_query($sql2);
$num=mysql_numrows($query2);
mysql_close();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/2000/REC-xhtml1-20000126/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Email - Search Results</title>
<link href="style2.css" rel="stylesheet" type="text/css" media="all" />
</head>
<body>
<center>
<div class="content">
<?php echo "Displaying results " . $InfoArray["START_OFFSET"] . " - " . $InfoArray["END_OFFSET"] . " of " . $InfoArray["TOTAL_RESULTS"] . "<BR>"; ?>
<table border="0" cellspacing="1" cellpadding="2" width="100%">
<tr bgcolor="#000000">
<td class="headertext" align="left">Last</td>
<td class="headertext" align="left">First</td>
<td class="headertext" align="left">Username</td>
<td class="headertext" align="left">Location</td>
<td class="headertext" align="left">E-mail</td>
<td class="headertext" align="left">Edit</td>
</tr>
<?php
$i=0;
while($data = mysql_fetch_array($query2)) {
while ($i < $num) {
$aid= mysql_result($query2,$i,"aid");
$First=mysql_result($query2,$i,"First");
$Last=mysql_result($query2,$i,"Last");
$UserName=mysql_result($query2,$i,"UserName");
$Location=mysql_result($query2,$i,"Location");
$Domain=mysql_result($query2,$i,"Domain");
//the following three lines are used to alternate the color of rows.
$color1 = "#DCDCDC";
$color2 = "#FFFFFF";
$row_color = ($i % 2) ? $color1 : $color2;
?>
<tr bgcolor="<?php echo "$row_color"; ?>">
<td align="left"><?php echo "$Last"; ?></td>
<td align="left"><?php echo "$First"; ?></td>
<td align="left"><?php echo "$UserName"; ?></td>
<td align="left"><?php echo "$Location"; ?></td>
<td align="left"><a href="mailto:<?php echo "$UserName$Domain"; ?>">E-mail</a></td>
<td align="left"><a href="update.php?aid=<?php echo "$aid"; ?>">Edit</a></td>
</tr>
<?php
++$i;
}
}
echo "</table>";
/* Print out our prev link */
if($InfoArray["PREV_PAGE"]) {
echo "<a href='?page=" . $InfoArray["PREV_PAGE"] . "&username_form=".$username_query. "&lastname_form=".$lastname_query."'>Previous</a>";
} else {
echo "Previous";
}
echo " | ";
/* Print out our next link */
if($InfoArray["NEXT_PAGE"]) {
echo " <a href='?page=" . $InfoArray["NEXT_PAGE"] . "&username_form=".$username_query. "&lastname_form=".$lastname_query."'>Next</a>";
} else {
echo "Next";
}
?>
</div>
</center>
</body>
</html>