[nycphp-talk] Dynamically Add Links to Text
tedd
tedd at sperling.com
Thu Aug 27 17:11:28 EDT 2009
At 10:04 AM -0400 8/27/09, Randal Rust wrote:
>In one of our applications, we have a need to apply inline links to
>the text, and would like to figure out a way to do this dynamically. I
>have looked for this high and low, but haven't been able to find what
>I am looking for. Perhaps I am searching the wrong keywords.
>
>What we have is a repository of 1,000 articles. Each article has a
>unique title. If the title of that article appears in another article,
>we want to apply a link to the text, but only the first instance of
>the text.
>
>My thought is that we have to:
>
>1. create an array of all titles
>2. create a variable that holds the article text
>3. loop through the title array and search the article text for a match
>4. the first instance that a match is found, append the link and
>update the string variable
>
>The trick is that when a new article is added, they system needs to
>automatically go back to all of the existing articles and run through
>this process, which I don't like, because this won't be 100% accurate
>in the editor's eyes. each updated article needs to be reviewed. I
>also think it would cause a performance hit and dramatically slow down
>the system, especially in one installation where we have nearly 4,000
>record that would need to be updated.
>
>Personally, it seems to me that JavaScript is a better solution here,
>and to simply append those links when the page loads. Unfortunately,
>these sites all have to comply with Section 508 and other
>accessibility guidelines, so that might not be an acceptable solution.
>
>Any suggestions?
>
>--
>Randal Rust
Randal:
The way I would handle this would be like so:
Create a database that would contain a table that would hold your
articles. The table would have an index (ID), title of the article,
and the meat of the article. It could have other fields, but for sake
of demonstration let's use just those three.
Normally if one wanted to see an article all they would have to know
is the ID of the article and send that ID to a script that would
display the article -- something like display-article.php&id=215.
Note I am sending the id of the article and not the title of the
article to the display script.
With that said, all that would be needed would be a one-time search
through all the articles looking for all the titles you have in your
article table . This could be accomplished with a FULL TEXT search.
When a title is found, then simply add text to each title that would
make it a link. For example, if your script found "History of
America" with an ID of 215 and then the script would alter that
string like so:
<a href="display-article.php&id=215">History of America</a>
As such, your article linking problem would be solved.
Now if you add another article, then the process needs to be reset
and redone. In other words, find all links and remove them leaving
the titles.
The only fly in the ointment is if your articles already have links
-- if so, then you could get around that problem by simply adding a
class attribute to your new links, like so:
<a href="display-article.php&id=215" class="dyn-link">History of America</a>
A such, when the script finds a link with a "dyn-link" class, then it
simply removes that link and returns the article from above to
"History of America". Note that class attributes don't alter anything
unless you specify them in your css/
Then rinse, repeat and the problem is solved. This would work and
only needs to be "rinsed and repeated" when new articles are added OR
when old articles are removed. Additionally, this could be done in a
instant by locking the table, doing the update, and unlocking the
table without any noticeable interruption.
Cheers,
tedd
PS: No javascript required.
--
-------
http://sperling.com http://ancientstones.com http://earthstones.com
More information about the talk
mailing list