Pagination of search results, help!

I have a php file that show the results of three queries, is for a directory, one shows the name alphabetically if you click a letter it shows names started with that letter, the other is a drop down menu and the last is a search field.

I need to paginate the results. I have pagination on the main page but I’m trying to do the same on the results one but Its not working, this is the code:

[php]

<?php require_once("connect.php");

$count = 0;

$bigltr="images/letterA.png";

if(isset($_GET['ltr'])) {
	$qstring="SELECT alum_id,alum_fname,alum_photo,alum_lname,alum_title,alum_company,alum_program,alum_gradyear FROM tbl_alumni WHERE alum_lname LIKE '".$_GET['ltr']. "%' ORDER BY alum_lname ASC"; // letter search: LIKE is the one that finds words that star with the letter declared by 'ltr' (abcd....)
	$bigltr="images/letter".$_GET['ltr'].".png";	//this variable 'ltr' calls the images of the letters to use it on the qstring
}/*else{
	$qstring="SELECT alum_id,alum_fname,alum_photo,alum_lname,alum_title,alum_company,alum_program,alum_gradyear FROM tbl_alumni ORDER BY alum_lname ASC";	 //these are the elements that are called from the database
}*/

$results = mysql_query($qstring);

$rowsperpage = 6;
if(isset($_GET[‘page’])) {
$pagenum = $_GET[‘page’];
}else{
$pagenum=1;
}

$offset = ($pagenum - 1) * $rowsperpage;

$totalnumltr = mysql_query(“SELECT alum_id FROM tbl_alumni WHERE alum_gradyear=’”.$_GET[‘ltr’]."’ ORDER BY alum_lname ASC LIMIT");

$totalnumgrad = mysql_query(“SELECT alum_id FROM tbl_alumni WHERE alum_lname LIKE '”.$_GET[‘gradyear’]. “%’ ORDER BY alum_lname ASC LIMIT”);

$totalnumsrch = mysql_query(“SELECT alum_id FROM tbl_alumni WHERE alum_lname LIKE '%”.$_GET[‘searchbox’]."%’ OR alum_fname LIKE ‘%".$_GET[‘searchbox’]."%’ OR alum_title LIKE ‘%".$_GET[‘searchbox’]."%’ OR alum_program LIKE ‘%".$_GET[‘searchbox’]."%’ ORDER BY alum_lname ASC LIMIT");

$totalall = mysql_query(“SELECT alum_id,alum_fname,alum_photo,alum_lname,alum_title,alum_company,alum_program,alum_gradyear FROM tbl_alumni WHERE alum_lname ORDER BY alum_lname ASC”);

$total = mysql_num_rows($totalnum);
$maxpage = ceil($total/$rowsperpage);
$self = $_SERVER[‘PHP_SELF’];

if (isset($_GET[‘ltr’])){
$qstring = “if SELECT alum_id,alum_fname,alum_photo,alum_lname,alum_title,alum_company,alum_program,alum_gradyear FROM tbl_alumni WHERE alum_lname LIKE '”.$_GET[‘ltr’]. “%’ ORDER BY alum_lname ASC LIMIT”.$offset.",".$rowsperpage;
}elseif(isset($_GET[‘gradyear’])){
$qstring = “SELECT alum_id,alum_fname,alum_photo,alum_lname,alum_title,alum_company,alum_program,alum_gradyear FROM tbl_alumni WHERE alum_gradyear=’”.$_GET[‘gradyear’]."’ ORDER BY alum_lname ASC LIMIT “.$offset.”,".$rowsperpage;
}elseif(isset($_GET[‘searchbox’])){
$qstring = “SELECT alum_id,alum_fname,alum_photo,alum_lname,alum_title,alum_company,alum_program,alum_gradyear FROM tbl_alumni WHERE alum_lname LIKE '%”.$_GET[‘searchbox’]."%’ OR alum_fname LIKE ‘%".$_GET[‘searchbox’]."%’ OR alum_title LIKE ‘%".$_GET[‘searchbox’]."%’ OR alum_program LIKE ‘%".$_GET[‘searchbox’]."%’ ORDER BY alum_lname ASC LIMIT “.$offset.”,".$rowsperpage;
}else{
$qstring=“SELECT alum_id,alum_fname,alum_photo,alum_lname,alum_title,alum_company,alum_program,alum_gradyear FROM tbl_alumni WHERE alum_lname ORDER BY alum_lname ASC”;}

$results = mysql_query($qstring);

?>

Contemporary Media - Alumni Profile
names letter A
    <div id="nav">
                <div id="search">
                    <form class="margTop" name="searchbox" id="searchbox" action="results.php" method="get">
                        <p>Search: <input type="text" name="user" size="18" class="formbox1" />
                        <input type="submit" value="Submit" class="button"/></p>
                    </form> 
                </div><!--end of search --> 
                
                <div id= "yrSearch">
                    <form class="margTop2" method="get" action="results.php">
                        <p>Graduation Year: <select name="gradyear" id="gradyear">
                        <option value="1998">1998</option>
                        <option value="1999">1999</option>
                        <option value="2000" selected="selected">2000</option>
                        <option value="2002">2001</option>
                        <option value="2002">2002</option>
                        <option value="2003">2003</option>
                        <option value="2004">2004</option>
                        <option value="2005">2005</option>
                        <option value="2006">2006</option>
                        <option value="2007">2007</option>
                        <option value="2008">2008</option>
                        <option value="2009">2009</option>
                        <option value="2010">2010</option>
                        <option value="2011">2011</option>
                        <option value="2012">2012</option>
                        </select>
                        <input type="submit" value="Submit" class="button"/></p>
                        </form>
                </div><!--end of yrsearch--> 
                <div id="abc">
                        <ul class="abcM">
                        <li><a href="alumni_list.php?ltr=A">A</a></li>
                        <li><a href="alumni_list.php?ltr=B">B</a></li>
                        <li><a href="alumni_list.php?ltr=C">C</a></li>
                        <li><a href="alumni_list.php?ltr=D">D</a></li>
                        <li><a href="alumni_list.php?ltr=E">E</a></li>
                        <li><a href="alumni_list.php?ltr=F">F</a></li>
                        <li><a href="alumni_list.php?ltr=G">G</a></li>
                        <li><a href="alumni_list.php?ltr=H">H</a></li>
                        <li><a href="alumni_list.php?ltr=I">I</a></li>
                        <li><a href="alumni_list.php?ltr=J">J</a></li>
                        <li><a href="alumni_list.php?ltr=K">K</a></li>
                        <li><a href="alumni_list.php?ltr=L">L</a></li>
                        <li><a href="alumni_list.php?ltr=M">M</a></li>
                        <li><a href="alumni_list.php?ltr=N">N</a></li>
                        <li><a href="alumni_list.php?ltr=O">O</a></li>
                        <li><a href="alumni_list.php?ltr=P">P</a></li>
                        <li><a href="alumni_list.php?ltr=Q">Q</a></li>
                        <li><a href="alumni_list.php?ltr=R">R</a></li>
                        <li><a href="alumni_list.php?ltr=S">S</a></li>
                        <li><a href="alumni_list.php?ltr=T">T</a></li>
                        <li><a href="alumni_list.php?ltr=U">U</a></li>
                        <li><a href="alumni_list.php?ltr=V">V</a></li>
                        <li><a href="alumni_list.php?ltr=W">W</a></li>
                        <li><a href="alumni_list.php?ltr=X">X</a></li>
                        <li><a href="alumni_list.php?ltr=Y">Y</a></li>
                        <li><a href="alumni_list.php?ltr=Z">Z</a></li>
                     </ul>
                </div><!--end of abc --> 
            </div><!--end of nav -->
<?php $count = 0; $results = mysql_query($qstring); //this calls the variable $qstring and then depending on the selection, it could give results by using the gradyear drop down menu or by putting a query on the search field. while($row=mysql_fetch_array($results)) { //results is set to show all the people in an alphabetical order echo "
"; echo ""; echo "".""; echo "

".""; echo "".$row['alum_fname']." ".$row['alum_lname']."

"; echo "

"; echo "".$row['alum_title']."

"; echo "

"; echo "".$row['alum_company']."

"; echo "

"; echo "".$row['alum_program']." ".$row['alum_gradyear']."

"; echo "
"; } if($total > $rowsperpage) { if ($pagenum > 1) { $page = $pagenum - 1; $prev = ""; $first = ""; }else{ $prev = "    "; $first = "  "; } if ($pagenum < $maxpage) { $page = $pagenum + 1; $next = ""; $last = ""; }else{ $next = "    "; $last = "  "; } echo "".$first."  ".$prev."  Page ".$pagenum." of ".$maxpage." Pages  ".$next."  ".$last.""; } ?>

[/php]

Please could someone give me some hints on how to make this work, thanks!!!

Hi,

Have you tried working with ‘limit’ in the query ?

LIMIT $offset, $length

In the link that takes the user to the next resultlist you put the new offset, build the query, run it again and serve the right set of results back to the user.

Hope it helps,
O.

( Oh, btw… you use this for constructing the SQL, it’s not PHP )

Sponsor our Newsletter | Privacy Policy | Terms of Service