mrelliott Posted February 25, 2007 Share Posted February 25, 2007 I'm new at PHP and MySQL. I've used the tutorial on this site to set up a search engine for a database of songs for the band Queen. It does find results but I've run into some problems. I'm having difficulty figuring out how to set up a session that will store the search results collected, they disappear after I click on page 2. And I had read somewhere else on this forum that the results should be stored in a session, but I can't find that topic anymore. Also I want to set up the pages so that the search results determine how many pages are needed. What I have set up in the count function section of the code counts all the records in my song table and I want it to count the full text index if that's possible. Here's my code. <?php session_start(); ?> <html><!-- #BeginTemplate "/Templates/queenlayout.dwt" --> <head> <!-- #BeginEditable "doctitle" --> <title>Queen Album & Song Database -- Search Results</title> <!-- #EndEditable --> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body bgcolor="#000066" text="#CCCCCC" link="#CCCCCC" vlink="#CCCCCC" alink="#CCCCCC"> <div align="center"> <table width="98%" border="0" height="116"> <tr> <td height="36"> <div align="center"></div> <div align="center"></div> <div align="center"><!-- #BeginEditable "header" --> <?php include ('./includes/queenheader.html'); ?> <!-- #EndEditable --></div> <div align="center"></div> </td> </tr> <tr> <td height="483"> <div align="center"></div> <div align="center"></div> <div align="center"><!-- #BeginEditable "results" --> <?php // Full-Text Search Example // Conect 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 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') AS score FROM master_list WHERE MATCH(title, album, writer) AGAINST ('$searchstring') ORDER BY score DESC LIMIT $start, $display"; break; case "boolean": $sql = "SELECT song_id, title, album, writer, MATCH(title, album, writer) AGAINST ('$searchstring' IN BOOLEAN MODE) AS score FROM master_list WHERE MATCH(title, album, writer) AGAINST ('$searchstring' IN BOOLEAN MODE) ORDER BY score DESC LIMIT $start, $display"; break; } // echo $sql; $result = mysql_query($sql) or die (mysql_error()); while($row = mysql_fetch_object($result)) { echo '<font size="-3"><strong>Title: '.stripslashes(htmlspecialchars($row->title)).'</strong><br /></font>'; echo '<font size="-3">Score:'. number_format($row->score, 1).' Album: '.stripslashes(htmlspecialchars($row->album)).'<br /></font>'; echo '<font size="-3"><p>Writer: '.stripslashes(htmlspecialchars($row->writer)).'</p></font>'; echo '<hr size="-3" />'; } break; } //mysql_free_result ($result); // Free up the resources. mysql_close(); //Close connection. // 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="searchresults.php?s=' . ($start - $display) . '&np=' . $num_pages .'">Previous</a> '; } // Make all the numbered pages. for ($i = 1; $i <= $num_pages; $i++) { if ($i != $current_page) { echo '<a href="searchresults.php?s='.(($display * ($i - 1))).'&np='.$num_pages .'">'.$i.'</a> '; } else { echo $i.' '; } } // If it's not the last page, make a Next button. if ($current_page != $num_pages) { echo '<a href="searchresults.php?s=' . ($start + $display) . '&np=' . $num_pages .'">Next</a> '; } echo '</p>'; } // End of links section. ?> <!-- #EndEditable --></div> </td> </tr> <tr> <td height="27"> <div align="center"><!-- #BeginEditable "footer" --> <?php include ('./includes/queenfooter.html'); ?> <!-- #EndEditable --></div> </td> </tr> </table> </div> </body> <!-- #EndTemplate --></html> Quote Link to comment https://forums.phpfreaks.com/topic/40010-creating-a-session-for-and-proper-pagination-of-full-text-search-results/ Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.