Jump to content

Still looking for help with pagination and full text search, PLEASE ANYONE HELP!


mrelliott

Recommended Posts

Can somebody PLEASE show me how to set up my counting code to select only what's being searched for and not everything in the table. I've been trying this for days. When I actually seem to get a result that works only ten results are listed with no page links at the bottom of the screen or (as in the case of the code below) it lists page links but all results disappear when one is clicked, even from the first page.

 

<?php 
// Full-Text Search Example 
// Connect to the database. 
require_once ('./includes/mysql_connect.php'); //connect to database

//Records to display per page.
$display = 10;

//Check if the number of required pages has been determined. 
if (isset($_GET['np'])) {   //Already been determined.
$num_pages = $_GET['np'];   
} else { //Need to determine.

//I need this code to select only what's been entered into the search bar. The columns I need to count from are title, album and writer.

    // Count the number of records
    $query = "SELECT COUNT(*) FROM master_list ORDER BY song_id";   
    $result = @mysql_query ($query);
$row = mysql_fetch_array ($result, MYSQL_NUM);
    $num_records = $row[0];

//calculate number of pages.
if ($num_records > $display) {   
$num_pages = ceil ($num_records/$display);   
} else {
$num_pages = 1;
}

} //end of np if.

//where to start returning results.
if (isset($_GET['s'])) {   
$start = $_GET['s'];
} else {
$start = 0;
} 

// Create the search function: 
function searchForm() 
{ 
  // Re-usable form 
   
  // variable setup for the form. 
  $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 '<font size="-3">Search for: <input type="text" name="words" value="'.$searchwords.'" /></font> '; 
  echo '<font size="-3">Mode: </font>'; 
  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: 
    //echo '<h1><font size="-3">Search Database!</font></h1>'; 
    searchForm();  
  break; 
  case "search": 
    searchForm(); 
    //echo '<h3><font size="-3">Search Results:</font></h3><br />'; 
     
    $searchstring = mysql_escape_string($_GET['words']); 
    switch($_GET['mode']) 
    { 
      case "normal": 
        $sql = "SELECT song_id, title, album, writer, MATCH (title, album, writer) AGAINST ('$searchstring') FROM master_list  
               WHERE MATCH(title, album, writer) AGAINST ('$searchstring') ORDER BY title LIMIT $start, $display"; 
      break; 
       
      case "boolean": 
        $sql = "SELECT song_id, title, album, writer, MATCH (title, album, writer) AGAINST ('$searchstring' IN BOOLEAN MODE) FROM master_list  
               WHERE MATCH(title, album, writer) AGAINST ('$searchstring' IN BOOLEAN MODE) ORDER BY title LIMIT $start, $display"; 
      break; 
    }  
     
    // echo $sql;
     
$result = mysql_query($sql) or die (mysql_error()); 

while($row = mysql_fetch_object($result)){ 
      echo '<font size="-3">Title: '.stripslashes(htmlspecialchars($row->title)).'<br /></font>'; 
      echo '<font size="-3">Album: '.stripslashes(htmlspecialchars($row->album)).'<br /></font>';
      echo '<font size="-3">Writer: '.stripslashes(htmlspecialchars($row->writer)).'<br /></font>'; 
      echo '<hr size="-3" />'; 
    } 
  break; 
} 
// Make the links to other pages, if necessary.
if ($num_pages > 1) {

    echo '<br /><p>';
    // Determine what page the script is on.
    $current_page = ($start/$display) + 1;

    // If it's not the first page, make a Previous button.
    if ($current_page != 1) {
       echo '<a href="searchresults2.php?s=' . ($start - $display) . '&np=' . $num_pages . '&words=' . $searchstring .'">Previous</a> ';   
    }

    // Make all the numbered pages.
for ($i = 1; $i <= $num_pages; $i++) {
if ($i != $current_page) {
echo '<a href="searchresults2.php?s='.(($display * ($i - 1))).'&np='.$num_pages.'&words='.$searchstring .'">'.$i.'</a> ';   
} else {
echo $i.' ';
}
}

    // If it's not the last page, make a Next button.
    if ($current_page != $num_pages) {
       echo '<a href="searchresults2.php?s=' . ($start + $display) . '&np=' . $num_pages . '&words=' . $searchstring .'">Next</a> ';   
    }

    echo '</p>';

} 
// End of links section.
?>

Link to comment
Share on other sites

  • 3 weeks later...

Can somebody PLEASE just read through this and tell me what am I doing wrong with this code. Why can't I get the page links to display at the bottom of the page. What am I missing here! Here's the full code. It does look up words but will only display a max of 10 even when I know there's more. If there's something wrong with it tell me, I'm new with this and I'm getting ready to just give up.

<?php 
// Full-Text Search Example 
// Connect to the database. 
require_once ('./includes/mysql_connect.php'); //connect to database

//Records to display per page.
$display = 10;

//Check if the number of required pages has been determined. 
if (isset($_GET['np'])) {   //Already been determined.
$num_pages = $_GET['np'];   
} else { //Need to determine.

    // Count the number of records
$query = "SELECT COUNT(*) FROM master_list WHERE MATCH(title, album, writer) AGAINST('$searchstring') ORDER BY song_id";
$result = mysql_query($query);
$row = mysql_fetch_array($result, MYSQL_NUM);
    $num_records = $row[0];

//calculate number of pages.
if ($num_records > $display) {   
$num_pages = ceil($num_records/$display);   
} else {
$num_pages = 1;
}

} //end of np if.

//where to start returning results.
if (isset($_GET['s'])) {   
$start = $_GET['s'];
} else {
$start = 0;
} 

// Create the search function: 
function searchForm() 
{ 
  // Re-usable form 
   
  // variable setup for the form. 
  $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 '<font size="-1">Search for: <input type="text" name="words" value="'.$searchwords.'" /></font> '; 
  echo '<font size="-1">Mode: </font>'; 
  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: 
   echo '<h1><font size="-1">Search Database!</font></h1>'; 
    searchForm();  
  break; 
  case "search": 
    searchForm(); 
    echo '<h3><font size="-1">Search Results:</font></h3><br />'; 
     
    $searchstring = mysql_escape_string($_GET['words']); 
    switch($_GET['mode']) 
    { 
      case "normal": 
        $sql = "SELECT song_id, title, album, writer, MATCH (title, album, writer) AGAINST ('$searchstring') FROM master_list  
               WHERE MATCH(title, album, writer) AGAINST ('$searchstring') ORDER BY title LIMIT $start, $display"; 
      break; 
       
      case "boolean": 
        $sql = "SELECT song_id, title, album, writer, MATCH (title, album, writer) AGAINST ('$searchstring' IN BOOLEAN MODE) FROM master_list  
               WHERE MATCH(title, album, writer) AGAINST ('$searchstring' IN BOOLEAN MODE) ORDER BY title LIMIT $start, $display"; 
      break; 
    }  
     
    // echo $sql;
     
$result = mysql_query($sql) or die (mysql_error()); 

while($row = mysql_fetch_object($result)){ 
      echo '<font size="-1">Title: '.stripslashes(htmlspecialchars($row->title)).'<br /></font>'; 
      echo '<font size="-1">Album: '.stripslashes(htmlspecialchars($row->album)).'<br /></font>';
      echo '<font size="-1">Writer: '.stripslashes(htmlspecialchars($row->writer)).'<br /></font>'; 
      echo '<hr size="-1" />'; 
    } 
  break; 
} 
// Make the links to other pages, if necessary.
if ($num_pages > 1) {

    echo '<br /><p>';
    // Determine what page the script is on.
    $current_page = ($start/$display) + 1;

    // If it's not the first page, make a Previous button.
    if ($current_page != 1) {
       echo '<a href="searchresults3.php?s='.($start - $display).'&np='.$num_pages.'&words='.$searchwords .'">Previous</a> ';   
    }

    // Make all the numbered pages.
for ($i = 1; $i <= $num_pages; $i++) {
if ($i != $current_page) {
echo '<a href="searchresults3.php?s='.(($display * ($i - 1))).'&np='.$num_pages.'&words='.$searchwords.'">'.$i.'</a> ';   
} else {
echo $i.' ';
}
}

    // If it's not the last page, make a Next button.
    if ($current_page != $num_pages) {
       echo '<a href="searchresults3.php?s='.($start + $display).'&np='.$num_pages.'&words='.$searchwords.'">Next</a> ';   
    }

    echo '</p>';

} 
// End of links section.
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.