Add Default Image to List Output

12 posts by 3 authors in: Forums > CMS Builder
Last Post: January 27, 2021   (RSS)

By hans - January 4, 2021

Ideally, you would do it by checking the length of the current array. Also, all data that is user-inputted should be html-encoded so as to prevent a JS injection.

<?php if(count($isp_listRecord['logo_image']) === 0){ ?>

<div class="vanishmobile"><img src="https://via.placeholder.com/350x150" alt="" ?> Logo Image" title="Logo Image" /></div>

<?php } else { ?>

<?php foreach ($isp_listRecord['logo_image'] as $upload): ?>

<div class="vanishmobile"><a href="<?php echo $isp_listRecord['website_link'] ?>" target="_new"><img src="<?php echo htmlencode($upload['urlPath'],ENT_QUOTES) ?>" alt="<?php echo htmlencode($isp_listRecord['title'],ENT_QUOTES) ?> Logo Image" title="<?php echo htmlencode($isp_listRecord['title'],ENT_QUOTES) ?> Logo Image" /></a></div>

<?php endforeach ?>

<?php } ?>

With regards to the other code:

<?php if (count($isp_listRecords) === 0): ?>
	<br /><br /><b><u>No records were found!</u></b><br/><br/>
<?php endif ?>
Hans Marcon
PHP Programmer (In Training)
interactivetools.com

By mark99 - January 5, 2021

Hi Hans,

If I strip that right back to just trying to output the image URL then I still get these:

E_WARNING: count(): Parameter must be an array or an object that implements Countable

E_NOTICE: Undefined index: logo_image

.. stemming from this line:

<?php if(count($isp_listRecords['logo_image']) === 0){ ?>

By hans - January 5, 2021

Oh my bad. I assumed logo_image was an array.

<?php if(gettype($isp_listRecord['logo_image']) !== "string"){ ?>

<div class="vanishmobile"><img src="https://via.placeholder.com/350x150" alt="Logo Image" title="Logo Image" /></div>

<?php } else { ?>

<div class="vanishmobile">
<a href="<?php echo htmlencode($isp_listRecord['website_link'],ENT_QUOTES); ?>" target="_new">

<img src="<?php echo htmlencode($isp_listRecord['logo_image'],ENT_QUOTES); ?>" alt="<?php echo htmlencode($isp_listRecord['title'],ENT_QUOTES); ?> Logo Image" title="<?php echo htmlencode($isp_listRecord['title'],ENT_QUOTES); ?> Logo Image" />

</a></div>

<?php } ?>

Try that instead, and get back to me.

Hans Marcon
PHP Programmer (In Training)
interactivetools.com

By mark99 - January 5, 2021 - edited: January 5, 2021

So that didn't work either. I ended up going back to the Code Generator just to get the basics working first and for testing came up with this:

    <?php foreach ($isp_listRecords as $record): ?>

      <!-- STEP 2a: Display Uploads for field 'logo_image' (Paste this anywhere inside STEP2 to display uploads) -->

        <?php foreach ($record['logo_image'] as $index => $upload): ?>

          <img src="<?php echo htmlencode($upload['urlPath']) ?>" width="200px" alt="<?php echo $record['title'] ?>" title="<?php echo $record['title'] ?>"><br>
          	  
        <?php endforeach ?>
        	
<?php if(gettype($record['logo_image']) !== "string"){ ?>
<img src="<?php echo htmlencode($upload['urlPath']) ?>" width="200px" alt="<?php echo $record['title'] ?>" title="<?php echo $record['title'] ?>"><br>
<?php } else { ?>
<div class="vanishmobile"><img src="https://via.placeholder.com/350x150" alt="Logo Image" title="Logo Image" /></div>
<?php } ?>

      <!-- STEP2a: /Display Uploads -->

    <?php endforeach ?>

I've included both the standard image code from CMSB and your selection code below (so we're out putting two images here per record, just for testing). You'll note I reversed the order of the default image from your code, as otherwise it loaded the default for everything.

Now initially this seems to work, but when I delete one of the product images to see if the placeholder image loads instead (the desired outcome) then it doesn't. Oddly the image from a different product loads in its place (usually taken from the product directly above the one that has a deleted image). Suggestions?

By hans - January 6, 2021

Perhaps we can discuss this with a screen share? Just so I won't miss any details.

Thanks.

Hans Marcon
PHP Programmer (In Training)
interactivetools.com

By mark99 - January 6, 2021

May be easier to just send you the PHP file I'm using to do the output? Email?

By hans - January 6, 2021

I don't think it would be enough for me to test whether the code works as intended or not, unfortunately.

Hans Marcon
PHP Programmer (In Training)
interactivetools.com

By mark99 - January 7, 2021

I can setup a similar test in our private beta install and let you have access to do a quick look, but need to know where to send the details?

By hans - January 7, 2021

Sure. Send the details at hans@interactivetools.com. I need FTP access, minimum.

Hans Marcon
PHP Programmer (In Training)
interactivetools.com