Jump to content

PHP Full-Text Search Pagination


john-formby

Recommended Posts

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:

[code]<?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>';
?>[/code]


Thanks,

John
Link to comment
https://forums.phpfreaks.com/topic/15131-php-full-text-search-pagination/
Share on other sites

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.

[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>';
?>[/code]


Thanks,

John

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.