PeterdeB
asked on
Why does this function not return partial results?
Hi all
Can anyone alter this function so that it will return partial results, and explain to me why it does not yet do this?
It does return searches like : 'applications', but if you use 'applications, components' or 'applications components' i want it to return two results, instead of just one, or in this case it returns no result at all
Let me know if any of the details are missing, and I will add them gladly, tried to be as precise as possible
Can anyone alter this function so that it will return partial results, and explain to me why it does not yet do this?
It does return searches like : 'applications', but if you use 'applications, components' or 'applications components' i want it to return two results, instead of just one, or in this case it returns no result at all
Let me know if any of the details are missing, and I will add them gladly, tried to be as precise as possible
// the contents of the database
INSERT INTO `articles` (`article_id`, `title`, `description`, `url`, `keywords`) VALUES (1, 'Free delphi components', 'All delphi components at my site are for free always', 'http://www.delphi7.nl/components.php', 'delphi, delphi7, delphi 7, components'), (2, 'Delphi applications', 'All delphi applications can be downloaded at my site are for free always', 'http://www.delphi7.nl/applications.php', 'delphi, delphi7, delphi 7, applications');
the submitform
<?php
if (isset($_POST['keywords'])) {
$suffix = "";
$keywords = mysql_real_escape_string(htmlentities(trim($_POST['keywords'])));
$errors = array();
if (empty($keywords)) {
$errors[] = 'Please enter a search term';
} else if (strlen($keywords)<3){
$errors[] = 'Your search term must be three or more characters';
} else if (search_results($keywords) === false) {
$errors[] = 'Your search for '.$keywords.' returned no results';
}
if (empty($errors)) {
$results = search_results($keywords);
$results_num = count($results);
$suffix = ($results_num !=1) ? 's' : '';
echo '<p>Your search for <strong>', $keywords, '</strong> returned <strong>', $results_num, '</strong> result', $suffix, '</p>';
foreach($results as $result) {
echo '<p> <strong>', $result['title'], '</strong> <br> ', $result['description'], '... <br> <a href="', $result['url'], '" target="_blank">', $result['url'], '</a> </p>';
}
} else {
foreach($errors as $error) {
echo $error, '</br>';
}
}
}
?>
CREATE TABLE IF NOT EXISTS `articles` ( `article_id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(155) NOT NULL, `description` text NOT NULL, `url` varchar(255) NOT NULL, `keywords` varchar(55) NOT NULL, PRIMARY KEY (`article_id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=10;
function search_results($keywords) {
$returned_results = array();
$where = "";
$keywords = preg_split('/[\s]+/', $keywords);
$total_keywords = count($keywords);
foreach($keywords as $key=>$keyword) {
$where .= "`keywords` LIKE '%$keyword%'";
if ($key != ($total_keywords - 1)) {
$where .= " AND ";
}
}
$results = "SELECT `title`, LEFT(`description`, 70) as `description`, `url` FROM `articles` WHERE $where";
$results_num = ($results = mysql_query($results)) ? mysql_num_rows($results): 0;
if ($results_num === 0) {
return false;
} else {
while ($results_row = mysql_fetch_assoc($results)) {
$returned_results[] = array(
'title' => $results_row['title'],
'description' => $results_row['description'],
'url' => $results_row['url']
);
}
return $returned_results;
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER