Hello I want to know how to add pagination in search engine. I have tried so many times to add pagination in search engine but every time i was fail. Is there anybody who modify my following code to add pagination:


$itemsPerPage = 10;


$var = mysql_real_escape_string(@$_REQUEST['q']);


$s = mysql_real_escape_string($_REQUEST['s']);


if(strlen($var) < 3){
$resultmsg = "<p><div class=\"searchText\">Search Results</div><br><div class=\"searchText2\"><p>Search Error</p><p>Keywords with less then three characters are omitted...</p></div>" ;
}



$trimmed = trim($var);
$trimmed1 = trim($var);

$trimmed_array = explode(" ",$trimmed);
$trimmed_array1 = explode(" ",$trimmed1);


if ($trimmed == "") {
$resultmsg = "<p>Please enter a search...</p>" ;
}


if (!isset($var)){
$resultmsg = "<p><div class=\"searchText\">Search Results</div><br><p>Search Error</p><p>We don't seem to have a search parameter! </p>" ;
}


foreach ($trimmed_array as $trimm){

$query = "SELECT * , MATCH (id, title) AGAINST ('".$trimm."') AS score FROM articles WHERE MATCH (id, title) AGAINST ('+".$trimm."') ORDER BY score DESC";

$numresults=mysql_query ($query);
$row_num_links_main =mysql_num_rows ($numresults);


if($row_num_links_main < 1){
$query = "SELECT * FROM articles WHERE text LIKE '%$trimm%' OR text LIKE '%$trimm%' ORDER BY id DESC";
$numresults=mysql_query ($query);
$row_num_links_main1 =mysql_num_rows ($numresults);
}


if (empty($s)) {
$s=0;
}


$query .= " LIMIT $s,$itemsPerPage" ;
$numresults = mysql_query ($query) or die ( "Couldn't execute query" );
$row= mysql_fetch_array ($numresults);




do{
$adid_array[] = $row[ 'id' ];
}while( $row= mysql_fetch_array($numresults));
}




if($row_num_links_main == 0 && $row_num_links_main1 == 0){
$resultmsg = "<p><div class=\"searchText\">Search Results</div><br><div class=\"searchText2\">Sorry, there are no matching result for <b><span><strong>''". $trimmed."''</strong></span></b>.</br></br></div><div class=\"searchText2\">1.
Try more general words.</br>2. Try different words with similar
meaning.</br>3. Please check your spelling.</br></br></div>" ;

}


$tmparr = array_unique($adid_array);
$i=0;
foreach ($tmparr as $v) {
$newarr[$i] = $v;
$i++;
}


$row_num_links_main = $row_num_links_main + $row_num_links_main1;


if( isset ($resultmsg)){
echo $resultmsg;
}else{
echo "<p><div class=\"searchText\">Search Results</div><br>";
echo "<div class=\"searchText2\">SHOWING RESULTS FOR:
<span><strong>''".$var."''</strong></span></div>";
echo "<div class=\"searchText2\">Your search produced <strong>" .$row_num_links_main."</strong> results !</div></p><div class=\"searchText2under\"></div>";


$count = 1 + $s ;


foreach($newarr as $value){


$query_value = "SELECT * FROM articles WHERE id = '".$value."'";
$num_value=mysql_query ($query_value);
$row_linkcat= mysql_fetch_array ($num_value);
$row_num_links= mysql_num_rows ($num_value);


$introcontent = strip_tags($row_linkcat[ 'description']);
$introcontent = substr($introcontent, 0, 130)."...";


$title = preg_replace ( "'($var)'si" , "<strong>\\1</strong>" , $row_linkcat[ 'title' ] );
$desc = preg_replace ( "'($var)'si" , "<strong>\\1</strong>" , $introcontent);
$url = preg_replace ( "'($var)'si" , "<strong>\\1</strong>" , $row_linkcat[ 'url' ] );

foreach($trimmed_array as $trimm){
if($trimm != 'b' ){
$title = preg_replace( "'($trimm)'si" , "<strong>\\1</strong>" , $title);
$desc = preg_replace( "'($trimm)'si" , "<strong>\\1</strong>" , $desc);
$url = preg_replace( "'($trimm)'si" , "<strong>\\1</strong>" , $url);
}
}



echo '<div class="selectorClass search-result" >';
echo "<div class='title'><a href='$url'>$count.)&nbsp;".$title."</a></div>";
echo '<div class="url">';
echo "<a href='$url'><b>$url</b></a>";
echo '</div>';
echo '<div class="desc">';
echo $desc;
echo '</div>';
echo '</div><br>';
$count++ ;

}
}



$a = $s + ($itemsPerPage) ;
if ($a > $row_num_links_main) { $a = $row_num_links_main; }
$b = $s + 1 ;
echo "<p>Showing results $b to $a of $row_num_links_main</p>";



if (isset($_GET['var'])) {
$var = preg_replace('#[^0-9]#i', '', $_GET['var']);

} else {
$var = 1;
}

$itemsPerPage = 10;

$lastPage = ceil($row_num_links_main / $itemsPerPage);

if ($var < 1) {
$var = 1;
} else if ($var > $lastPage) {
$var = $lastPage;
}


$centerPages = "";
$sub1 = $var - 1;
$sub2 = $var - 2;
$add1 = $var + 1;
$add2 = $var + 2;
if ($var == 1) {
$centerPages .= '&nbsp; <span class="pagNumActive">' . $var . '</span> &nbsp;';
$centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?var=' . $add1 . '">' . $add1 . '</a> &nbsp;';
} else if ($var == $lastPage) {
$centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?var=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
$centerPages .= '&nbsp; <span class="pagNumActive">' . $var . '</span> &nbsp;';
} else if ($var > 2 && $var < ($lastPage - 1)) {
$centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?var=' . $sub2 . '">' . $sub2 . '</a> &nbsp;';
$centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?var=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
$centerPages .= '&nbsp; <span class="pagNumActive">' . $var . '</span> &nbsp;';
$centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?var=' . $add1 . '">' . $add1 . '</a> &nbsp;';
$centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?var=' . $add2 . '">' . $add2 . '</a> &nbsp;';
} else if ($var > 1 && $var < $lastPage) {
$centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?var=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
$centerPages .= '&nbsp; <span class="pagNumActive">' . $var . '</span> &nbsp;';
$centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?var=' . $add1 . '">' . $add1 . '</a> &nbsp;';
}

$paginationDisplay = "";

if ($lastPage != "1"){

$paginationDisplay .= 'Page <strong>' . $var . '</strong> of ' . $lastPage. '&nbsp; &nbsp; &nbsp; ';

if ($var != 1) {
$previous = $var - 1;
$paginationDisplay .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?var=' . $previous . '"> Back</a> ';
}

$paginationDisplay .= '<span class="paginationNumbers">' . $centerPages . '</span>';

if ($var != $lastPage) {
$nextPage = $var + 1;
$paginationDisplay .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?var=' . $nextPage . '"> Next</a> ';
}
}