Forum Moderators: coopster
in my db i have a table for articles and another for a glossary style widget defintion list.
i would like to add to this so when an article is submitted in the cms, the processing script reads it and adds appropriate hrefs prior to submitting to database and lets me review the additions prior to INERT INTO stage.
also, id like to be able to match e.g. 'red widget' linking to 'widgets, red'
i have only done very (very) basic sites in php/mysql, and dont know how to approach this.
any pointers much appreciated. thanks
ben
So I wanted to create a glossary, and each word on any page that matched a term in the glossary would automatically become a hyperlink linked to the definition of the term.
So I came up with a couple of possibilities, each with their own set of problems.
The first was to do what you've suggested. Though this would mean that each time I add a word to the glossary (because it will never be complete and will always grow), I would have to go through my whole database and add that link on each article in which the term exists.
The second was to create a scripting language to highlight these terms, but it creates the same problem as the first because it requires either going back and making modifications as the glossary changes, or having a preconceived notion of what glossary terms will appear in the future.
The third option gave me the ideal situation of having the ability to dynamically update the glossary and have it have an instantaneous site-wide affect, but it would put an incredible load on the server. The idea was to compare every word on the page with every word in the glossary and determine if it existed in the glossary to convert it to a link, realtime as the page was requested. But on a page with 1,000 words and a glossary of 100 terms, this would result in 100,000 string comparisons as the page was loading, and potentially many more as the glossary grew or for pages with more text. But other than server load and processing time, I really liked the power of this option, so I worked on it.
So I started by writing my script that would filter out all of the stop words that wouldn't be in the glossary and I would not need to compare them with each glossary term (of,it,in,on,out,a,the, etc). Next, a created a string that was all of the glossary terms concatenated, and then I check each word on the page to see if it existed in this string. If it did, it was added to an array for later processing. Once going all the way through each word on the page, I went through this array to compare it against each word in the glossary.
In doing it this way, I now have only slightly more than one string comparison per word on the page instead of 100 (or however many glossary terms there are). Has worked very well for months now :-).
id pretty much discounted option3 , cause as you say the load on the server would be immense.
i hadnt really considered what to do when adding to the gloassary(doh). i was more concerned about adding articles. im glad you bought it up.
as i said i was hoping to scan the articles prior to submission to put the links in. do you think it would possible when adding glossary terms to scan back through all articles in the database and update them?
as i said, im not very experienced with php/sql. any pointers on how you approached this technically?
thanks
ben
$myText = <text from database>;
$parsedText = eregi_replace ("(word1Śword2Śword3)", "<A href=\"glossary.html?word=\\1\">\\1</A>", $myText);
echo $myText;