Avatar of PeterdeB
PeterdeB
Flag for Netherlands 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
// 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

MySQL Server

Avatar of undefined
Last Comment
PeterdeB

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
johnsone

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
PeterdeB

ASKER
Wow, so much code and yet your answer consists out of a very short line, and you got it right straight away, thanks
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes