Jump to content

If count, switch classes...


onjegolders
 Share

Recommended Posts

Also, echo ">" should probably be echo "'>" (note the single quote inside there.)

That's correct, i changed those later, and maybe you copied them before...

Link to comment
Share on other sites

Also, notice that on my code I use multiple classes like

class= 'testimonial_box even last'

instead of

class= 'testimonial_box_even'

This should make it easier to style all boxes, and then target these new classes for smaller changes

Link to comment
Share on other sites

Also, notice that on my code I use multiple classes like

class= 'testimonial_box even last'

instead of

class= 'testimonial_box_even'

This should make it easier to style all boxes, and then target these new classes for smaller changes

I tend to use these overly long class names in fear that I'll double reference them in some way. Old bad habits die hard!

Link to comment
Share on other sites

I saw this over on CSS Tricks. It might be of help: http://css-tricks.co...riping-a-table/

And this if you were wanting a CSS-only solution that supported older browsers: http://www.keithclar...nded-selectors/

<?php
$c = 0;
$items = $pages->get("/items/")->find("template=items");
foreach($items as $item) {
echo "<li class='";
  echo $c++&1 ? 'odd' : 'even';
echo "'>{$item->title}</li>";
}
?>

Some good examples here too: http://functino.com/2009/04/zebra-tables-how-to-alternate-table-row-colors/

Link to comment
Share on other sites

Using getItemKey isn't really necessary since you can already get that from the foreach(). I think that CSS is where all this really belongs, but if needed to support legacy browsers without using the ":" options in CSS, here's another approach...

I'm writing in the browser without testing, so forgive me if this needs adjustment. :)

foreach($page->children as $key => $item) {
   $class = 'a' . ($key+1) . ' z' . ($page->numChildren - $key) . ' ' . ($key % 2 ? 'odd' : 'even');  
   echo "<li class='$class'>{$item->title}</li>";
}

From there, you should be able to target any item directly with CSS:

li.a1 {
   /* first item */
}

li.z1 {
   /* last item */
}

li.a2 {
   /* second item */ 
}

li.z3 {
   /* 3rd from last item, etc... */
}

li.even, li.odd {
   /* self explanatory */
}

The "a" class is counting from the beginning while the "z" class is counting from the end. You could substitute any class name, but I'm just using "a" to refer to "from the beginning" and "z" to refer to "from the end" (US alphabet).

  • Like 3
Link to comment
Share on other sites

Using getItemKey isn't really necessary since you can already get that from the foreach(). I think that CSS is where all this really belongs, but if needed to support legacy browsers without using the ":" options in CSS, here's another approach...

I'm writing in the browser without testing, so forgive me if this needs adjustment. :)

foreach($page->children as $key => $item) {
$class = 'a' . ($key+1) . ' z' . ($page->numChildren - $key) . ' ' . ($key % 2 ? 'odd' : 'even');  
echo "<li class='$class'>{$item->title}</li>";
}

From there, you should be able to target any item directly with CSS:

li.a1 {
/* first item */
}

li.z1 {
/* last item */
}

li.a2 {
/* second item */
}

li.z3 {
/* 3rd from last item, etc... */
}

li.even, li.odd {
/* self explanatory */
}

The "a" class is counting from the beginning while the "z" class is counting from the end. You could substitute any class name, but I'm just using "a" to refer to "from the beginning" and "z" to refer to "from the end" (US alphabet).

Ryan, I believe you're operating on a much higher brain frequency than me! I'm in awe...

I'm quite new to PHP and I tend to try not to use code that I don't yet understand.

Needless to say that was a bit over my head!

Thanks for helping out.

Link to comment
Share on other sites

Just to give more explanation behind it, the "a" class is meant to count from the beginning, so "a1" means first item, "a2" means second item, etc., while the "z" class is meant to count from the end so "z1" means last item, "z2" means second to last, etc. It could just as easily be called "from-the-beginning-1" or "from-the-end-1", but just wanted to make sure my extra short class names weren't causing confusion.

The purpose of going this route was to keep comparisons to numbers rather than objects. We know that the first item is always going to have a key of 0, and the last item is going to have the key of $page->numChildren-1. So we don't really need to call first() or last(), though there's no harm in it either.

The only other real difference here from earlier examples is just that I'm not using the getItemKey() method. It's not necessary to do that because anytime you call foreach() in PHP you have the option of including the $key in the foreach. As a result, this:

foreach($page->children as $key => $item) {

is the same as this:

foreach($page->children as $item) {
   $key = $item->getItemKey();
  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...