Add Default Image to List Output

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

By mark99 - January 4, 2021

I currently output a simplistic list summary using this code.

<?php
  /* STEP 1: LOAD RECORDS - Copy this PHP code block near the TOP of your page */
  require_once "/system/lib/viewer_functions.php";

  $orderBy = @$_REQUEST['orderBy'];  
  $orderByClean = "";

  if(!$orderByClean) { $orderByClean = 'ufbb_cheap_total_price+0'; }

  list($isp_listRecords, $isp_listMetaData) = getRecords(array(
    'tableName'   => 'isp_list',
    'limit'     => '5',
	'loadCreatedBy' => false,
	'where'       => " category LIKE '%Ultrafast Broadband%' ", 
	'orderBy'       => $orderByClean,
  ));

?>

<ul class="g_ulbox">
    <?php foreach ($isp_listRecords as $record): ?>
<li style="padding-bottom:3px;"><a href="//www.DOMAIN.com/Detail_Output.php?<?php echo preg_replace("/[ ]/", "-", $record['title']); ?>-<?php echo $record['num'] ?>" title="<?php echo $record['title'] ?>"><?php echo $record['title'] ?></a> &pound;<?php echo $record['ufbb_cheap_total_price'] ?><br />Speed: <?php echo $record['ufbb_cheap_download_average_speed'] ?>, <?php echo $record['ufbb_cheap_usage'] ?><br />Gift: <?php echo $record['ufbb_cheap_voucher_or_gift_credit'] ?></li>
    <?php endforeach ?>
</ul>
		
<?php if (!$isp_listRecords): ?>
	<br /><br /><b><u>No records were found!</u></b><br/><br/>
<?php endif ?>

Now what I want to be able to do is add the product image to that output, while also displaying a default image if the product doesn't yet have an image uploaded. I can already do this by inserting the following code on an individual product output page using this code:

<?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 $upload['urlPath'] ?>" alt="<?php echo $isp_listRecord['title'] ?> Logo Image" title="<?php echo $isp_listRecord['title'] ?> Logo Image" /></a></div>
<?php endforeach ?>
<?php if (!$isp_listRecord['logo_image']) : ?>
<div class="vanishmobile"><a href="<?php echo $isp_listRecord['website_link'] ?>" target="_new"><img src="//www.DOMAIN.COM/noImageProduct.jpg" alt="Default Logo Image" title="Default Logo Image" /></a></div>
<?php endif ?>

So ideally I need to get something similar working in the list output too, but I'm not sure quite how to do it?

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