Adding in a CSS element on last item in list

4 posts by 3 authors in: Forums > CMS Builder Community
Last Post: August 24, 2016   (RSS)

By ciaran_mac - August 23, 2016

Hi There,

I am not sure what way this should be put in. I am looking to add in a CSS element for the last list item. Can anyone let me know how to do this? The code that I want to enter is "last" in the div's class "<div class="col-1-5">" (highlighted within ***************)

My code is:

// load records from 'club_sponsors'
  list($club_sponsorsRecords, $club_sponsorsMetaData) = getRecords(array(
    'tableName'   => 'club_sponsors',
    'limit'       => '5',
    'orderBy'     => 'RAND()',
    'loadUploads' => true,
    'allowSearch' => false,

 <?php foreach ($club_sponsorsRecords as $record): ?>
                       <?php foreach ($record['company_logo'] as $index => $upload): ?>
                                ***************<div class="col-1-5">***************

                                             <a href="<?php echo htmlencode($record['website']) ?>"><img src="<?php echo htmlencode($upload['thumbUrlPath']) ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" alt="<?php echo htmlencode($record['title']) ?>" /></a>

                       <?php endforeach ?>    
<?php endforeach ?>

                <?php if (!$club_sponsorsRecords): ?>
                  No records were found!<br/><br/>
                <?php endif ?>

By Daryl - August 23, 2016

Hi ciaran_mac,

You can use the end() function to get the last array element then add an if statement inside your foreach loop that checks if the current array's num is equal to the last array element's num. If yes, then add the class.


Daryl Maximo
PHP Programmer -

By Toledoh - August 23, 2016

Try playing around with the css selector of :last-child or :last-of-type.  something like this may work

<ul id="sample">

<?php for each... ?>

<li>Output goes here...</li>

<?php endforeach ?>


- - - -

#sample li:last-of-type { colour: red;}


Tim (