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.

  /* 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="//<?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 ?>
<?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 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="" 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)

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="" 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" />


<?php } ?>

Try that instead, and get back to me.

Hans Marcon
PHP Programmer (In Training)

By hans - January 6, 2021

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


Hans Marcon
PHP Programmer (In Training)

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)

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 I need FTP access, minimum.

Hans Marcon
PHP Programmer (In Training)