Specifications
else
$k_string .= “k.keyword = ‘“.$k[$i].”’ “;
}
$and .= “and ($k_string) “;
}
This code uses the PHP function split() to create an array containing each word in the
$keyword string separated by a space character. If only one word is specified, it still returns a
single element array and the subsequent loop is executed once. Ultimately the condition stored
in $and will look something similar to
and (k.keyword = ‘keyword1’ or k.keyword = ‘keyword2’ or k.keyword =
‘keyword3’)
The search query built based on the previous code would be
select s.id,
s.headline,
10 * sum(k.weight) / $num_keywords as score
from stories s, keywords k
where s.id = k.story
and (k.keyword = ‘keyword1’
or k.keyword = ‘keyword2’
or k.keyword = ‘keyword3’)
group by s.id, s.headline
order by score desc, s.id desc
The calculation for the score is the sum of the weights from all matching keywords divided by
the number of keywords searched for and then multiplied by ten. This favors searches in which
all the keywords entered match the keywords in the database.
Because the weights range from 1 to 10, the maximum value for the score is 100. A search for
three keywords would only be a 100% match with a story if all three were found for that story
and each had a weight of 10.
Editor Screen
The only part of the system we haven’t covered is how a story actually gets published after it
has been written. The script publish.php makes a story live. It is shown in Listing 26.10.
LISTING 26.10 publish.php Lists All Documents so the Editor Can Choose Which Ones
Are Shown on the Live Site
<?
include (“include_fns.php”);
Building Practical PHP and MySQL Projects
P
ART V
614
32 7842 ch26 3/6/01 3:36 PM Page 614










