[thelist] Online Glossary Database (PHP/MySQL))

Gijs van Tulder evolt at gmx.net
Sat Jan 25 05:50:01 CST 2003


Hi Jennifer,

> So, when viewing "gloss term", we would like the word
> "things" to be a link that would take you to the definition
> for "things". And clearly I have no intention of doing all
> this cross-referencing on my own. Does anyone know how I
> might go about accomplishing this (PHP/MySQL)?

If you don't want to do it yourself, you'll need a script that does this for
you. This script will have to look up each word in the definition in the
database. You could do something like this:

<?php
$word = 'things';

// get definition from database
$result = mysql_query("SELECT definition FROM words WHERE word='$word'");
$definition = mysql_result($result, 0, 'definition');

// split definition in words
$words = explode(' ', $definition);

// look up each word
for ($i=0, $i<count($words); $i++) {
	$result = mysql_query("SELECT word FROM words WHERE word='".
	                      $words[$i]."'");
	if (mysql_num_rows($result)==1) {
		// this word has a definition
		// make a link
		$words[$i] = '<a href="glossary.php?word='.$words[$i].'">'.
		             $words[$i].'</a>';
	}
}

// join the words and tags
$linked_definition = join(' ',$words[$i]);

// display results
echo "Word: $word<br>\nDefinition:<br>\n$linked_definition";
?>

This obviously isn't very efficient. For each pageview, the script has to
link the definition. You might want to run the script once, and then save
the linked definitions in the database. The glossary script then just has to
display the definition.

HTH,

Gijs




More information about the thelist mailing list