Google Answers Logo
View Question
 
Q: Automatic Linking in PHP MySQL to defined words/phrases ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: Automatic Linking in PHP MySQL to defined words/phrases
Category: Computers > Programming
Asked by: barryfreed-ga
List Price: $10.00
Posted: 08 Jan 2003 12:29 PST
Expires: 07 Feb 2003 12:29 PST
Question ID: 139401
I think my first problem is not knowing what to call what I'm trying
to do, so let me explain what I want, and hopefully you can give me a
shove in the right direction with some links.

On sites like Yahoo, when you're reading an article, you'll notice
that there will be certain names that are followed with links.
(Example: President Bush (news | web sites)<- these would be links).
Now, I know that there's a way to have a MySQL database table that
defines links for certain phrases, and makes those phrases into links
automatically. I just can't find a way to do it.
I am currently converting our site to being completely PHP mySQL based
and want to make everything nice and databasey. One of the best
examples of how I want this to work is on this page:
http://lgu.com/whatsnew/
I want to set up a content management system whereby users can enter
the date of their event, the text to accompany it, the link URL, etc.
Since there's no standard place to add the link, it wouldn't make
sense to make a link field in the cms form. Also, you'll notice that
every time a lawyer's name is mentioned, there's a link to their bio.
My thought was that if I can define links in one place, I would be
able to have their name linked to their bio without me having to touch
anything. This would save me tremendous time and effort.
And on the other part (links to events, etc), I would just be able to
add this phrase to my definitions, and then I won't have to mess with
the linking part again.
So my specific questions are:
1. What is this called?
2. How can I do it?
3. Are there any links or tutorials available that could help me?

Thanks, and I look forward to your suggestions.
Answer  
Subject: Re: Automatic Linking in PHP MySQL to defined words/phrases
Answered By: webadept-ga on 08 Jan 2003 13:18 PST
Rated:5 out of 5 stars
 
Hi, 

I've been coding for years and I don't know what that is called
either. Not even sure there is a term for it, but I can tell you how
it is done dynamically.

First you need a table with your Keywords (the names of the people and
articles in this case) in one field and your hyperlinks in another.
Set an index on the names so the table will work quicker.

Next, you make your form and submission program. In that program you
are going to want to run a query against that table and replace all
the names with the hyperlinked names. To do this you will use
str-replace.

http://www.php.net/manual/en/function.str-replace.php

So, in your table you have a listing record that says this. 

John Smith | <a href="http://www.mysite.com/john_smilth_bio.html">John
Smith</a>

Using a while loop, go through all the listings in your table and
str-replace the "John Smith"'s
with the link <a href="http://www.mysite.com/john_smilth_bio.html">John
Smith</a>

Then submit this to your database. When it makes the page later using
this text field it will have the linked name in the text.

Thanks, 

webadept-ga

Request for Answer Clarification by barryfreed-ga on 10 Jan 2003 07:50 PST
Thank you for pointing me in the right direction, but I guess I'm
still just a little bit foggy about one thing. I've been able to come
up with code that uses str_replace to do some auto-linking on a page.
I'll paste the code below:

###############333
<?php 
$data = array ( 
          'words' => array('George', 'David', 'Samuel'), 
          'links' => array('george.html','david.html','samuel.html') 
          ); 
$text = "David was the father of George and son of Samuel"; 
$numberoflinks = count($data['links']); 
for ($i=0;$i<=$numberoflinks;$i++){ 
   $data['links'][$i] = "<a
href=\"http://".$data['links'][$i]."\">".$data['words'][$i]."</a>";
} 
$text = str_replace($data['words'], $data['links'], $text); 
echo $text; 
#########################

Now, this will work to change the words defined as $text to links.
This is good, and is a huge step forward. However, I want the text to
come from database calls. Will this work when the data comes out of
the tables and into this page, or do I have to do the str_replace
somewhere else first? Meaning, can I define $text as a database call
and have everything still work?

Thanks again for your answer. Let me know if this is outside the scope
of the question, and I'll be happy to rate this and move on somewhere
else. Just thought you might know off the top of your head.

Clarification of Answer by webadept-ga on 10 Jan 2003 11:32 PST
Hi again, 

I would do the replace before the text went into the database, not on
the call to make the page. The reason is, while you are putting the
text into the database from the form, the speed here is not that
important, because you may only have (at most) 5 or 6 users doing it
at the same time, even on the busiest period. If you do it while
making the page for web users to see, then this could be done much
more often. Now, your page right now may not be busy enough to worry
about it, but it is a much better practice for the future to keep as
little processing on a web-active page as possible. The simpler the
final dynamic page is, the better.

Once the paragraph is written, it is basically static, meaning, this
paragraph isn't going to be changing every hour or even every day. So
you would do better doing the replace as you are putting the info in a
text field in mysql. Then you just call the info from the database and
display it with the hyperlink already there, no extra processing
required.

One of the basic rules for programming dynamic sites is to plan for
success. Always look at your code as if 1000 people are going to be
running it every minute. Less is more. So while you use the dynamics
of getting the info out of the database, try to make sure that this
info needs as little changing as possible.

webadept-ga
barryfreed-ga rated this answer:5 out of 5 stars
Now that makes sense. I was looking all around for how to do the
str_replace, and all I found were scripts that do it on the other end.
Like the one I listed above, they work for very small snippets of
text, and it would take a lot of processing to do it the other way. I
just didn't even consider doing this on the submit side of the cms. I
totally understand the concept now, just have to figure out how to do
that.
Thanks so much. Now at least I know my next steps. Huge help.
nb

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy