Jump to content

Recommended Posts

I am in desperate need of a simple site search for a small business website.  I found a how-to on the net and, though I have very limited experience with PHP and MySql I was able to follow the directions, build the database and tables, etc.  There are a lot of postings on the orginal site which indicate that the script works fine.

 

The how-to from which I'm working is here:

http://www.onlamp.com/pub/a/php/2002/10/24/simplesearchengine.html

 

I'm doing all of this on a linux server, by the way.

 

The whole thing seemed fairly simply but there is one page/script that searches URLS and adds them to the database.  The author of the how-to doesn't really explain, very well, where in the heck you put the URLs you want indexed.  Someone had posted a similar question on the site (which is really old - therefore I didn't ask there) and another individual replied that you simply add the URL(s) "at the end of the php script".  If I do as those instructions indicate I get a parsing error at the line where I've added something like:

 

populate.php?url=http://www.cnn.com/

 

If someone could take a look at this thing and tell me what's wrong I would be extremely grateful.  I'm an old man and really don't have the time to study php and mysql from the ground up - well, I'm trying to learn...

 

The populate.php script, without any designated URLS, looks like this:

 

 

<?

/*

* populate.php

*

* Script for populating the search database with words,

* pages and word-occurences.

*/

 

/* Connect to the database: */

mysql_pconnect("localhost","root","secret")

    or die("ERROR: Could not connect to database!");

 

mysql_select_db("test");

 

/* Define the URL that should be processed: */

 

$url = addslashes( $_GET['url'] );

 

if( !$url )

{

   die( "You need to define a URL to process." );

}

else if( substr($url,0,7) != "http://" )

{

   $url = "http://$url";

}

 

/* Does this URL already have a record in the page-table? */

$result = mysql_query("SELECT page_id FROM page WHERE page_url = \"$url\"");

$row = mysql_fetch_array($result);

 

if( $row['page_id'] )

{

   /* If yes, use the old page_id: */

   $page_id = $row['page_id'];

}

else

{

   /* If not, create one: */

   mysql_query("INSERT INTO page (page_url) VALUES (\"$url\")");

   $page_id = mysql_insert_id();

}

 

/* Start parsing through the text, and build an index in the database: */

if( !($fd = fopen($url,"r")) )

   die( "Could not open URL!" );

 

while( $buf = fgets($fd,1024) )

{

   /* Remove whitespace from beginning and end of string: */

   $buf = trim($buf);

 

   /* Try to remove all HTML-tags: */

   $buf = strip_tags($buf);

   $buf = ereg_replace('/&\w;/', '', $buf);

 

   /* Extract all words matching the regexp from the current line: */

   preg_match_all("/(\b[\w+]+\b)/",$buf,$words);

 

   /* Loop through all words/occurrences and insert them into the database: */

   for( $i = 0; $words[$i]; $i++ )

   {

      for( $j = 0; $words[$i][$j]; $j++ )

      {

         /* Does the current word already have a record in the word-table? */

         $cur_word = addslashes( strtolower($words[$i][$j]) );

 

         $result = mysql_query("SELECT word_id FROM word

                                WHERE word_word = '$cur_word'");

         $row = mysql_fetch_array($result);

         if( $row['word_id'] )

         {

            /* If yes, use the old word_id: */

            $word_id = $row['word_id'];

         }

         else

         {

            /* If not, create one: */

            mysql_query("INSERT INTO word (word_word) VALUES (\"$cur_word\")");

            $word_id = mysql_insert_id();

         }

 

         /* And finally, register the occurrence of the word: */

         mysql_query("INSERT INTO occurrence (word_id,page_id)

                      VALUES ($word_id,$page_id)");

         print "Indexing: $cur_word<br>";

      }

   }

}

 

fclose($fd);

 

?>

 

Where would I put the URL(s), exactly - and what syntax should I use?  As I've said, if I type something like "populate.php?url=http://www.cnn.com/", right before ?>, I get a parsing error.

 

 

 

Thanks for any help,

 

Guy Merritt

Flint, MI

No I'm not an affiliate ;)

 

Are you sure? Virtually all your recent posts are advertising HotScripts. How will anyone ever learn if someone shoves a pre-made script of poor quality up their nose which they probably won't read the source of?

Yes I'm not an affiliate, just trying to help you (for what it's worth it to you).

 

In the early days I used the javascript source a lot, now I use the hotscripts :)

 

They realy have nice and easy solutions (for free) the quality is sometimes bad , and sometimes goed, but you can look at the ratings.

 

In recent posts I have advised 2 people about hotscripts (one of them was you) feel free to find a better solution.

 

Kind regards,

Sebas.

Your welcome :)

 

And I'm sorry for sounding harsh.

I just figured that it might have been easier to get a ready made code since it was to be a simple search function and you (like me) have limited php experiance.

 

I personally always learn a lot by looking at some one else his code and than change parameters to see what happens. Well anyways it was not my intention to sound harsh it was late (my local time) when I wrote it.

 

Good luck with future programming.

 

Kind regards,

Sebas

I'm sorry if I offended you.

 

And I now realize that it is someone else who thinks I'm an affiliate of hotscripts.

 

Please feel free to read all my posts and you will see that I actually do try to help people rather than providing a quick solution.

http://www.phpfreaks.com/forums/index.php?action=profile;u=43754;sa=showPosts

 

It was absolutely not my intention to withhold someone from learning php. I honestly thought that because he was in a desperate need for a simple site search function and he had limited php experience a solution that had been created by someone and has proven to work well might be a solution to him.

 

In my opinion there is nothing wrong with giving someone a suggestion. But I'm sorry if there has been a miss understanding. I only tried to help and it helps me a lot by looking at working solutions and adapting them to my needs.

 

As far as I know I have only mentioned hotscripts 2 times in all my posts. I'm not an affiliate with them and do not earn any gain by trying to help people on this forum. Even though my experience is limited, I am more than willing to help if I can.

 

Kind regards,

Sebastiaan

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.