We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 18830
    • 161 Posts
    I have a set of TVs named like this:

    image1 … image20

    and

    caption1 … caption 20

    What I need is a snippet, that runs trough all the TVs and shows for every TV that is not empty an image element:

    <img src="path to image1" title="caption1" alt="" />


    If someone could help me this would be very nice.

    Thanks-
      • 4310
      • 2,310 Posts
      <?php
      $output = '';
      $myArray = $modx->getTemplateVarOutput(array('firstImage','secondImage','thirdImage'), $modx->documentIdentifier);
       if(!empty($myArray['firstImage'])){
       $output .='<img src="'.$myArray['firstImage'].'" width="380" height="300" />';
       }
       if(!empty($myArray['secondImage'])){
       $output .='<img src="'.$myArray['secondImage'].'" width="380" height="300" />';
       }
       if(!empty($myArray['thirdImage'])){
       $output .='<img src="'.$myArray['thirdImage'].'" width="380" height="300" />';
       }
       return $output;
      ?>

      I really ought to clean this up to auto loop through each TV wink
        • 18830
        • 161 Posts
        Thank you, bunk58, and excuse me for being late with my answer - GoogleMail marked MODx motify mail as spam sad

        In the meantime I found a solution. You just hove to name the TVs according zo this scheme: image1, image2 and so on. In the example the last image TV has the number 20.



        <?php
        for ($x = 1; $x < 21; $x++) {
           $foto = $modx->getTemplateVarOutput(array('NAME OF THE IMAGE TVs'.$x));
           if (!empty($foto['NAME OF THE IMAGE TVs'.$x]) && file_exists(MODX_BASE_PATH.$foto['NAME OF THE IMAGE TVs'.$x])) {
               echo '<img src="[*bild'.$x.'*]" title="[*NAME OF THE IMAGE TVs'.$x.'*]" width="CHANGE THIS" height="CHANGE THAT" />';
           }
        }
        ?>