Hi! Alright, here's the deal. I'm trying to display a list of data from a table (reflist) in the database. I'm trying to set it up so there are 50 rows listed, then the option to go to the next page, etc. Everything is working EXCEPT for the "Next" and "Previous" functions. If you click on those (or on the various available page numbers), it just continues to display the first 50 rows.
Any help would be very much appreciated. I've looked at this for about 4 hours now and cannot figure it out.
Thanks in advance!
<?
//set up table and database names
require($_SERVER['DOCUMENT_ROOT'] . '/include/reflist.php');
// set number of results to display per page
$pagelimit = "50";
// run query (change yourtable to the name of your table)
$strSQL = mysql_query("SELECT * FROM $table_name WHERE reg_l_name LIKE '$_SESSION[search_for_referee]%' ORDER BY reg_l_name");
// count number of matches
$totalrows = mysql_num_rows($strSQL);
// determine how many pages there will be by using ceil() and dividing total rows by pagelimit
$pagenums = ceil ($totalrows/$pagelimit);
// if no value for page, page = 1
if ($page==''){
$page='1';
}
// create a start value
$start = ($page-1) * $pagelimit;
// blank matches found
echo "<b>" . $totalrows . " matches found</b><br>\n";
// Showing Results 1 to 1 (or if you're page limit were 5) 1 to 5, etc.
$starting_no = $start + 1;
if ($totalrows - $start < $pagelimit) {
$end_count = $totalrows;
} elseif ($totalrows - $start >= $pagelimit) {
$end_count = $start + $pagelimit;
}
echo "Results $starting_no to $end_count shown.<br>\n";
// create dynamic next, previous, and page links
/* lets say you're set to show 5 results per page and your script comes out with 7 results.
this will allow your script to say next2 if you're on the first page and previous5 if you're on the second page. */
if ($totalrows - $end_count > $pagelimit) {
$var2 = $pagelimit;
} elseif ($totalrows - $end_count <= $pagelimit) {
$var2 = $totalrows - $end_count;
}
$space = " ";
// dynamic page number links (make sure to change yourpage.php to the name of your page)
for ($i=1; $i<=$pagenums; $i++) {
if ($i!=$page) {
echo " <a href='do_find_info_l_name.php?page=$i' class=main>$i</a>";
}
else {
echo " <b class='red'>$i</b>";
}
}
// previous link (make sure to change yourpage.php to the name of your page)
if ($page>1) {
echo "<br>« <a href='do_find_info_l_name.php?page=".($page-1)."' class=main>Previous" . $space . $pagelimit . "</a>" . $space . $space . "";
}
// next link (make sure to change yourpage.php to the name of your page)
if ($page<$pagenums) {
echo "" . $space . $space . $space . $space . " <a href='do_find_info_l_name.php?page=".($page+1)."' class=main>Next " . $var2 . "</a> »";
}
/* output your data wherever you'd like.
BUT
in order for this all to work, before outputting your data, you have to run the query over using MySQL's LIMIT. This will limit how many results are actually displayed on the page. */
$query = "SELECT reg_id, reg_ussf_id, reg_l_name, reg_f_name, reg_m_name, trim(concat(reg_f_name, ' ', reg_m_name, ' ' ,reg_l_name)) as referee_name,
reg_address1, reg_address2, reg_city, reg_state, reg_zip, reg_email, reg_home_ph, reg_work_ph, reg_district, reg_currentussf
FROM $table_name WHERE reg_l_name LIKE '$_SESSION[search_for_referee]%' ORDER BY reg_l_name LIMIT $start,$pagelimit";
$result = @mysql_query($query,$connection)or die(mysql_error());
while($r=mysql_fetch_array($result)) /*this causes the loop to be ran until the query is empty*/
{
$reg_id = $r["reg_id"];
$reg_ussf_id = $r["reg_ussf_id"];
$reg_l_name = $r["reg_l_name"];
$reg_f_name = $r["reg_f_name"];
$reg_m_name = $r["reg_m_name"];
$referee_name = $r["referee_name"];
$reg_address1 = $r["reg_address1"];
$reg_address2 = $r["reg_address2"];
$reg_city = $r["reg_city"];
$reg_state = $r["reg_state"];
$reg_zip = $r["reg_zip"];
$reg_email = $r["reg_email"];
$reg_home_ph = $r["reg_home_ph"];
$reg_work_ph = $r["reg_work_ph"];
$reg_district = $r["reg_district"];
$reg_currentussf = $r["reg_currentussf"];
$display_block .= "
<table cellspacing=0 cellpadding=0 border=0 bordercolor=000000>
<tr>
<td valign=top width=175>
<center><font size=1>$referee_name</font></center>
</td>
<td valign=top width=150>
<center><font size=1>$reg_ussf_id</font></center>
</td>
<td valign=top width=75>
<font size=1><center><a href=\"/admin/manage_db_edit_referee.php?id=$reg_id\">Edit</a></center></font>
</td>
<td valign=top width=75>
<font size=1><center><a href=\"/admin/manage_db_delete_referee.php?id=$reg_id\">Delete</a></center></font>
</td>
<td valign=top width=50>
<font size=1><center><a href=\"/admin/show_view_info_referee.php?id=$reg_id\" target=\"new\">View</a></center></font>
</td>
</tr>
</table>
";
}
// LIMIT 0,10 will start at 0 and display 10 results
// LIMIT 10,5 will start at 10 and display 5 results
/* now you can do whatever you'd like with this query. it will only output one page at a time. change the $pagelimit variable to whatever to output more than 1 result per page. */
?>