Hello to anyone looking at this thread and can help me out. I have found a great set of pagination code (prev/next with 5|10
|20|50) search results from a MySQL database. I have tried to get pretty with the design and tried to code in alternating
table row colors from the results...I have achieved this goal however when I choose to return say 20 or 50 results at a time,
on the last page of my results, I am getting empty color filled rows of "lines" because of my search criteria-50 returns per
page-and only having 8 returns on a second page (58 total rows of data), I get 42 rows of empty, scrunched up lines of color
...
This is ugly and I am tring to figure out a better way to implement alternating table row colors without having these extra
lines of color in them. Could some of you help me on this please?
I know this is hard to describe in text, so I have attached a URL from my web site so you can see it in action.
URL LINK
I have also attached the code for the pagination and alternating row colors here...I have ommitted most of the HTML coding
from the page to save space...Feel free to take this code and alter it for your liking by the way.
I am also trying to figure out how to implement clickable table headers that can sort the MySQL return results by the
different column headings...I have found snippets of code for this too but they are long and arduous. I know that this stems
from the MySQL queries and somehow linking them to the PHP and HTML tags, but where to begin, I am lost too...
This is a secondary goal so if one can help with the first part(alternating row colors within the pagination results) I would
be super happy! Thanks for your time and help!
The code:
<?
$db_addr = 'localhost'; // address of MySQL server.
$db_user = 'blah'; // Username to access server.
$db_pass = 'blah'; // Password access server.
$db_name = 'somedbase'; // Name of database to connect to.
$connect = @mysql_connect("$db_addr", "$db_user", "$db_pass");
if (!($connect)) // If no connect, error and exit().
{
echo("<p>Unable to connect to the database server.</p>");
exit();
}
if (!(@mysql_select_db($db_name))) // If can't connect to database, error and exit().
{
echo("<p>Unable to locate the $db_name database.</p>");
exit();
}
if (!($limit)){
$limit = 10;} // Default results per-page.
if ( !$page or $page < 0 ) { $page = 0; } // Default page value.
$numresults = mysql_query("SELECT * FROM domains"); // the query.
$numrows = mysql_num_rows($numresults); // Number of rows returned from above query.
if ($numrows == 0){
echo("No results found matching your query"); // modify the "Not Found" error for your needs.
exit();}
$pages = intval($numrows/$limit); // Number of results pages.
// $pages now contains int of pages, unless there is a remainder from division.
if ($numrows % $limit) {
$pages++;} // has remainder so add one page
$current = intval($page/$limit) + 1; // Current page number.
if (($pages < 1)) {
$total = 1;} // If $pages is less than one, total pages is 1.
else {
$total = $pages;} // Else total pages is $pages value.
$first = $page + 1; // The first result.
if (!((($page + $limit) / $limit) >= $pages) && $pages != 1) {
$last = $page + $limit;} //If not last results page, last result equals $page plus $limit.
else{
$last = $numrows;} // If last results page, last result equals total number of results.
//escape from PHP mode and go into HTML mode.
?>
<TABLE class=border cellSpacing=0 cellPadding=10 width="100%"
align=center border=0>
<TBODY>
<TR>
<TD width="100%" class=side> <table width="100%" border="0">
<tr>
<td colspan="2" align="left"><font size="1" face="Arial, Helvetica, sans-serif">Results
per-page: <a href="<?=$PHP_SELF?>?query=<?=$query?>&page=<?=$page?>&limit=5">5</a>
| <a href="<?=$PHP_SELF?>?query=<?=$query?>&page=<?=$page?>&limit=10">10</a>
| <a href="<?=$PHP_SELF?>?query=<?=$query?>&page=<?=$page?>&limit=20">20</a>
| <a href="<?=$PHP_SELF?>?query=<?=$query?>&page=<?=$page?>&limit=50">50</a></font><font face
="Arial, Helvetica, sans-serif">
</font></td>
</tr>
<tr>
<td colspan="2" align="left"> </td>
</tr>
<tr>
<td width="50%" align="left" valign="top"><font size="2" face="Arial, Helvetica, sans-serif"
Showing
Results <b>
<?=$first?>
</b> - <b>
<?=$last?>
</b> of <b>
<?=$numrows?>
</b></font> </td>
<td width="50%" align="left" valign="top"> <div align="left"><font size="2" face="Arial,
Helvetica, sans-serif">Page
<b>
<?=$current?>
</b> of <b>
<?=$total?>
</b></font></div></td>
</tr>
<tr>
<td height="21" colspan="2" align="left"> <font face="Arial, Helvetica, sans-serif">
</font> </td>
</tr>
<tr>
<td height="21" colspan="2" align="left">
<?
//un comment this $limitnum next line and the below elseif if you want to return limited pages of searches...say 6 on screen
//at a time
//instead of 300 pages for example...stays pretty on screen.
//$limitnum = 15;
if ($page != 0) { // Don't show back link if current page is first page.
$back_page = $page - $limit;
echo("<a href=\"$PHP_SELF?query=$query&page=$back_page&limit=$limit\">back</a> \n");}
for ($i=1; $i <= $pages; $i++) // loop through each page and give link to it.
{
$ppage = $limit*($i - 1);
if ($ppage == $page){
echo("<b>$i</b>\n");} // If current page don't give link, just text.
//elseif ($ppage < $page - $limitnum || $ppage > $page + $limitnum){}
else{
echo("<a href=\"$PHP_SELF?query=$query&page=$ppage&limit=$limit\">$i</a> \n");}
}
if (!((($page+$limit) / $limit) >= $pages) && $pages != 1) { // If last page don't give next link.
$next_page = $page + $limit;
echo(" <a href=\"$PHP_SELF?query=$query&page=$next_page&limit=$limit\">next</a>");}
?>
</td>
</tr>
</table>
<?
//Go back into PHP mode.
// Now we can display results
$results = mysql_query("SELECT * FROM domains ORDER BY domains ASC LIMIT $page, $limit");
echo "<TABLE width=\"100%\" BORDER=\"0\">\n";
echo "<TR bgcolor=\"#4f5c38\"><TD><font color=\"white\">Domain Name</font></TD><TD><font color=\"white\">Date Submitted</
font></TD><TD><font color=\"white\">Asking Price</font></TD></TR>\n";
//while ($row = mysql_fetch_array ($results))
for($i = 0; $i < $limit; $i++)
{
$row = mysql_fetch_array($results); //get a row from our result set
if($i % 2) { //this means if there is a remainder
echo "<TR bgcolor=\"#c8cfc6\">\n";
} else { //if there isn't a remainder we will do the else
echo "<TR bgcolor=\"#9aa486\">\n";
}
echo "<TD>".$row['domains']."</TD><TD>".$row['domainDate']."</TD><TD>".$row['price']."</TD></TR>";
echo "</TR>";
}
//now let's close the table and be done with it
echo "</TABLE>";
?>