[SOLVED] Search issues

Post Reply
ctuxboy
Ssr. Bludit
Posts: 25
Joined: Sat Dec 15, 2018 11:37 pm
Has thanked: 4 times
Been thanked: 2 times

Hello,

I made a search-page (search.php), for the search results.

Adding this to the index.php page

Code: Select all

<?php
if($WHERE_AM_I == 'search'){
      include(THEME_DIR_PHP.'search.php');
    } else {
      if ($WHERE_AM_I == 'page') {
	 include(THEME_DIR_PHP.'page.php');
      } else {
	 include(THEME_DIR_PHP.'home.php')
      }
    }
?>
1. when test the search function, not only the matched page(s) are showing, but also all the other pages showing after the search results.

2. How can i show the search-word on the search.php?
In the url i see this https://mydomain.com/search/my_search_word

That is what i try to do:

Results for MY_SEARCH_WORD

Here you can see my test-website: https://gewoonsimpel.be/search/brood
Last edited by ctuxboy on Sat Jan 07, 2023 10:57 am, edited 1 time in total.
ctuxboy
Ssr. Bludit
Posts: 25
Joined: Sat Dec 15, 2018 11:37 pm
Has thanked: 4 times
Been thanked: 2 times

The issues are solved :)
A friend, professional developer (Kenny Liard), helped me a lot.
I will post here the solutions, so it can help others:

1. Search problem, that not only shows the matched posts:

Now the search plugin works correct, shows only the matched posts.
Replace the code in the private function search($text) in the plugin.php:

Code: Select all

private function search($text)
	{
		// Read the cache file
		$json = file_get_contents($this->cacheFile());
		$cache = json_decode($json, true);

		// Inlcude Fuzz algorithm
		require_once($this->phpPath().'vendors/fuzz.php');
		$fuzz = new Fuzz($cache, 10, 0, true);
		$results = $fuzz->search($text, $this->getValue('minChars'));
		
		$filtered_results = array();
		for ($i=0; $i < count($results); $i++) { 
			if (array_values($results)[$i] == 0) {
				array_push($filtered_results, array_keys($results)[$i]);
			}
		}

		return($filtered_results);
	}
2. print the searchword, adding this in the breadcrumb-code did the job:

Code: Select all

<a href="<?php echo Theme::siteUrl() ?>">Home</a> » Resultaten voor: 
	<?php
	   $searchword = substr($url->slug(), 7);
	  echo '<strong>' . $searchword . '</strong>';
	?>
See it in action on my website: https://gewoonsimpel.be/search/brood

So it can help someone :)
ctuxboy
Ssr. Bludit
Posts: 25
Joined: Sat Dec 15, 2018 11:37 pm
Has thanked: 4 times
Been thanked: 2 times

Found another search issue:
All the pages with a page-description, don't shows up in the search results.

So i did a small 'hack' in this function: private function createCache() in plugin.php

On line 208, change this code:

Code: Select all

$cache[$pageKey]['description'] = $page->description();
To this:

Code: Select all

$cache[$pageKey]['description'] = '';
Now the search works correct :)
Post Reply