troubleshooting Question

Why does this function not return partial results?

Avatar of PeterdeB
PeterdeBFlag for Netherlands asked on
MySQL Server
2 Comments1 Solution312 ViewsLast Modified:
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
// 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
johnsone
Senior Oracle DBA

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros