Adding code to first item in a list

6 posts by 2 authors in: Forums > CMS Builder
Last Post: April 11, 2012   (RSS)

By Mohaukla - April 9, 2012 - edited: April 9, 2012

I wish to add specific code to the first item in a list.

Example:
<h2 class="current">Preventive Dentistry</h2>

After that the rest of the list can have regular H2 tags

How do I make that happen?

More accurate code to help see what I am doing:
<?php foreach ($servicesRecords as $record): ?><h2 class="current"><?php echo $record['title'] ?></h2>
<div class="pane" style="display:block">
<p><?php foreach ($record['service_image'] as $upload): ?><img src="<?php echo $upload['urlPath'] ?>" width="<?php echo $upload['width'] ?>" height="<?php echo $upload['height'] ?>" style="float:left;"><?php endforeach ?><?php echo $record['summary_content'] ?></p>
<a href="<?php echo $record['_link'] ?>" class="csbutton-color" style="float: right">Learn more</a><br /><br />
</div><?php endforeach ?>


Thanks in advance..
Michael Moyers



Owner of Just Rite Design Inc. A growing network of professionals in web design, graphic design, flash development, programming, and audio & video productions.



"Due to budget constraints, the Light at the end of the tunnel will be temporarily out!"

Re: [justritedesign] Adding code to first item in a list

By (Deleted User) - April 10, 2012

Hi Michael,

Try this:
<?php foreach ($servicesRecords as $key=>$record): ?>
<h2 <?php if ($key == 0) { echo "class='current'"; } ?>><?php echo $record['title'] ?></h2>
<div class="pane" style="display:block">
<p>
<?php foreach ($record['service_image'] as $upload): ?>
<img src="<?php echo $upload['urlPath'] ?>" width="<?php echo $upload['width'] ?>" height="<?php echo $upload['height'] ?>" style="float:left;">
<?php endforeach ?>
<?php echo $record['summary_content'] ?>
</p>
<a href="<?php echo $record['_link'] ?>" class="csbutton-color" style="float: right">Learn more</a>
<br />
<br />
</div>
<?php endforeach ?>

By assigning the variable $key to the index of the array that stores the record, you can check where in the array you are. The first item in the array is always indexed at '0', so if $key is '0', you output the required class.

Let me know if that helps,

Tom

Re: [justritedesign] Adding code to first item in a list

By (Deleted User) - April 10, 2012

Hi Michael,

You've got a bit of jQuery doing the nice accordion effect, and the class will be read happily (it's not a matter of single or double quotes), the issue is that the accordion effect is not being run after the page has finished loading.

I'm not sure which accordion function you're using, but you need to make sure it runs at $(document).ready(function ($) { ... } and not beforehand. This will force the function to wait until the rest of the page is drawn before performing any neat jQuery tricks on it.

Let me know if this helps,

Tom

Re: [Tom P] Adding code to first item in a list

By Mohaukla - April 11, 2012

OK great,
That was the ticket there although what I was overlooking also was the style of "display block" just after that was another element that needed to go away if it was not the first item in the list. There was no need to change the jQuery in this case.

So actually I was able to fix that with the same solution...
<?php if ($key == 0) { echo "style='display:block'"; } ?>

Now I would like to know how to express the key number elsewhere.

<?php foreach ($home_pageRecord['slideshow_uploads'] as $upload): ?>
<?php if ($upload['hasThumbnail']): ?>
<a href="#wows0" title="<?php echo $upload['info1'] ?>"><img src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?> alt="<?php echo $upload['info1'] ?>"/>1</a>
<?php endif ?>
<?php endforeach ?>


Here I need to use the key number to change for each one on the list. #wows0 #wows1 #wows2 etc etc ....
This works well since they are already set up that same way.
So how does that look?
My guess was :<?php echo $record['$key'];?> ... wrong :P

Thanks

Michael
Michael Moyers



Owner of Just Rite Design Inc. A growing network of professionals in web design, graphic design, flash development, programming, and audio & video productions.



"Due to budget constraints, the Light at the end of the tunnel will be temporarily out!"

Re: [justritedesign] Adding code to first item in a list

By (Deleted User) - April 11, 2012

Hi Michael,

To assign the index of an array to a variable when using foreach, all you have to do is declare it using the format 'index'=>'value', for example:
foreach ($array as $key=>$value) {
echo $key." is the key!<br>";
echo $value." is the value!<br><br>";
}

So in your second loop, that would be:
<?php foreach ($home_pageRecord['slideshow_uploads'] as $key=>$upload): ?>
<?php if ($upload['hasThumbnail']): ?>
<a href="#wows<?php echo $key; ?>" title="<?php echo $upload['info1'] ?>"><img src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?> alt="<?php echo $upload['info1'] ?>"/>1</a>
<?php endif ?>
<?php endforeach ?>


Let me know if this helps,

Tom