Optional Extras

Random snippets

The image and text below the navigation menu is selected at random from a user-created series of "extras". The extras are created and saved in the admin panel and there is no limit on quantity. They can be edited or deleted, and the quantity to be made available for selection can also be defined.

In other words, if there are 50 extras, all 50 can be selectable at random or the quantity can be set to a lower number - down to zero if none is required. Obviously, the greater the quantity, the greater the variety and the lower the chance of the same extra appearing on two consecutive page views.

On this website the system is used to generate random snippets that draw attention to the content of other pages. Also, some people believe that the mere fact that part of the content of a web page changes on a frequent basis helps it to rank in search engine results.

Site search

The search box can be used to search for keywords and phrases relating to the content of the site's pages. Rather than search through the whole text of all the pages, the system searches a user-defined list of keywords and phrases. Provided the list is created with care, this is likely to return search results of the most relevance, as determined by the webmaster.

The search script is simple enough:

// Convert case and trim ends
$theSearch = trim(strtolower($_POST['search_term']));
// Strip out extra spaces in search string
$theSearch = preg_replace("/[ ]+/", " ", $theSearch);
// Check for a result
$number = str_word_count($theSearch);
if ($number == 0) {
  $problem = true;
  echo "You didn't enter a search term.\n";
} else {
  echo "Pages found for $theSearch\n\n";
}
// If something was searched for...
if (!$problem) {
  $filename = "path/search_index.txt";
  $fp = fopen($filename, "r") or die("No $filename.");
  // Loop through all the lines
  while (!feof($fp)) {
    $line = fgets($fp, 1024);
    $line = strtolower($line);
    // Check each line for search term
    if (preg_match("/b$theSearchb/", $line)) {
      $result = true;
      // Convert line string into array
      $lineArray = explode("|", $line);
      // Print first element
      echo '<* href="' . $lineArray[0] . '" 
      title="' . $lineArray[1] . '">' . $lineArray[1] . '';
      echo "\n";
      // Slice off after 2nd element to new array
      $keywords = array_slice($lineArray, 2);
      // Loop through each array for the keywords
      echo "Keywords: ";
      foreach ($keywords as $val) {
        echo " $val ";
      }
      echo "\n";
    } // End of match conditional
  } // End of while loop
  if (!$result) {
    echo 'No match was found for ' . $theSearch . '.';
    echo "\n";
  }
}

Each line of the search index file is written thus (eg):

/optional-extras|Optional Extras|random snippets|search|etc|

For older people and pensioners who might be struggling to remember their PHP scripts, help is at hand: the handy PHP aide-memoire...

Read more »