Insert Search Result on a Page

Hi,

I am building some normal contant pages, and under my the “normal HTM content” I would like to include pictures from a relevant predetermined search. Kind of like “Insert results” instead of “insert alblum”

You can see what I am trying to do below - not to display the link of the search, but the actual pictures of the search.

https://tasmanianphotos.com/ben-lomond/

Is that possible? jQuery or some other script to append this search result to my page?

Cheers,
Chumby

I can’t think of a good way to do this at the moment. Maybe later, if we ever integrate keyword search into the JSON API. Not sure when or if that will happen, though.

For now, the best way is probably to put the images you want into an album and embed it.

Thanks Matthew,

Yes pretty keen to see if I can get this working. My site heavily relies on search/metadata and to an extent that is what creates my “albums”.

I will see if there is some Javascript / JQuery code which may be able to do the trick and post here if I get a solution. My single image pages are not getting indexed at all (becasue they are all the same) so I need to generate pages with unique content with associated images.

Appreciate you help as always and am really enjoying BL3.

Cheers,
Chumby.

Hi,

Playing around with phplugins and have come up with the code below, based on Daniel’s idea to be able to add specific PHP code to individial pages.

Getting a syntax error on the line: echo ‘$html = file_get_contents(’/backlight/search/q=ben+lomond’)’;

(Unexpected error: syntax error, unexpected ‘=’, expecting ‘,’ or ‘;’ in my-phplugins.php on line 622)

but cant see why…

Any ideas?

Cheers,
Chumby

// helper function to determine page

function dlp_page_match($page) {
if (substr($_SERVER[“REQUEST_URI”], 0, strlen($page)) == $page) {
return 1;
} else {
return 0;
}
}

// Unique php per page - needs function dlp_page_match

function main_bottom( ){
if ( $this->dlp_page_match(’/about’) ) {
echo ‘…’;
} elseif ( $this->dlp_page_match(’/ben-lomond/’) ) {
echo ‘$html = file_get_contents(’/backlight/search/q=ben+lomond’)’;
echo ‘$html’;
}
}

try escaping these single quotes
’/backlight/search/q=ben+lomond’

Hi @Chumby,

Please add your code as ‘preformatted text’. You can click the </> icon to get it. This way, your quotes are not translated into smart quotes and the code looks a bit nicer.

function main_bottom( ){
   if ( $this->dlp_page_match(’/about’) ) {
      echo '…';
   } elseif ( $this->dlp_page_match('/ben-lomond/') ) {
      $html = file_get_contents("/backlight/search/q=ben+lomond");
      echo $html;
   }
}

As @rod_barbee indicated, you use a string delimited by single quotes inside your echo statement that uses single quotes too. Either escaping or double quotes should be used. In my example above, I use double quotes.

I don’t think that file_get_contents works for the backlight search page. You might want to look at using curl. Have a look at my example for fetching JSON at https://lab.danielleu.com/blog/using-backlight-3-json-api-from-php-take-2/. By removing json_decode() should get you the HTML code of the search.
Please note that some hosts block curl requests originating and targeting their own server!

Additionally, you will need to strip a lot of data from $html since you don’t want to embed an entire page.

1 Like

Hi Daniel and Rod,

Thank you very much for this.

Dan I have looked at your code on your JSON explanation page and come up with the below. Not sure how the two functions are suppose to exist - or whether I merge this code into one function?

function dlp_get_json_response($url){
   $curl = curl_init();
   curl_setopt_array($curl, array(
      CURLOPT_RETURNTRANSFER => 1,
      CURLOPT_URL => $url,
      CURLOPT_HTTPHEADER => array('Content-Type: application/json'),
      CURLOPT_FAILONERROR => true,
      CURLOPT_SSL_VERIFYHOST => 0,
      CURLOPT_SSL_VERIFYPEER => 0
   ));
   $posts = curl_exec($curl);
 
   // Was an error detected?
   if (curl_errno($curl)>0){	 
      $response = Array();
      $response['status'] = 'error';
      $response['code'] = curl_getinfo($curl, CURLINFO_RESPONSE_CODE);
      $response['message'] = curl_errno($curl).': '.curl_error($curl);
      $response = $json;
      }		  
   curl_close($curl);
   return $response;	
}  
    
function main_bottom( ){
   if ( $this->dlp_page_match('/about') ) {
      echo '…';
   } elseif ( $this->dlp_page_match('/ben-lomond/') ) {
      echo '$html = file_get_contents("/backlight/search/q=ben+lomond")';
      echo '$html';
   }
}

At the moment I am only seeing a printout (echo) on the screen of the two lines of code. Any help or pointers here appreciated.

Cheers
Chumby

Give me a day or so to have a look at this.

Brilliant - appreciate your efforts, Daniel. No worries.

Hi Daniel,

Did you get to have a look yet?

Cheers,
Chumby

Nope, not yet. Sorry!

All good, Daniel.

Hey Chumby,

I got it working! You’ll find the steps over on my blog at https://lab.danielleu.com/blog/embedding-photo-search-results/

Fantastic work, Daniel! I will check it out over there!

Cheers,
Chumby

Hi Daniel,

Have it “half” working - looks like the search is working and clicking through to a single page - just not getting the thumbnails displaying. See:

https://www.tasmanianphotos.com/ben-lomond/

Would like this returned search to appear similar to a regualr search (but under my page “ben-lomond content”)

https://www.tasmanianphotos.com/backlight/search/?q=ben+lomond

Any ideas what I need to adjust?

Cheers,
Chumby

Hi Chumby,

I guess you overlooked a little hint I wrote at the end of my post:

If you’d like to use this smart album in a regular page, you need to add an empty album in Backlight in order to have the proper css and javascript, and use the ‘main_top’ or ‘main_bottom’ phplugins function.

I created an album called ‘empty’ just for such purposes.

Hi Daniel,

I am just having a little bit of trouble understanding that part of your instructions. My ‘main_bottom’ code appears as:

function main_bottom( ){
   if ( $this->dlp_page_match('/ben-lomond/') ) {
      echo $this->dlp_get_smart_album('"ben"+"lomond"');
   }
}

Is there more code that I need to add to that from an album called ‘empty’? Or am I on the wrong track here?

Cheers
Chumby

The search pattern works. In Backlight Page designer, you need to insert an empty album. This way you get the necessary javascript and css code.

Let’s move the discussion over to the comment section of the blog post.

1 Like

No worries… goodo!

OK. Massive shoutout to Daniel as we now have this “smart album” idea working. Thanks heaps!

To see it working…

  • Navigate to https://www.tasmanianphotos.com
  • Select “Tasmania” from the top drop down menu
  • Navigate to “National Parks” - “Mount Field” on the menu and you should see a mix of text content and images
  • Then navigate to “Nature” - “Rainforest” and you again will see text content and images, including images that were also displayed in the “Mount Field” … “smart album”.

You can do the same for “Snug” (notice a couple of images of beaches) and “Beaches” (notice a couple of images from Snug)!

All images being displayed on these content pages are based on metadata and search queries! Hundreds of pages with smart albums can be created this way!

Cheers,
Chumby

2 Likes