Posted in site bugs
24790
2:44 am, January 12, 2021
 

linking search results to view pages [finished] 🤣

I was thinking it might be useful to link the search term to the page, so that it can be found more easily using that search.

For example, Link this search text (somehow) to the post page and display it as a search link tag. 

https://kruxor.com/search/code/linking+search+results

So someone will come along to the search page and type in "linking search results" 

Then this search term will have to be sent along to that page when its clicked in the search results.

Then it can be added to another table called "search links" or something similar, and maybe add a count number to it as well so can see how many times it has been searched if it already exists in the table for that link. 

What will we be storing?

Nice Names

  • Search Text
  • Post UID
  • Post Class
  • View Number

How will it attach to the post?

Pass it back as a string to the search results page, so will need to change each view link in the search results to include the search text url encoded.

Instead of the string to view the page being:

https://kruxor.com/view/code/tlBLq/linking-search-results-to-view-pages-idea/ it will change to 

https://kruxor.com/view/code/tlBLq/linking-search-results-to-view-pages-idea/&search_text=linking+search+results 

How to make it happen?

  1. Create new class and table to store the new data. Called search_links
  2. Modify the current search listing page to include the encoded search text in the view links
  3. Change the view page to check for the incoming encoded search link
  4. If it finds the link and it does not already exist then add it
  5. if it find the link and it does already exist then update the view count
  6. Change the view page to include the related search links if it has any
  7. Other Notes and After Thoughts on this Idea

1. Create new class and table to store the new data. Called search_links

DB Field Names

  • search_text
  • post_uid
  • post_class
  • view_number

This should be enough data to link it to the post. This class extend creates the SQLite database and also the variables named as the field names above.

2. Modify the current search listing page to include the encoded search text in the view links

This is probably a strange order to add this change, as i still have not added a way to import the results, but its easier to do it in this order to test it. 

I think there is a default template where I can add the encoded link onto the view url. It may not work correctly for all classes. 

The search results page uses the list_all function passing in the following.

So its already including the "search" and also the $search_term so that function will need to be changed to encode the search term and then add it to the link. 

Added a urlencoded version of the search term and also a blank variable, incase the search term is not needed. 

Add the new item to the template output.

Now find the template that generates the url to view. 

Seems to be in core-list-item.html

Added the new link, now test to see if its actually working and if anything is broken. What happens if there is no search_text.

Testing on this link, view the source we can see that the search url encoded is being passed through. 

What happens if i add a bunch of random characters to the search. does this break it?

Test Link: https://kruxor.com/search/code/linking+search+results 

Seems to be ok, even passing emoji's to it works. As the search is sql encoded anyway so it cant do any damage.

I think that means that you could actually search by emoji! :) not sure if thats useful.

Hmm search by emoji does work.

It even encodes it into the url. Not sure how cross platform compatable this is. I guess emojis do work cross platform. 

Emoji Test Link: https://kruxor.com/search/code/%F0%9F%A4%A3 

Sorry got distracted with emojis, ok on to the main task. We now have the search term linking into the page so next step.

3. Change the view page to check for the incoming encoded search link

Im having some issues with this step, it seems that if i send back &search_text=word that works ok with $_GET['search_text'] but then if i send back &search_text=word+word it does not return properly.

I tried using the urlencode and rawurlencode which uses %20 rather than + for the space encoding, but still does not work correctly.

Testing on the view page using var_dump($_GET); and also

if(isset($_GET['search_text'])) {
	echo $search_text_raw = $_GET['search_text'];
}

I think the script i use to grab the nice page strings is breaking the normal way $_GET works so im going to do something a bit different.

Ill try adding the values as search_term:::word%20word and see if that works and totally bypass the $_GET

Now $p5 is set to search_term:::word%20word just need to check that its a search term and then add it to the db i think.

Check each page variable for the string "search_term:::" if its found then add/update it as a search term. Can search for this string using strpos. Details on strpos here

$search_string_match = "search_text:::";
if($p5 > "") {
	if (strpos($p5, $search_string_match) !== false) {
	    echo 'true';
	}
}

Have to keep plugging away at this one so i dont forget about it! More effort than i thought it would be, but i started now...

4. If it finds the link and it does not already exist then add it

Now that we have the string "search_term:::" have to extract the words from it and then check if they are valid. 

I tried this code, but its not actually returning anything as i was passing in the wrong variable into it. 

/* Search String Matching */
$search_string_match = "search_text:::";
if($p5 > "") {
	if (strpos($p5, $search_string_match) !== false) {
	    // echo 'true';
			// add search recording and updating here !!!!!!!! !!!!!

			$search_text_array = explode ( ":::" , $search_string_match );

			var_dump($search_text_array);

	}
}
/* Search String Matching */

* side note ^^ add this to a template for easy code formatting insertion to tinymce

So the correct version of this is... 

/* Search String Matching */
$search_string_match = "search_text:::";
if($p5 > "") {
	if (strpos($p5, $search_string_match) !== false) {
	    // echo 'true';
			// add search recording and updating here !!!!!!!! !!!!!

			$search_text_array = explode ( ":::" , $p5 );

			var_dump($search_text_array);

	}
}
/* Search String Matching */

using $p5 rather than $search_string_match and then i get a correct result. 

Test Link: https://kruxor.com/view/code/qezOG/explode-a-string-into-an-array/search_text:::string%20into%20an%20array 

Returns:

array(2) { [0]=> string(11) "search_text" [1]=> string(20) "string into an array" }

Which is what im looking for! Finally. Hopefully this does not break too easily depending on what is passed to it.

Test that we have the correct variable from the array.

/* Search String Matching */
$search_string_match = "search_text:::";
if($p5 > "") {
	if (strpos($p5, $search_string_match) !== false) {
	    // echo 'true';
			$search_text_array = explode ( ":::" , $p5 );
			$search_text = $search_text_array[1];
			// var_dump($search_text_array);
			echo "\$search_text:$search_text";
	}
}
/* Search String Matching */

and we should be good to move onto the next step.

5. if it find the link and it does already exist then update the view count

/* Search String Matching */
$search_string_match = "search_text:::";
if($p5 > "") {
	if (strpos($p5, $search_string_match) !== false) {
	    // echo 'true';
			$search_text_array = explode ( ":::" , $p5 );
			$search_text = $search_text_array[1];
			// var_dump($search_text_array);
			// echo "\$search_text:$search_text";

			$search_links = new search_links;
			$search_links->add_to_menu = false;
			$search_links->start();

			// how are we going to load this, via the title or search link.
			// note ** the load_from_title already escapes the string so its sql safe.

			// can replace this laoder with the load_from_fields_two to check search_text and also uid which is unique.
			// if($search_links->load_from_title($search_text)) {
			if( $search_links->load_from_fields_two(
				$field_one_name = "search_text",
				$field_one_value = $search_text,
				$field_two_name = "post_uid",
				$field_two_value = $p3 )
			) {

				// we already have this as search text so add to the count.
				// may need to also check the post_uid and post_uid to have unique per search.
				// currently this only allows to add one search string for all posts so need to add another check in here for the uid and class.
				// load_from_fields_two($field_one_name, $field_one_value, $field_two_name, $field_two_value )
				$search_links->view_number = $search_links->view_number + 1;
				$search_links->update();

			} else {
				// search text does not exist so add it.
				$search_links->title = $search_text;
				$search_links->search_text = $search_text;
				$search_links->view_number = 1;

				// get the post data as well. these can be loaded from the current post.
				$search_links->post_uid = $p3;
				$search_links->post_class = $p2;
				$search_links->category = $class->category;

				// then once ready.. add it.
				$search_links->add();

			}

	}
}
/* Search String Matching */

6. Change the view page to include the related search links if it has any

I always get to the stage here when i have been working on something on and off for a while, where i just want to move onto the next thing. Finish this one thing 1st! :)

Last Step.

On the view pages, list the related search tags.

19 Jan 2020

Added new listing function for the search items:

PHP

public function list_related_searches (
    $start = 0,
    $max = 50,
    $list_type = "latest",
    $template_file = "search_links-list-item-compact.html",
    $post_uid = "",
    $post_class = ""
  ) {

    global $functions;
    global $base_directory;

    $db_table_name = $this->db->escapeString($this->db_table_name);
    $start = $this->db->escapeString($start);
    $max = $this->db->escapeString($max);
    $post_uid = $this->db->escapeString($post_uid);
    $post_class = $this->db->escapeString($post_class);

    if($list_type == "latest") {
      $sql = "select * from $db_table_name where post_uid = '$post_uid' and post_class = '$post_class' order by insdate desc limit $start,$max";
    }

    if(!isset($sql)) {
        return "No List Found";
    }

    $out = "";
    $result = $this->db->query($sql);
    while($row = $result->fetchArray()) {

      $template = new template($template_file);
      foreach($this->load_array as $load_title) {
          $template->set($load_title, $row[$load_title]);
          $template->set("title_escaped", htmlentities($row['title']));
          $template->set("search_text_enc", urlencode($row['search_text']));
      }
      $out .= $template->output();

    }
    return $out;

  }

New Template

<div class="core-link">
  <a href="/search/[@post_class]/[@search_text_enc]/" class="list-group-item list-group-item-action text-truncate" title="[@title_escaped]" data-toggle="tooltip">
    <span class="badge badge-info"> Posted in  site bugs </span>
    [@title_escaped] <span class="number-inline">[@view_number]</span>
  </a>
</div>

Then on the view page, add a search for the related search terms and list them.

/* Load and List Search Matches */
$search_links = new search_links;
$search_links->add_to_menu = false;
$search_links->start();

$search_links_html = $search_links->list_related_searches(
	$start = 0,
	$max = 500,
	$list_type = "latest",
	$template_file = "search_links-list-item-compact.html",
	$post_uid = $p3,
	$post_class = $p2
);
if($search_links_html > " ") {
	$s_card_template = new template("card-title-mb-3.html");
	$s_card_template->set("title", "Related Search Terms");
	$s_card_template->set("content", $search_links_html);
	$search_links_html = $s_card_template->output();
}

/* Load and List Search Matches */

Then finally add them to the page content, so they list like so:

// add search related links to every page
$page_content .= $search_links_html;

The Final Result:

You should now be able to see something similar at the bottom of each post, the full search string with the number of search times added to it. 

7. Other Notes and After Thoughts on this Idea

** its adding and updating for multiple pages, just need to list the searches under each page now.

** also add the code for adding and updating using core functions.

So i checked the results the next day and as expected, its full of random spam searches for example.

https://kruxor.com/list-table/search_links/ 

What can we do to prevent this kind of spam and make it more useful. I think what the bots do is find a link, like head and then follow each of the links that it finds. Which is ok but how to prevent "head" being added 4 million times for each different link. or is this actually useful. 

to get rid of the 1's and useless small keyword searches we can add a filter on the search term that it is recording. 

Additional Rules to add:

  • if the search is a number then dont add it, no point recording numbers.
  • if the search is less than 2 characters dont add it. 
  • only allow search link recording from certain post types, like "code", "content", "linx" and skip things like "nz postcodes".
  • i think the head search is ok to add, as it is relevent to the post.

Here are the additional rules added to stop the search item from being added if it has any of these issues.

// set add_search_item to true and then check for certain things below and set it to false if required
$add_search_item = true;

// if the text length is less than or equal 2 dont add it
if(strlen($search_links->search_text) <= 2) {
	$add_search_item = false;
}
// if the text length is greater than or equal 50 dont add it
if(strlen($search_links->search_text) >= 50) {
	$add_search_item = false;
}
// if the search is a number dont add it
if(is_numeric($search_links->search_text)) {
	$add_search_item = false;
}

// then once ready.. add it.
if($add_search_item) {
	$search_links->add();
}

 

 

View Statistics
This Week
604
This Month
3954
This Year
7989

No Items Found.

Add Comment
Type in a Nick Name here
 
Related Search Terms
Search Code
Search Code by entering your search text above.
Welcome

This is my test area for webdev. I keep a collection of code here, mostly for my reference. Also if i find a good link, i usually add it here and then forget about it. more...

Subscribe to weekly updates about things i have added to the site or thought interesting during the last week.

You could also follow me on twitter or not... does anyone even use twitter anymore?

If you found something useful or like my work, you can buy me a coffee here. Mmm Coffee. ☕

❤️👩‍💻🎮

🪦 2000 - 16 Oct 2022 - Boots
Random Quote
“We ought to take outdoor walks in order that the mind may be strengthened and refreshed by the open air and much breathing.".
Seneca
Random CSS Property

speak-as

The speak-as descriptor specifies how a counter symbol constructed with a given @counter-style will be represented in the spoken form. For example, an author can specify a counter symbol to be either spoken as its numerical value or just represented with an audio cue.
speak-as (@counter-style) css reference