detecting first record listed in any sort

5 posts by 4 authors in: Forums > CMS Builder
Last Post: October 11, 2009   (RSS)

By rez - October 9, 2009 - edited: October 9, 2009

I have a <ul> list and the first <li> looks like this:

<li class="first-item">

the rest are just <li>


So, to make this with CMS builder, i want to put an if statement in there:

<li<if this is the first record> class="first-item"<endif>>


My records are currently sorted by decending order. So the first record in the list is the highest number record. (or do the record number change when dragged in the editor?)

How do I make the if statement detect whatever the first item in my ul? I hope there is a universal way that will work no matter how i sort (but I want this to work either way and can't figure it out). I need this to work in several sorting ways, like:

decending order, ascending order, and drag sort order.

I gave up on it and deleted my code, making all my records look the same without the "first-item" class. I hope this is easy or I can give it a go again and post my attempts.

Re: [rez] detecting first record listed in any sort

By irelocation - October 9, 2009

I have the same issue, Rez. Anyone have any ideas? I am especially interested in doing it for ascending order....
home security system

Re: [rez] detecting first record listed in any sort

By Damon - October 10, 2009

Here is one way of treating the first item in your list a different way no matter how the list is sorted.

<ul>
<?php $counter = 1; ?>
<?php foreach ($inventoryRecords as $record): ?>
<?php if($counter == 1) : ?>
<li class="first-item"><?php echo $record['name'] ?></strong></li>
<?php else : ?>
<li><?php echo $record['name'] ?></li>
<?php endif; ?>
<?php $counter++ ?>
<?php endforeach; ?>
</ul>


You set a $counter to one before the foreach loop. Then in the loop, you check with an if statement if the $counter equals 1. If it does, this is the first record, and style it differently, else style it another way.

The $counter than increases after the first loop so the $counter won't equal 1 again.

I'm sure there is lots of ways to get the same results but this is the way I usually do it. :)

Hope this makes sense and helps.
Cheers,
Damon Edis - interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [rez] detecting first record listed in any sort

By Chris - October 11, 2009

Hi rez,

Here you go:

<ul>
<?php $counter = 1; ?>
<?php foreach ($inventoryRecords as $record): ?>
<?php if ($counter == 1): ?>
<li class="first-item"><?php echo $record['name'] ?></strong></li>
<?php elseif ($counter == sizeof($inventoryRecords)): ?>
<li class="last-item"><?php echo $record['name'] ?></strong></li>
<?php else: ?>
<li><?php echo $record['name'] ?></li>
<?php endif; ?>
<?php $counter++ ?>
<?php endforeach; ?>
</ul>


I hope this helps. Please let me know if you have any questions.
All the best,
Chris