Page 1 of 1

:?: some problems of displaying search results in multipage

Posted: Mon Dec 06, 2004 4:34 am
by lauehuang
Hi, everybody

I am tring to build a search function with PHP and keyword matching(without mysql ) for my homepage. The search function has already worked. however, the search results are just displayed in one page. So I want to change it to represent in multipage. for example 5 results pre page. 12 results will be arranged in 3 pages.

But now I met a big problem. It shows nothing if I click the second or third page. I am not sure if the code is incorrect. Any help and suggestions are welcome. Thanks in advance.

Code: Select all

$found = "0"; 
   $file_type = ".html"; 
   $directory = opendir('.'); 
   $limit = 5; 

   $page = $_REQUESTї'page']; 
   $query = $_POSTї'query']; 


   if (!isset($_GETї'page'])) { 
    $page = 1; 
   } 

   if($page > 1){ 
    $query = $_REQUESTї'query']; 
   } 

    if(strlen($search) < 3)&#123; 
    echo "Please enter more characters in this field."; 
               exit(); 
   &#125;
... // search process

Code: Select all

if($found != "0")&#123; 
           echo "$found Results found for " $search "<p>"; 
           $arr = explode("\n", $arr); 
           natsort($arr); 

           reset($arr); 

           $arr = array_reverse($arr); 

       // create multipages for Search Results 
       $num_rows = sizeof($arr); 
       $sOut = "<center><p> - "; 
       $num_pages = ceil($num_rows / $limit); 


      $first = ($page-1) * $limit; 
      $last = $first + $limit; 

       // output the results saved in Array 
       for($i=$first; $i<$last; $i++) &#123; 
        $output .= $arr&#1111;$i]."\n"; 
       &#125; 
       print $output; 

       for($x=1;$x<=$num_pages;$x++)&#123; 
        $pos_page = ($x - 1) * $limit; 

  if($pos_page != $page)&#123; 
     $sOut .= "<a href="$PHP_SELF?query=$query&page=".($pos_page + 1)."">$x</a> - "; 
   &#125; 
           else&#123; 
        $act_page = $x; 
        $sOut .= "<font style="font-weight: bold">$x</font> - "; 
     &#125; 
       &#125; 
   $sOut .= "<br /><b>Page $act_page from $num_pages pages</b><br />"; 
       $sOut .= "</p></center>"; 
       echo $sOut; 
&#125;

Posted: Mon Dec 06, 2004 6:54 am
by harsha
Hi

I had faced the same problem when I tried to design my first PHP FLAT file GUEST book it is here

pixel.f2o.org/nazo/view.php

I use a text file to store the data.

and for displaying the messages page wise I loaded the data in a array

and using PHP function

array_splice();

I achived pagination.

Note: validate the data coming from $_GET and $_POST.

it is like this.

Big array say 1---------10

split array 1 - 5 display in page 1
6 - 10 display in page 2