mysql - pagination in php and keeping results -
have tried implement pagination script in php code (adopted sample code found on net) page returns resaults of mysql query. default empty 'keyword' returns info databse. succeeded far pagination works when info returned when filter results through keyword search clicking 'next page' returns next page in unfiltered results (ie searched keyword lost in refresh)
this relevant code (hope makes sense):
blockquote
$txt1='<a href="/memimomedia/music/128kb/'; $txt2='" ><img src="/save.png" width="24" height="24" align="middle" border="0"></a>'; $txt9a='<a href="'; $txt9b='" target="_blank">'; $txt9c='</a>'; $txt10a='<audio src="./128kb/'; $txt10d='" controls></audio>'; if(empty($_post['searchkeywords']) && empty($_get['searchkeywords'])) { $result = mysql_query("select *
from ppl_tracks mixed !='0' order track"); } if(!empty($_get['searchkeywords'])) { $searchword=$_get['searchkeywords']; }
if(!empty($_post['searchkeywords'])) { $searchword=$_post['searchkeywords']; }
$totalresults = mysql_query("select track (ppl_tracks left bring together trackstyle on ppl_tracks.recno = trackstyle.trackid left bring together styletable on trackstyle.styleid = styletable.id) left bring together trackmood on ppl_tracks.recno = trackmood.trackid left bring together moodtable on trackmood.moodid = moodtable.id (moodchoices '%$searchword%' or description '%$searchword%' or stylechoices '%$searchword%' or tempo '%$searchword%' or track '%$searchword%' ) , mixed =true grouping track order track" );
//this checks see if there page number. if not, set page 1 if(isset($_get['pagenum'])) { $pagenum = $_get['pagenum']; } else { $pagenum = 1; }
//here count number of results $hits = mysql_num_rows($totalresults);
//this number of results displayed per page $page_hits = 15;
//this tells page number of our lastly page $last = ceil($hits/$page_hits);
//this makes sure page number isn't below one, or more our maximum pages if ($pagenum < 1) { $pagenum = 1; } elseif ($pagenum > $last) { $pagenum = $last; }
//this sets range display in our query $max = 'limit ' .($pagenum - 1) * $page_hits .',' .$page_hits;
$result = mysql_query("select * (ppl_tracks left bring together trackstyle on ppl_tracks.recno = trackstyle.trackid left bring together styletable on trackstyle.styleid = styletable.id) left bring together trackmood on ppl_tracks.recno = trackmood.trackid left bring together moodtable on trackmood.moodid = moodtable.id (moodchoices '%$searchword%' or description '%$searchword%' or stylechoices '%$searchword%' or tempo '%$searchword%' or track '%$searchword%' ) , mixed =true grouping track order track $max" );
echo " track - artist description download ";
$color="1";
while($row = mysql_fetch_array($result)) { $selectedstyles = array(); $track=$row['track']; $selectedstylesquery = mysql_query(" select stylechoices ppl_tracks left bring together trackstyle on ppl_tracks.recno = trackstyle.trackid left bring together styletable on trackstyle.styleid = styletable.id ppl_tracks.track='$track'"); while($row1 = mysql_fetch_array($selectedstylesquery)) { $selectedstyles[] = $row1[stylechoices]; } $selectedmoods = array(); $selectedmoodsquery = mysql_query(" select moodchoices ppl_tracks left bring together trackmood on ppl_tracks.recno = trackmood.trackid left bring together moodtable on trackmood.moodid = moodtable.id ppl_tracks.track='$track'"); while($row2 = mysql_fetch_array($selectedmoodsquery)) { $selectedmoods[] = $row2[moodchoices]; } sort($selectedmoods); sort($selectedstyles); $strings= implode(", ", $selectedstyles); $stringm= implode(", ", $selectedmoods);
if($color==1) { echo ""; echo "" . $row['track'] . " - " . ucwords($row['artist']) . ""; echo "" . $stringm . "".$strings.""; echo "" . $txt1.$row['filename'].$txt2 . ""; echo "" . $txt10a.$row['filename'].$txt10d.""; echo ""; $color="2"; }
else { echo ""; echo "" . $row['track'] . " - " . ucwords($row['artist']) . ""; echo "" . $stringm . "".$strings.""; echo "" . $txt1.$row['filename'].$txt2 . ""; echo "" . $txt10a.$row['filename'].$txt10d.""; echo ""; $color="1";}
} echo "";
// shows user page on, , total number of pages
echo " --page $pagenum of $last--
"; // first check if on page one. if don't need link previous page or first page nothing. if aren't generate links first page, , previous page.
if ($pagenum == 1)
{
}
else
{ echo " <<-first "; echo " - "; $previous = $pagenum-1; echo " <-previous "; }
$counter=1; while ( $counter <= $last ) { if ($counter==$pagenum) { echo " - "; echo " $counter "; echo " - "; }
else { echo " - "; echo " <a href='{$_server['php_self']}?pagenum=$counter'>$counter</a> "; echo " - "; } $counter = $counter + 1; }
//this same above, checking if on lastly page, , generating next , lastly links
if ($pagenum == $last)
{
}
else { $next = $pagenum+1; echo " next -> "; echo " - "; echo " lastly ->> "; } mysql_close($con); ?>
you reloading page every time go onto new page, need pass search term page again. easiest way update link pagination following:
echo " <a href='{$_server['php_self']}?searchkeywords=" . $_request["searchkeywords"] . "&pagenum=$counter'>$counter</a> ";
$_request handle both $_post , $_get variables, may want in own way. set need pass searchkeywords url
php mysql pagination
No comments:
Post a Comment