We help IT Professionals succeed at work.

Why does this function not return partial results?

PeterdeB
PeterdeB used Ask the Experts™
on
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');

Open in new window

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>';
}
}
}
?>

Open in new window

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;

Open in new window

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;
}
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior Oracle DBA
Commented:
It looks like you are combining your where searches with an AND.  You should probably change that to an OR.

Author

Commented:
Wow, so much code and yet your answer consists out of a very short line, and you got it right straight away, thanks