Jump to content


Photo

PHP Full-Text Search Pagination


  • Please log in to reply
1 reply to this topic

#1 john-formby

john-formby
  • Members
  • PipPipPip
  • Advanced Member
  • 91 posts
  • LocationMahe, Seychelles

Posted 20 July 2006 - 11:53 AM

Hi again,

I have been using the Full-Text Search tutorial ound on this site and now have it working exactly as I require except for one thing; I need to add pagination to the results.  I have tried a couple of methods but am unable to make it work.  Please could someone help me with this, I am really struggling.  I have seen this question posted on other forums yet the poster has never received a definitive answer.

Here is the search code I am using:

<?php 
// Conect to the database. 
include "dblinks.inc";
 

// Create the search function: 

function searchForm() 
{ 
  // Re-usable form 
   
  // variable setup for the form. 
    echo '<div id="maincol"><h1>Search</h1>';

  $searchwords = (isset($_GET['words']) ? htmlspecialchars(stripslashes($_REQUEST['words'])) : ''); 
  $normal = (($_GET['mode'] == 'normal') ? ' selected="selected"' : '' ); 
  $boolean = (($_GET['mode'] == 'boolean') ? ' selected="selected"' : '' ); 
   
  echo '<form method="get" action="'.$_SERVER['PHP_SELF'].'">'; 
  echo '<input type="hidden" name="cmd" value="search" />'; 
  echo 'Search for: <input type="text" name="words" value="'.$searchwords.'" /> '; 
  echo 'Mode: '; 
  echo '<select name="mode">'; 
  echo '<option value="normal"'.$normal.'>Normal</option>'; 
  echo '<option value="boolean"'.$boolean.'>Boolean</option>'; 
  echo '</select> '; 
  echo '<input type="submit" value="Search" />'; 
  echo '</form>'; 
} 

// Create the navigation switch 
$cmd = (isset($_GET['cmd']) ? $_GET['cmd'] : ''); 

switch($cmd) 
{ 
  default:
    searchForm(); 
  break;
   
   
  case "search": 
    searchForm(); 
    echo '<h3>Search Results:</h3>'; 
     
    $searchstring = mysql_escape_string($_GET['words']); 
    switch($_GET['mode']) 
    { 
      case "normal": 
        $sql = "SELECT link_id, link_text, url, description, keywords, time_added, new_window,  
               MATCH(link_text, description, keywords)  
               AGAINST ('$searchstring') AS score FROM links  
               WHERE MATCH(link_text, description, keywords)  
               AGAINST ('$searchstring') ORDER BY score DESC"; 
      break; 
       
      case "boolean": 
        $sql = "SELECT link_id, link_text, url, description, keywords, time_added, new_window, 
               MATCH(link_text, description, keywords)  
               AGAINST ('$searchstring' IN BOOLEAN MODE) AS score FROM links  
               WHERE MATCH(link_text, description, keywords)  
               AGAINST ('$searchstring' IN BOOLEAN MODE) ORDER BY score DESC"; 
      break; 
    }  
     
    // echo $sql; 
     
    $result = mysql_query($sql) or die (mysql_error()); 
     
    while($row = mysql_fetch_object($result)) 
    { 

      echo '<hr /><br />';

if ($row->new_window == 1) {
echo '<b class="linkhead"><a href="'.$row->url.'"target=\"_blank\">'.stripslashes(htmlspecialchars($row->link_text)).'</a></b>';
} else {
echo '<b class="linkhead"><a href="'.$row->url.'">'.stripslashes(htmlspecialchars($row->link_text)).'</a></b>';
}

      echo '<br />'.stripslashes(htmlspecialchars($row->description)).'<br />'; 

if ($row->new_window == 1) {
echo '<a href="'.$row->url.'"target=\"_blank\">'.stripslashes(htmlspecialchars($row->url)).'</a><br /><br />';
} else {
echo '<a href="'.$row->url.'">'.stripslashes(htmlspecialchars($row->url)).'</a><br /><br />';
}
    } 
  break; 
} 

echo '<hr /><br /></div>';
?>


Thanks,

John
"I think computer viruses should count as life. I think it says something about human nature that the only form of life we have created so far is purely destructive. We've created life in our own image." ~ Stephen Hawking

#2 john-formby

john-formby
  • Members
  • PipPipPip
  • Advanced Member
  • 91 posts
  • LocationMahe, Seychelles

Posted 21 July 2006 - 11:03 AM

I have had another go at the pagination and have managed to get it to display just 10 records (which I want per page).  The problem is that I know there are more than 10 results for the query but am unable to see any page numbers.  Please can someone have a look at my code and tell me where I am going wrong.  I have highlighted where I have inserted the pagination code.

<?php 
// Connect to the database. 
include "dblinks.inc";
 

// Create the search function: 

function searchForm() 
{ 
  // Re-usable form 
   
  // variable setup for the form. 
    echo '<div id="maincol"><h1>Search</h1>';

  $searchwords = (isset($_GET['words']) ? htmlspecialchars(stripslashes($_REQUEST['words'])) : ''); 
  $normal = (($_GET['mode'] == 'normal') ? ' selected="selected"' : '' ); 
  $boolean = (($_GET['mode'] == 'boolean') ? ' selected="selected"' : '' ); 
   
  echo '<form method="get" action="'.$_SERVER['PHP_SELF'].'">'; 
  echo '<input type="hidden" name="cmd" value="search" />'; 
  echo 'Search for: <input type="text" name="words" value="'.$searchwords.'" /> '; 
  echo 'Mode: '; 
  echo '<select name="mode">'; 
  echo '<option value="normal"'.$normal.'>Normal</option>'; 
  echo '<option value="boolean"'.$boolean.'>Boolean</option>'; 
  echo '</select> '; 
  echo '<input type="submit" value="Search" />'; 
  echo '</form>'; 
} 

// Create the navigation switch 
$cmd = (isset($_GET['cmd']) ? $_GET['cmd'] : ''); 



// ************** Pagination Start ****************


$num_per_page=10;

$num_pages=ceil($num/$num_per_page);

$pages=Array();

FOR ($i=0;$i<$num_pages;$i++)
IF ($mode == normal) {
$pages[]="<a href=\"?cmd=search&words=searchstring&mode=normal=".$i."\">[".($i+1)."] </a>";
} ELSE {
$pages[]="<a href=\"?cmd=search&words=searchstring&mode=boolean=".$i."\">[".($i+1)."] </a>";
}

$pages=implode("&nbsp;",$pages); #Here's your string with the page links, output where ever

if ($start<0)
$start=0;

$x=($start*$num_per_page);
$y=$num_per_page;


// ************** Pagination End ****************



switch($cmd) 
{ 
  default:
    searchForm(); 
  break;
   
   
  case "search": 
    searchForm(); 
    echo '<h3>Search Results:</h3>'; 
     
    $searchstring = mysql_escape_string($_GET['words']); 
    switch($_GET['mode']) 
    { 
      case "normal": 
        $sql = "SELECT link_id, link_text, url, description, keywords, time_added, new_window,  
               MATCH(link_text, description, keywords)  
               AGAINST ('$searchstring') AS score FROM links  
               WHERE MATCH(link_text, description, keywords)  
               AGAINST ('$searchstring') ORDER BY score DESC LIMIT {$x},{$y}"; 
      break; 
       
      case "boolean": 
        $sql = "SELECT link_id, link_text, url, description, keywords, time_added, new_window, 
               MATCH(link_text, description, keywords)  
               AGAINST ('$searchstring' IN BOOLEAN MODE) AS score FROM links  
               WHERE MATCH(link_text, description, keywords)  
               AGAINST ('$searchstring' IN BOOLEAN MODE) ORDER BY score DESC LIMIT {$x},{$y}"; 
      break; 
    }  
     
    // echo $sql; 
     
    $result = mysql_query($sql) or die (mysql_error()); 



// ************** Pagination Start ****************

$num=mysql_num_rows($result);


// ************** Pagination End ****************


    while($row = mysql_fetch_object($result)) 
    { 

      echo '<hr /><br />';

if ($row->new_window == 1) {
echo '<b class="linkhead"><a href="'.$row->url.'"target=\"_blank\">'.stripslashes(htmlspecialchars($row->link_text)).'</a></b>';
} else {
echo '<b class="linkhead"><a href="'.$row->url.'">'.stripslashes(htmlspecialchars($row->link_text)).'</a></b>';
}

      echo '<br />'.stripslashes(htmlspecialchars($row->description)).'<br />'; 

if ($row->new_window == 1) {
echo '<a href="'.$row->url.'"target=\"_blank\">'.stripslashes(htmlspecialchars($row->url)).'</a><br /><br />';
} else {
echo '<a href="'.$row->url.'">'.stripslashes(htmlspecialchars($row->url)).'</a><br /><br />';
}
    } 
  break; 
} 

echo '<hr /><br />';



// ************** Pagination Start ****************


echo $pages;


// ************** Pagination End ****************



echo '</div>';
?>


Thanks,

John
"I think computer viruses should count as life. I think it says something about human nature that the only form of life we have created so far is purely destructive. We've created life in our own image." ~ Stephen Hawking




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users