Jump to content

I am trying to make a prev, and next link at bottom of search page?


Collegeboox

Recommended Posts

This is the code I have now however when you click next to view more results it says please enter a search..., (there is more code above this however I thought that the problem would be within the code provided.)

 

$currPage = (($s/$limit) + 1);

 

//break before paging

  echo "<br />";

 

  // next we need to do the links to other results

  if ($s>=1) { // bypass PREV link if s is 0

  $prevs=($s-$limit);

  print " <a href=\"$PHP_SELF?s=$prevs&q=$var\"><<

  Prev 10</a>&nbsp ";

  }

 

// calculate number of pages needing links

  $pages=intval($numrows/$limit);

 

// $pages now contains int of pages needed unless there is a remainder from division

 

  if ($numrows%$limit) {

  // has remainder so add one page

  $pages++;

  }

 

// check to see if last page

  if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {

 

  // not last page so give NEXT link

  $news=$s+$limit;

 

  echo " <a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 >></a>";

  }

 

$a = $s + ($limit) ;

  if ($a > $numrows) { $a = $numrows ; }

  $b = $s + 1 ;

  echo "<p>Showing results $b to $a of $numrows</p>";

 

?>

this is how I put in some paging, I think it looks nice, sort of google like.

Do a search here and you can see it work

http://www.sdowney717.byethost16.com/mysearch2first.php

 



//******************************************************
// how many rows to show per page
$rowsPerPage = 10;


$offset = 0;
// if $_GET['page'] defined, use it as page number, $_GET gets the page number out of the url
//set by the $page_pagination below
if(isset($_GET['page'])){
  $page_num = $_GET['page'];
  $offset = (($page_num * $rowsPerPage )-$rowsPerPage) ; }
else{
  // by default we show first page
  $page_num = 1;}


//the point to start for the limit query

//$offset = $rowsPerPage;
// Zero is an incorrect page, so switch the zero with 1, mainly because it will cause an error with the SQL
if($page_num == 0) {$page_num = 1;}

//$query = "SELECT * FROM bookdata where ". $test2." ORDER BY titles";}

// how many rows we have in database
$sql2  = "SELECT COUNT(*) AS numrows FROM bookdata where $test2";

// do this test because session vars time out echo $test2.'<BR>';
if (trim($test2) ==""){
//if (!$test2 ){
   // close the database connection
   mysql_close($con); 
   //Nothing selected to search for';
   header("Location: /mysearch2first.php");
   exit;}

//http://localhost/mysearch3.php?page=1&id=1284?&div1scroll=0

$res2  = mysql_query($sql2) or die(mysql_error());

$row2  = mysql_fetch_array($res2);
$numrows = $row2['numrows'];

// how many pages we have when using paging?
$numofpages = ceil($numrows/$rowsPerPage);

//   if (!empty($_POST['button'])){
//     switch ($_POST['button']){
//       case 'firstbutton':         
  //        $offset = 0; 
  //        break;
//       case 'lastbutton': 
          //needs to show last 10 results             
  //        $offset = ($numrows -$rowsPerPage);
  //        if ($offset < 0){$offset = 0;}
  //        $page_num = (ceil($offset/$rowsPerPage)+1) ;           
//          echo 'page'.$page_num.'<BR>';   
//          echo 'offset'.$offset.'<BR>';
//          break;
//     }
  //   }


//LIMIT means start at some number , then go x numbers
//echo 'offest '.$offset.'<BR>';
//echo 'rowsperpage'.$rowsPerPage.'<BR><BR>';

$sql = "SELECT * FROM bookdata where ". $test2." order by titles LIMIT $offset, $rowsPerPage ";
$result2 = mysql_query($sql) or die(mysql_error());



//******************************************************

// how many pages we have when using paging?
$numofpages = ceil($numrows/$rowsPerPage);


if ($numofpages > '1' ) {

            $range =25; //set this to what ever range you want to show in the pagination link
            $range_min = ($range % 2 == 0) ? ($range / 2) - 1 : ($range - 1) / 2;
            $range_max = ($range % 2 == 0) ? $range_min + 1 : $range_min;
            $page_min = $page_num- $range_min;
            $page_max = $page_num+ $range_max;

            $page_min = ($page_min < 1) ? 1 : $page_min;
            $page_max = ($page_max < ($page_min + $range - 1)) ? $page_min + $range - 1 : $page_max;
            if ($page_max > $numofpages) {
                $page_min = ($page_min > 1) ? $numofpages - $range + 1 : 1;
                $page_max = $numofpages;
            }

            $page_min = ($page_min < 1) ? 1 : $page_min;

            //$page_content .= '<p class="menuPage">';
               
              //was used to test for showing first  
//            if ( ($page_num > ($range - $range_min)) && ($numofpages > $range) ) {//                 
                 If ($on =="off"){$page_pagination .= '<a class="num"  title="Click for First Record" href="' . $self . 'page=1"><</a> ';}            
                 else{$page_pagination .= '<a class="num"  title="Click for First Record" href="javascript:my_href(\'' . $self . 'page=1\')"><</a> ';}


//           }

            if ($page_num != 1) {

               If ($on =="off"){$page_pagination .= '<a class="num" href="'.$self.'page='.($page_num-1). '">Previous</a> ';}
               else{$page_pagination .= '<a class="num" href="javascript:my_href(\''       .    $self    .    'page='   .    ($page_num-1)    . '\')">Previous</a> ';}

            }

            for ($i = $page_min;$i <= $page_max;$i++) {
                if ($i == $page_num)
                  $page_pagination .= '<span class="num"><strong>' . $i . '</strong></span> '; 
               else
                 If ($on =="off"){$page_pagination.= '<a class="num" href="'.$self.'page='.$i. '">'.$i.'</a> ';}
                 else{$page_pagination.= '<a class="num" href="javascript:my_href(\''  .  $self  .  'page='.$i. '\')">'.$i.'</a> ';}

           }            
            
            if ($page_num < $numofpages) {
                If ($on =="off"){$page_pagination.= ' <a class="num" href="'.$self.'page='.($page_num + 1) . '">Next</a>';}//the '.' is appending the string
                else{$page_pagination.= ' <a class="num" href="javascript:my_href(\''.$self.'page='.($page_num + 1) . '\')">Next</a>';}//the '.' is appending the string

           }

            // a test for last  
           // if (($page_num< ($numofpages - $range_max)) && ($numofpages > $range)) {
                If ($on =="off"){$page_pagination .= ' <a class="num" title="Click for Last Record" href="'.$self.'page='.$numofpages. '">></a> ';}
                else{$page_pagination .= ' <a class="num" title="Click for Last Record" href="javascript:my_href(\''.$self.'page='.$numofpages. '\')">></a> ';}


           // }
            
            //$page['PAGINATION'] ='<p id="pagination">'.$page_pagination.'</p>';
        }//end if more than 1 page 

echo $page_pagination.'<BR><BR>';

echo 'Number of results - '.$numrows ;
echo ' and Number of pages   - '.$numofpages.'<BR><BR>';

Really I was thinking of local intra net usage at my wife's school.

I just modeled it sort of like after google type search look.

It does use a lot of explode into arrays for the multiword searching.

And a lot of combining of where clauses for mysql.

I just tried PHP because I heard it was simple to implement, AND

importantly, a lot of free web hosting involves PHP and mysql.

I found no matter what language it takes work.

It may look simple but there is a lot of backend though gone into that.

It really is just a rough result output and needs refining.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.