You are here:  » Using Images from one feed

Support Forum



Using Images from one feed

Submitted by paddyman on Tue, 2007-11-20 23:31 in

Hi David,

Having a problem with the quality of images, some merchants include in there feeds. One of my feeds has very good images, is there anyway to include these and if the merchant doesn't have a product then the image will come from the cheapest product. Remember something similar being done for descriptions.

Thanks

Adrian

Submitted by support on Wed, 2007-11-21 10:22

Hi Adrian,

This is straight forward for the product pages, but search results (where it may not matter so much as the images are smaller) is not so easy because it is down to the database which image_url field it selects (not necessarily the cheapest).

For the products page, you will see the following code in html/product.php

        <?php if ($mainProduct["image_url"]): ?>
          <img width='180' src='<?php print $mainProduct["image_url"]; ?>' alt='<?php print $mainProduct["name"]; ?>' />
        <?php endif; ?>

Try inserting the the following code immediately ABOVE this:

<?php
  $imgMerchant 
"Merchant Name"// set this to the name of the merchant with good images
  
foreach($product["products"] as $imgProduct)
  {
    if (
$imgProduct["merchant"] == $imgMerchant)
    {
      
// we have found the merchant with good images, so overwrite image_url in the main record
      
$mainProduct["image_url"] = $imgProduct["image_url"];
      break;
    }
    
// if the merchant with good images wasn't found the original, cheapest image_url is used
  
}
?>

Hope this helps!
Cheers,
David.

Submitted by paddyman on Wed, 2007-11-21 18:37

Thanks David,

That works great. Much appreciated.

Adrian

Submitted by dculpepper on Mon, 2008-01-14 21:28

Would like to get this working but I get the following error:

Parse error: syntax error, unexpected T_FOREACH in /path/to/my/site/product.php on line 8

Any ideas?

Thanks,

David Culpepper

Submitted by support on Mon, 2008-01-14 21:31

Hi David,

Could you email me your modified html/product.php and i'll check it over for you. Reply to your registration code or forum reg email is the easiest way to get me...

Cheers,
David.

Submitted by dculpepper on Wed, 2008-01-16 18:09

Hello David,

Thanks! The updated file you sent worked like a champ.

Regards,

David

Submitted by mally on Thu, 2008-03-27 21:59

Hello David

I've just tried adding the code above and got the same error as mentioned above.

Do you recall the changes you made in the file above?

thanks

MAlly

Submitted by support on Fri, 2008-03-28 08:42

Hi Mally,

Sorry about that - the problem was just a missing ; after $imgMerchant = "Merchant Name" - I've fixed this in the code above now...

Cheers,
David.

Submitted by mally on Sat, 2008-03-29 20:35

Hi Daivid

I tried again and got the following error

Warning: Invalid argument supplied for foreach() in /home/**removed**/public_html/html/product.php on line 86

when checking my product file line 86 its says

  foreach($product["products"] as $imgProduct)

Not sure whats up here.

Mally

Submitted by mally on Sat, 2008-05-17 10:33

Hi

I got this working for my products, just wondering if theres any way to get it to work with the search results and other things like featured and related.

Thanks

Mally

Submitted by support on Sat, 2008-05-17 10:42

Hi Mally,

It's a little more complex for search results and featured products (they work in the same way) because they rely on summary queries where you cannot determine which row MySQL is going to select the non-summary fields (i.e. image_url) from.

However, since you are running a very niche site with a relatively small database (and on a server that has good performance); one way to do it might be to pick the better image URL for each result during the loop that already exists to do post search-query alternations. In search.php, look for the start of this loop as follows:

    foreach($searchresults["products"] as $k => $product)
    {

...and try inserting the following immediately afterwards. This is straight off the top of my head as I don't have the same database, but it should be close.... :o

    $imgMerchant = "Merchant Name"; // set this to the merchant name with good images
    if ($product["merchant"] <> $imgMerchant)
    {
      $sql = "SELECT image_url FROM `".$config_databaseTablePrefix."products` WHERE name='".database_safe($product["name"])."' AND merchant='".database_safe($imgMerchant)."'";
      if (database_querySelect($sql,$rows))
      {
        $searchresults["products"][$k]["image_url"] = $rows[0]["image_url"];
      }
    }

Fingers crossed...!

Cheers,
David.

Submitted by mally on Sat, 2008-05-17 11:45

Hi David

That works well for search results. so much so I've changed the size of the images to reflect the best merchant.

Is it possible to use something similar for the featured?

cheers

Mally

Submitted by support on Sat, 2008-05-17 15:34

Hi Mally,

Sure - for each set of featured products (index.php) you will find a loop where the items are processed just as in search.php. Look for the following code:

    foreach($featured["products"] as $k => $product)
    {

And almost exactly as before, insert the following immediately afterwards:

    $imgMerchant = "Merchant Name"; // set this to the merchant name with good images
    if ($product["merchant"] <> $imgMerchant)
    {
      $sql = "SELECT image_url FROM `".$config_databaseTablePrefix."products` WHERE name='".database_safe($product["name"])."' AND merchant='".database_safe($imgMerchant)."'";
      if (database_querySelect($sql,$rows))
      {
        $featured["products"][$k]["image_url"] = $rows[0]["image_url"];
      }
    }

(the only change $featured["products"] instead of $searchresults["products"])

Cheers,
David.

Submitted by mally on Sat, 2008-05-17 16:17

Hello David

Here's my featured code, its a bit different to above..

<h2 class="featured"><?php print translate("Featured Magazine Subscriptions")?></h2>
<div id="featuredwrapper">
      <?php foreach($featured["products"] as $product):
if (is_int(($i/4))) {print "<div class='clear'></div>"; } ?>
        <div class="featuredproduct">
          <?php if ($product["image_url"]): ?>
            <p><a href='<?php print $product["productHREF"]; ?>' title='<?php print $product["name"]; ?>'><img class='bdr' width='100' height='140' src='<?php print $product["image_url"]; ?>' alt='<?php print $product["name"]; ?>' onerror="this.src='/images/defaultimg.jpg';" title='<?php print $product["name"]; ?>' /></a></p>
          <?php endif; ?>
          <h3><a href='<?php print $product["productHREF"]; ?>' title='<?php print $product["name"]; ?>'><?php print $product["name"]; ?></a></h3>
          <?php if ($config_useInteraction): ?>
            <small>
            <?php if ($product["reviews"]): ?>
              <?php print tapestry_stars($product["rating"],"s"); ?>&nbsp;
              <a href='<?php print $product["reviewHREF"]; ?>' title='<?php print $product["reviews"]." ".translate("Reviews"); ?>'><?php print $product["reviews"]." ".translate("Reviews"); ?></a>
            <?php else: ?>
              <a href='<?php print $product["reviewHREF"]; ?>' title='<?php print translate("Review This Product"); ?>'><?php print translate("Review This Magazine"); ?></a>
            <?php endif; ?>
            </small>
          <?php endif; ?>
          <p>
          <?php if ($product["numMerchants"] > 1): ?>
            <em><?php print translate("from"); ?></em>&nbsp;<strong><?php print $config_currencyHTML.$product["minPrice"]; ?></strong><br />
            <span class='nobr'><a href='<?php print $product["productHREF"]; ?>'><img src='http://www.magazinesubscription.co.uk/images/Compare.png' alt='Compare Prices for <?php print $product["name"]; ?>' title='Compare Prices for <?php print $product["name"]; ?>'/></a></span>
          <?php else: ?>
            <strong><?php print $config_currencyHTML.$product["price"]; ?></strong><br />
            <span class='nobr'><a href='<?php print $product["productHREF"]; ?>'><img src='http://www.magazinesubscription.co.uk/images/Compare.png' alt='Compare Prices for <?php print $product["name"]; ?>' title='Compare Prices for <?php print $product["name"]; ?>'/></a></span>
          <?php endif; ?>
          </p>
        </div>
      <?php $i++; endforeach; ?>
</div>
<div class="clear"></div>

Cheers

Mally

Submitted by mally on Sat, 2008-05-17 17:00

sorry, just realised you said to look in the index!

Submitted by mally on Sat, 2008-06-07 07:13

Hello David

I've added the above code successfully to my site apart from the Related product area's.

I've checked the file related.php and has

<h2 class="banner"><?php print translate("Related Products"); ?></h2>
<?php
  require("html/searchresults.php");
?>

my searchresults.php is below

<?php
  if (file_exists("html/user_searchresults_before.php")) require("html/user_searchresults_before.php");
?>
<?php print $title?>
<div class='searchresults'>
  <table width='100%'>
    <?php foreach($searchresults["products"] as $product): ?>
      <tr>
        <td>
          <?php if ($product["image_url"]): ?>
          <a href='<?php print $product["productHREF"]; ?>'><img border='0' width='150' src='<?php print $product["image_url"]; ?>' alt='<?php print $product["name"]; ?>' onerror="this.src='/images/defaultimg.jpg';"/></a>
          <?php endif; ?>
        </td>
        <td>
          <h4><a href='<?php print $product["productHREF"]; ?>' title='<?php print $product["name"]; ?>'><?php print $product["name"]; ?></a></h4>
          <p><?php print substr($product["description"],0,250); ?>...</p>
        </td>
        <td>
          <?php if ($product["minPrice"] == "0.00"): ?>
            <a href='<?php print $product["productHREF"]; ?>'><img src='http://www.magazinesubscription.co.uk/images/Compare.png' alt='Compare Prices for <?php print $product["name"]; ?>' title='Compare Prices for <?php print $product["name"]; ?>'/></a>
          <?php elseif ($product["numMerchants"] > 1): ?>
            <p align="center"><strong>Price from <?php print $config_currencyHTML.$product["minPrice"]; ?></strong></p>
           <a href='<?php print $product["productHREF"]; ?>'><img src='http://www.magazinesubscription.co.uk/images/Compare.png' alt='Compare Prices for <?php print $product["name"]; ?>' title='Compare Prices for <?php print $product["name"]; ?>'/></a>
          <?php else: ?>
            <p align="center"><strong><?php if ($product["price"] <> 9999.00)
{
  print "Price ".$config_currencyHTML.$product["price"];
}
else
      {
      print "Not Stocked";
      }
 ?></strong></p>
            <a href='<?php print $product["productHREF"]; ?>'><img src='http://www.magazinesubscription.co.uk/images/MoreInfo.png' alt='More about <?php print $product["name"]; ?>' title='More about <?php print $product["name"]; ?>'/></a>
          <?php endif; ?>
          <br />
          <a href='<?php print tapestry_buyURL($product); ?>'><img src='http://www.magazinesubscription.co.uk/images/Subscribe.png' alt='Subscribe to <?php print $product["name"]; ?>' title='Subscribe to <?php print $product["name"]; ?>'/></a>
<?php if ($config_useInteraction): ?><br /><?php if ($product["rating"]) print tapestry_stars($product["rating"],""); ?>
<?php endif; ?>
        </td>
      </tr>
    <?php endforeach; ?>
  </table>
</div>
<br />
<?php
  if (file_exists("html/user_searchresults_after.php")) require("html/user_searchresults_after.php");
?>

Can you come up with how to add it to this section please.

Cheers

MAlly

Submitted by support on Sat, 2008-06-07 07:35

Hi Mally,

The related products are picked in products.php, (which then calls html/related.php which calls searchresults.php!). The code is almost identical, except for a different variable name. This time, look for:

  foreach($searchresults["products"] as $k => $related)
  {

..and the code to insert after this point:

    $imgMerchant = "Merchant Name"; // set this to the merchant name with good images
    if ($related["merchant"] <> $imgMerchant)
    {
      $sql = "SELECT image_url FROM `".$config_databaseTablePrefix."products` WHERE name='".database_safe($related["name"])."' AND merchant='".database_safe($imgMerchant)."'";
      if (database_querySelect($sql,$rows))
      {
        $searchresults["products"][$k]["image_url"] = $rows[0]["image_url"];
      }
    }

That should do the trick!

Cheers,
David.

Submitted by mally on Sat, 2008-06-07 11:50

spot on, does just that!

Thanks David

Cheers

Mally