Jump to content
alix32

Only display the 2nd product image on product_info.php

Recommended Posts

Hello all! I'm in the process of re-building my very old 2.2 site on 2.3.4. On the product_info page, my main image is displaying nicely along with its sub-images below. What I'd like to do now is copy over the image display code further down the page in a product description section. In that section, I'd like to display 2nd image of the product only if there are more than 1 image linked to that product, else I'd like to display the main image again.

I've been scratching my head on this one but my poor knowledge of PHP has brought me to posting my question to this forum.

So here's the code I have to display my product images. The main image appears in large (width of 410) and the next images appear smaller (width of 102) below the main image

 

<?php
    if (tep_not_null($product_info['products_image'])) {
      $pi_query = tep_db_query("select image, htmlcontent from " . TABLE_PRODUCTS_IMAGES . " where products_id = '" . (int)$product_info['products_id'] . "' order by sort_order");

      if (tep_db_num_rows($pi_query) > 0) {
?>

    <div id="piGal" style="float: left;">
      <nav>
	  <ul>

<?php
        $pi_counter = 0;
        while ($pi = tep_db_fetch_array($pi_query)) {
          $pi_counter++;

       $pi_entry = '        <li ' . ($pi_counter != 1 ? 'style="float:left;" class="imgthumbHover"' : '') . '><a href="';   
        
          if (tep_not_null($pi['htmlcontent'])) {
            $pi_entry .= '#piGalimg_' . $pi_counter;
          } else {
            $pi_entry .= tep_href_link(DIR_WS_IMAGES . $pi['image'], '', 'NONSSL', false);
          }
  if ($pi_counter == 1) {$piwidth = '410'; $piheight = ''; } else { $piwidth = '102'; $piheight = '200'; }
 $pi_entry .= '"rel="zoombox[vacances]">' . tep_image(DIR_WS_IMAGES . $pi['image'], $products_name, $piwidth, null) . '</a>';      
        
          if (tep_not_null($pi['htmlcontent'])) {
            $pi_entry .= '<div style="display: none;"><div id="piGalimg_' . $pi_counter . '">' . $pi['htmlcontent'] . '</div></div>';
          }

          $pi_entry .= '</li>';

          echo $pi_entry;
        }
?>

      </ul>
	  </nav>
    </div>



<?php
      } else {
?>

    <div id="piGal" style="float: left;">
      <?php echo '<a href="' . tep_href_link(DIR_WS_IMAGES . $product_info['products_image'], '', 'NONSSL', false) . '"rel="zoombox[vacances]">' . tep_image(DIR_WS_IMAGES . $product_info['products_image'], addslashes($product_info['products_name']), SMALL_IMAGE_WIDTH, SMALL_IMAGE_HEIGHT, 'hspace="5" vspace="5"') . '</a>'; ?>
    </div>

<?php
      }
?>

I understand that code will display my main image wider and above my sub-images if the product has more than 1 image, else it just displays the only image linked to that product.

But how could I change this code so that only the 2nd image displays if the product has more than 1 image, else display the only image linked to that product?

Hope that makes sense and someone knowledgeable is willing to take a few mns to help me out,

Thanks!

Share this post


Link to post
Share on other sites

The images are all in one table so you will need to sort by product_id and some other id such as sort order, something like this,

$sql = "SELECT id, products_id, sort_order, image FROM products_images
WHERE products_id='29' AND sort_order='2'";

 


 

Share this post


Link to post
Share on other sites

Of course the sort_order! That worked perfectly thanks JcMagpie! 

<?php 
      if (tep_db_num_rows($pi_query) > 1) {
	  $pi_query = tep_db_query("select image from " . TABLE_PRODUCTS_IMAGES . " where products_id = '" . (int)$product_info['products_id'] . "' and sort_order=2");
?>

 

Edited by alix32

Share this post


Link to post
Share on other sites

When you say you're "re-building my very old 2.2 site on 2.3.4", I hope you mean the very latest osC, 2.3.4.1BS Edge/CE/Frozen. It gives you PHP 7.1 compatibility (a few fixes needed for 7.2), responsive design (mobile friendly), and a number of new features. Please don't use the official "latest" offering of 2.3.4.1, as it's years out of date.

Share this post


Link to post
Share on other sites

Uhm well Mr Phil...I hear you. Right now I'm re-building the site on 2.3.4.1 as quickly as possible. I gave the BS Edge version a shot but couldn't get my head round it. Right now I just need to get the site running on PHP 5.6 at least before my host provider turns the lights off as I'm still running on PHP 4 + it's not compatible with TLS1.2.

2.3.4.1 was the fastest option for me to be back in business. And then I'll indulge into the BS version.

Share this post


Link to post
Share on other sites

OK, Alix. At least you're aware of the situation, and plan to seriously look into the BS version. Actually, I can't even guarantee that the official 2.3.4.1 won't give you trouble with PHP 5.6 -- I don't think it was ever updated that far, so don't be surprised if something breaks. Good luck with your upgrading!

Share this post


Link to post
Share on other sites

When you say "the second image", do you mean the First Large Image?

#1 - thumbnail
#2 - first large image
#3 - second large image
etc

If this is the case (ie just show the first large image)..simply set "Thumbnail Layout" to 1.   Not more, not less... just "1".

 

Edit: 
Or don't. 
Just read rest of thread. 
You aren't using an up to date version of osCommerce, so that wont work. 
Here is code (still assuming you want the first large image):

tep_db_query("select image from products_images where products_id = '" . (int)$product_info['products_id'] . "' order by sort_order limit 1");

 

Edited by burt
old version

This is a signature that appears on all my posts.  
IF YOU MAKE A POST REQUESTING HELP...please state the exact version
of osCommerce that you are using. THANKS

 
Get the latest Responsive osCommerce CE (community edition) here

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×