We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 45118
    • 123 Posts
    I'm still working on this snippet to display product information on a products page. To display product images, I'm adding code to create the link to the image:
    <?php
    $path = MODX_CORE_PATH . 'components/aeiproducts/';
    $result = $modx->addPackage('aeiproducts',$path .
        'model/','aei_');
    
    if (! $result) {
        return 'failed to add package';
    }
    
    $products = $modx->getCollection('Product');
    foreach($products as $product) {
        $imgName = $product->get('itemno'); // Grab the image name
        $imgLink = 'assets/images/prods/' . $imgName . '.jpg'; // Create the full link
        $fields = $product->toArray();
        $output .= $modx->getChunk('ShowProds', array_merge($fields, array('imgLink'=>$imgLink, 'imgName'=>$imgName)));
    }
    
    return $output;
    
    ?>

    This works just fine as long as there is an image available. How can I display a generic "no image available" jpeg whenever there's no image available for a certain product?

    Thank you for all your help!

    This question has been answered by breezer. See the first response.

      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Something like this should work...
      $imgName = isset($imgName) ? $imgName : $defaultImgName;
      $imgLink = 'assets/images/prods/' . $imgName . '.jpg'; // Create the full link
      
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 45118
        • 123 Posts
        Quote from: sottwell at Sep 20, 2013, 01:06 PM
        Something like this should work...
        $imgName = isset($imgName) ? $imgName : $defaultImgName;
        $imgLink = 'assets/images/prods/' . $imgName . '.jpg'; // Create the full link
        
        This doesn't work because even if there's no image of a particular itemno, the link still gets created. I might need a way to check in the images folder first, to see if the image is there. I just don't know how.
          • 3749
          • 24,544 Posts
          You could just get a different chunk if there's no image:

          $imgName = $product->get('itemno'); // Grab the image name
          if (empty($imgName)) {
              $output .= $modx->getChunk('ShowProdsNoImage');
          } else {
              $output .= $modx->getChunk('ShowProds', array_merge($fields, array('imgLink'=>$imgLink, 'imgName'=>$imgName)));
          }
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
          • discuss.answer
            • 4041
            • 788 Posts
            Maybe something like this:
            // set up default image at top of script
            $default_image ='assets/images/prods/default_image.jpg';
            


            then modify the foreach loop
            $imgLink = 'assets/images/prods/' . $imgName . '.jpg'; // Create the full link
            // if the image does not exist, use default
            if( !file_exists( $imgLink ) ){
                $imgLink = $default_image;
            }
            
              xforum
              http://frsbuilders.net (under construction) forum for evolution
              • 45118
              • 123 Posts
              Quote from: breezer at Sep 20, 2013, 10:26 PM
              Maybe something like this:
              // set up default image at top of script
              $default_image ='assets/images/prods/default_image.jpg';
              


              then modify the foreach loop
              $imgLink = 'assets/images/prods/' . $imgName . '.jpg'; // Create the full link
              // if the image does not exist, use default
              if( !file_exists( $imgLink ) ){
                  $imgLink = $default_image;
              }
              
              Thank you!!
              This works beautifully because it checks if the image file that the link ($imgLink) is pointing to really exists. Checking if $imgName is empty or not doesn't work; it always exists because it comes from the database 'itemno'.