Trying to display a thumbnail on search results

Permalink 1 user found helpful
Hi There,

I have a recipe search engine and need to display a page thumbnail as part of the search results - the thumbnail is a page attribute.

I looked at a page_list custom template and tried to transfer the code but get php error when trying to search.

Here is the results template with the image attribute code highlighted:
<div id="searchResults">
      <?php  foreach($results as $r) {
         // ADDED ---------------------------------------
         // load image helper
         $imageHelper = Loader::helper("image");
         // load page attribute 'main_image' and apply thumbnail size
         $thumbnail = $cobj->getAttribute('main_image');
            if ($thumbnail) {
               $thumbnailObj = $imageHelper->getThumbnail($thumbnail, 240, 155);
               }
         // END ADDED ---------------------------------------
         $currentPageBody = $this->controller->highlightedExtendedMarkup($r->getBodyContent(), $query);?>
         <div class="searchResult">
            <h4><a href="<?php echo $r->getPath()?>"><?php echo $r->getName()?></a></h4>
            <!-- ADDED -->


Any help would be much appreciated - is it possible?

 
pvernaglia replied on at Permalink Reply
pvernaglia
Try this

<?php
  $imgHelper = Loader::Helper('image');
  if($c->getAttribute('main_image')) {
    $imgHelper->outputThumbnail($c->getAttribute('main_image'),100 ,100);
  }
?>

http://www.weblicating.com/c5/cheat-sheet/...
cmscss replied on at Permalink Reply
Thanks for that, I've put that in to grab the image but I can't figure out how to output the thumbnail for each result.

Or maybe that code is trying to load 'main_image' from the current page (in this case the search results pages) instead of each result

I've tried the following:
<p><a href="<?php echo $r->getPath()?>"><img src="<?php echo $img->src; ?>" width="<?php echo $img->width; ?>" height="<?php echo $img->height; ?>"></a><p>


<p><a href="<?php echo $r->getPath()?>"><?php echo "<img src=\"{$thumbnailObj->src}\" alt=\"\" width=\"240\" />"; ?></a><p>


Anyway, hoping it can be done. Sorry, I didn't understand you have to have PHP knowledge to build a site using c5 (I'm used to CMS tags ala Modx or Expression Engine) so didn't allow for this and am now in the deep end with a deadline looming!

Any help would be much appreciated.
TheRealSean replied on at Permalink Reply
TheRealSean
You have used $thumbnail = $cobj->getAttribute('main_image');

I think you need to use, (assuming $r is the page object of each result page)

$thumbnail = $r->getAttribute('main_image');

Then all the other bits should work,

You are trying to get the Attribute of cobj which in the scope does not seem to exist.
cmscss replied on at Permalink Reply
Mate, I'm copying stuff from pages that work so have no idea what $r is!

$r is a variable right? So I guess on the pages where that code works, $r is defined somewhere at the top maybe?

Also I notice there are references to $c and $cob - are there references to Concrete5 and is $cob only used when you're pulling from the current page (and not from other pages)?
TheRealSean replied on at Permalink Reply
TheRealSean
in the top bit you have

<?php foreach($results as $r) {

this takes each page and gives each value of the array as r,

I have not looked at the search page, but if $r is the object for the search result page then you need to run the getAttribute on each search result page.

$c is often referred to as the collection(of the current page)
$cobj is used when running a foreach to differentiate the sub page from the current page.

This way when running a foreach on a page, like this one
if you used foreach $results as $c you would overwrite the current page object

But they are really just labels, so $cobj could be the current page. $c is used for consistency and is the c5 default
TheRealSean replied on at Permalink Reply
TheRealSean
Tweaked and added a div and class to the image
<div id="searchResults">
      <?php  foreach($results as $r) {
         // ADDED ---------------------------------------
         // load image helper
         $imageHelper = Loader::helper("image");
         // load page attribute 'main_image' and apply thumbnail size
         $thumbnail = $r->getAttribute('main_image');
            if ($thumbnail) {
               $thumbnailObj = $imageHelper->getThumbnail($thumbnail, 240, 155);
               }
         // END ADDED ---------------------------------------
         $currentPageBody = $this->controller->highlightedExtendedMarkup($r->getBodyContent(), $query);?>
         <div class="searchResult">
            <h4><a href="<?php echo $r->getPath()?>"><?php echo $r->getName()?></a></h4>
            <!-- ADDED -->
rainmaker replied on at Permalink Reply
rainmaker
Hey TheRealSeam!

I've gone over and over your posts a gazillion times in this thread. I've copied your code into my view.php for the search block, but I keep getting the same error:

Fatal error: Call to undefined method IndexedSearchResult::getAttribute() in /public_html/concrete/blocks/search/view.php on line 35

Which I'm thinking that just means that the Attribute just isn't there. I'm also trying to only look for thumbnails for images that are using the product_detail page ONLY. Which keeps bombing on me too. *sighs* HELP!!!!
TheRealSean replied on at Permalink Reply
TheRealSean
Is there any chance of getting a look at your code?,

I have not revisted this in a while so I'll take a look see if it needs updating.

The getAttribute() should be run on a page object if the attribute does not exist then this should return null.

Are you calling the getAttribute function on the page objects?

ie $r->getAttribute('value');
and not $r::getAttribute('value');

Is it possible you are then trying to run an extra method on a non existent variable?
ie $image = $r->getAttribute('value');
//assuming its null, the following I think would fail, not sure on the exact use of the file attributes
$image->getAttribute('extra_value');
TheRealSean replied on at Permalink Reply
TheRealSean
having a search it does look like the missing attribute may cause this issue.

With regards to your other issue the product_detail page only you should be able to take a similar approach to the page list block

In fact looking in the controller you can see the problem,
foreach($res as $r) { 
         $results[] = new IndexedSearchResult($r['cID'], $r['cName'], $r['cDescription'], $r['score'], $r['cPath'], $r['content']);
      }


So modifying the other code try the following,
$c = Page::getById($r->getID());
$c->getAttribute('main_image'));

thanks to rbnz for discovering that
http://www.concrete5.org/community/forums/usage/get-thubnail-attrib...

within the foreach loop
{
if($r->getCollectionTypeHandle()=="product_detail"){
   //continue..
}
}

It should be possible to create your own custom search block and modify the controller so that it filters out the other pages earlier, something like
$ipl->filterByCollectionTypeHandle('product_detail');
rainmaker replied on at Permalink Reply
rainmaker
Hey TheRealSean!

Thank ya thank ya for helping me out! Here's the code I've got so far:

<div id="searchResults">
      <?php  foreach($results as $r) { 
         // ADDED ---------------------------------------
         $oPage = Page::getById($r->getID()); //the ID of current result
         if($oPage->getCollectionTypeHandle()=="product_detail"){
              // load image helper
               $imageHelper = Loader::helper("image");
               // load page attribute 'main_image' and apply thumbnail size
               $thumbnail = $oPage->getAttribute('main_image');
             echo "Thumbnail ".$thumbnail;
                if ($thumbnail) {
                   $imageHelper->outputThumbnail($thumbnail, 118, 172); /* this outputs the image */ ?>
               ?>
               <img src="<?php echo $imageHelper->getThumbnail($thumbnailObj, 100, 100)->src ?>" alt="<?php echo $r->getName() ?>" style="border:1px solid red;" />
               <?php


I know I'm trying to output the thumbnail twice, but both aren't working. The page detail info works wonderfully! Just displaying the thumbnail isn't.... I don't think this line:

$thumbnail = $oPage->getAttribute('main_image');

...isn't quite right.
rainmaker replied on at Permalink Reply
rainmaker
Hey TheRealSean!

Update! I've got it where it pulls from a page attribute called "page_thumbnail". I'm trying to make it where the page_thumbnail is auto populated when the main image is uploaded. I can sure hope anyway. :-S

<div id="searchResults">
      <?php  foreach($results as $r) { 
         $oPage = Page::getById($r->getID()); //the ID of current result
         if($oPage->getCollectionTypeHandle()=="product_detail"){
             $ih = Loader::helper('image');
            $oPage = Page::getById($r->getID()); //the ID of current result
            $sTitle = $oPage->getCollectionName();
            $oThumb = $oPage->getAttribute('page_thumbnail');
            if(!empty($oThumb)){ 
            //execute the following code if the thumbnail exists
            ?>
              <a href="<?php echo $r->getPath(); ?>"><img src="<?php echo $ih->getThumbnail($oThumb, 120, 171)->src ?>" alt="<?php echo $r->getName(); ?>" /></a>
            <? }
         }
         ?>
BigBobbyD replied on at Permalink Reply
BigBobbyD
foreach($results as $r)


There's your $r variable.

PHP foreach takes an array and loops through the values one by one, and you can grab these variables with another name for conveniences. So all of you search results are contained in $results and "foreach" iteration of the loop, $r contains one search result.

$cobj is used in autonav, not here so that's the source of your error. Your asking for something from a variable that doesn't exist in this script.

So... TheRealSean is tryin' to tell you to replace $cobj->getThumbnail with $r->getThumbnail and you're cricket.