rateit05 Posted March 5, 2009 Share Posted March 5, 2009 Ok here is the problem, i am a complete newbie to PHP and i have been given some code to do with a simple search script. I have been tasked to improve how the information is presented - by this i mean, show partially what is contained within the file/page (like what google does). I have attached the file, any guidance will be appreciated. [attachment deleted by admin] Link to comment https://forums.phpfreaks.com/topic/148078-php-newbie-help-controling-information-that-is-shown/ Share on other sites More sharing options...
Yesideez Posted March 5, 2009 Share Posted March 5, 2009 TIP: Pasting your code into the topic (surrounding it with bbcode CODE tags) will get an answer a lot quicker. Link to comment https://forums.phpfreaks.com/topic/148078-php-newbie-help-controling-information-that-is-shown/#findComment-777265 Share on other sites More sharing options...
rateit05 Posted March 5, 2009 Author Share Posted March 5, 2009 <?php require_once('sql/connection.php');?> <?php mysql_select_db($database_webindex, $webindex); if (isset($_POST['searchwords'])) { $searchwords = $_POST['searchwords']; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Chris Bells Test Search Site</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <link href="style.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="search"> <div id="nav"> <ul> <li><a href="/page1.html">Page 1</a></li> <li><a href="#">Page 2</a></li> <li><a href="#">Page 3</a></li> </ul> </div> <div style="clear: both;"></div> <form method="post" action="search.php"> <input type="text" size="40" value="<?php echo $searchwords;?>" name="searchwords" style="background-image: url(images/search_back.gif); padding: 10px 10px 0px 10px; float: left; width: 417px; height: 32px; border: 0px;" /> <input type="submit" name="submit" value="" style="background-image: url(images/button.gif); width: 87px; height: 42px; border: 0px; float: right; margin: 0px 82px 0px 0px;" /> </form> </div> <br /> <div style="margin: 0 auto; width: 570px; height: 11px;"><img src="images/top.gif" width="570" height="11" alt="" /></div> <div id="results"> <p class="contentheading">Search Results</p> <p class="results_txt"> <?php if (isset($_POST['searchwords'])) { $searchwords = $_POST['searchwords']; // return a list of searched for words //$sqlstring="delete from tbl_wordindex where siteID=1 and filename='$filepath'"; //echo $sqlstring."<br>"; $searcharray = explode(' ',$searchwords,255); //echo "You searched for the following words:"; $totalcount=0; $totallist=array(); for ($i=0;$i<count($searcharray);$i++) { // find all matches with $ith word in search list $sqlstring="select distinctrow siteID,filename from tbl_wordindex where word='$searcharray[$i]'"; $recordset = mysql_query($sqlstring,$webindex) or die(mysql_error()); $searchrow = mysql_fetch_assoc($recordset); $numrows = mysql_num_rows($recordset); if ($numrows > 0) { do { $pagename = $searchrow['filename']; $totallist[$totalcount]=$pagename; // build array of matching pages - per word at first $totalcount++; }while ($searchrow = mysql_fetch_assoc($recordset)); } else { echo "<br>No pages contained the word <strong>".$searcharray[$i]."</strong>"; } } // Go through sorted array and count occurrences of pages // Then store these uniquely, along with count value in new array // Sort this new array by count to give a list of pages that match // the requested words in order of words matched if ($totalcount > 0) { // Extract and sort list of unique pages $uniquelist=$totallist; $uniquelist = array_unique($uniquelist); array_multisort($uniquelist); $uniquecount=array(); // Sort total list to allow it to be stepped through array_multisort($totallist); $indexinfulllist = 0; for ($i=0;$i<count($uniquelist);$i++) { // for each unique page in the list of returned pages // count how many matches there are $firstinstance = $totallist[$indexinfulllist]; $instancecount = 1; if ($indexinfulllist<count($totallist)-1) { $indexinfulllist++; $nextinstance = $totallist[$indexinfulllist]; while ($nextinstance == $firstinstance) { $indexinfulllist++; $instancecount++; $nextinstance = $totallist[$indexinfulllist]; } } $uniquecount[$i]=$instancecount; } // Sort the parallel arrays of filenames and number of matches $limit = count($uniquelist); for ($i=0;$i<$limit-1;$i++) { for ($j=$i;$j<$limit;$j++) { if ($uniquecount[$j]>$uniquecount[$i]) { // swap values in counter array $temp = $uniquecount[$i]; $uniquecount[$i] = $uniquecount[$j]; $uniquecount[$j] = $temp; // swap same values in filename array $temp = $uniquelist[$i]; $uniquelist[$i] = $uniquelist[$j]; $uniquelist[$j] = $temp; } } } // Output list of matching pages, sorted in order of number of words matched $lastval=-1; echo "<p>"; for ($i=0;$i<count($uniquelist);$i++) { $nextval=$uniquecount[$i]; if ($nextval <> $lastval) { // New value for number of matches echo '</p><p class="contenttext">The following page(s) contain '.$uniquecount[$i].' of your search words</p>'; } echo '<p class="result_txt"><a href="'.$uniquelist[$i].'">'.$uniquelist[$i]."</a> - this page contains the search word</p>"; $lastval=$nextval; } } } else { // No search criteria entered - do nothing } ?> </p> </div> <div style="margin: 0 auto; width: 570px; height: 11px;"><img src="images/bottom.gif" width="570" height="11" alt="" /></div> </body> </html> Link to comment https://forums.phpfreaks.com/topic/148078-php-newbie-help-controling-information-that-is-shown/#findComment-777270 Share on other sites More sharing options...
kickstart Posted March 5, 2009 Share Posted March 5, 2009 Hi That script is checking an already existing table containing all the words for each possible page. It doesn't actually search the pages itself. How to proceed from here depends on whether the database already also stores the complete text that is searched. All the best Keith Link to comment https://forums.phpfreaks.com/topic/148078-php-newbie-help-controling-information-that-is-shown/#findComment-777279 Share on other sites More sharing options...
rateit05 Posted March 5, 2009 Author Share Posted March 5, 2009 yes each word from the files on the server are stored in a table called tbl_wordindex. Link to comment https://forums.phpfreaks.com/topic/148078-php-newbie-help-controling-information-that-is-shown/#findComment-777315 Share on other sites More sharing options...
kickstart Posted March 5, 2009 Share Posted March 5, 2009 Hi Is there a similar table containing the full text of the page? All the best Keith Link to comment https://forums.phpfreaks.com/topic/148078-php-newbie-help-controling-information-that-is-shown/#findComment-777349 Share on other sites More sharing options...
rateit05 Posted March 5, 2009 Author Share Posted March 5, 2009 there are 2 tables: tbl_site tbl_word index I have no idea what tbl_site is used for as it is a empty table. Where as tbl_wordindex contains all the words that are n the pages that are being searched. (this is because the pages have to be indexed first or something, before they can be searched) Link to comment https://forums.phpfreaks.com/topic/148078-php-newbie-help-controling-information-that-is-shown/#findComment-777481 Share on other sites More sharing options...
kickstart Posted March 5, 2009 Share Posted March 5, 2009 Hi Think what you need to do is either directly grab the bits of the sites when you decide that they match the search criteria (and that would leave you stripping out the unwanted html to just display the page contents), or you need to store the full text (or maybe just the short bit to display) in another table when the page is originally indexed. All the bet Keith Link to comment https://forums.phpfreaks.com/topic/148078-php-newbie-help-controling-information-that-is-shown/#findComment-777504 Share on other sites More sharing options...
rateit05 Posted March 9, 2009 Author Share Posted March 9, 2009 i think i get what you mean....would that mean running an SQL Query to grab the information? Thanks Link to comment https://forums.phpfreaks.com/topic/148078-php-newbie-help-controling-information-that-is-shown/#findComment-780673 Share on other sites More sharing options...
kickstart Posted March 9, 2009 Share Posted March 9, 2009 Hi If you stored it when you did the initial indexing then you could pull it back from the table. This would probably be the most efficient way of doing it. Or when people do a seach you could grab the site and strip out the bit you want to display. This would likely be far less efficient, but would mean the quoted section of the site was up to date. All the best Keith Link to comment https://forums.phpfreaks.com/topic/148078-php-newbie-help-controling-information-that-is-shown/#findComment-780678 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.