Jump to content

If count, switch classes...


onjegolders
 Share

Recommended Posts

Sorry to start another post :)

Was just wondering how to replicate ""if count = 1", if count = total_results as well as switch classes in PW?

I'm guessing it's some sort of standard PHP but can't seem to figure it out.

The aim is to have divs with different margins, paddings, floats dependent on which number it is, ie. first one will have no margin-top, every second div will be floated right.

Thanks

Link to comment
Share on other sites

CSS:

.box::first-child{}
.box::last-child{}

in Processwire API PHP

foreach($pagearray as $p){
if($p === $pagearray->first()) ....;
if($p === $pagearray->last()) ....;
echo ...
}
  • Like 1
Link to comment
Share on other sites

Thanks Soma, that could be one way of doing it but not sure if that CSS is universal?

Any way of getting values other than first/last?

I'd like to have every other box float right, as an example.

Hi onjegolders. You could give CSS' nth-child a try, depending on what compatibility you need. Failing that, on the browser side I think jQuery has a similar selector that is universal, but it will fail for your visitors who don't have js enabled. If you need a solution in PHP you can keep an iteration counter and use the modulus operator to check for nth-ness...

if( 0 == $count % 2 ) {
// align one way
}
else {
// align the other
}

Edited to add: If you need to check for some other multiple, like every 5th iteration around a loop, just change the 2 in the example to 5.

Edited by netcarver
  • Like 1
Link to comment
Share on other sites

i wonder if this works...

if($p->index % 2) //class odd

Edit: so many answers while i was writing this :D

Edit: It doesn't work of course, the code above is trying to find the field named index, and not the index of the page inside an array.

Link to comment
Share on other sites

Wow thanks all of you for helping out, I was ideally looking for a simple-ish PW way of doing this, I was getting used to things being quite simple and intuitive!

Ideally I'd just like to count which number of the loop I was on and add classes to it.

It was relatively simple in EE and I figured there'd be something a little similar here.

Sample code in EE:

<div class="box{if count == 1} first_box{/if}"</div>
<div class="box {switch="even|odd"}"</div>
<div class="box{if count == total_results} last_box{/if}"</div>

In PW? I'm a bit stuck!

Link to comment
Share on other sites

i wonder if this works...

if($p->index % 2) //class odd

Edit: so many answers while i was writing this :D

Edit: It doesn't work of course, the code above is trying to find the field named index, and not the index of the page inside an array.

Thanks anyway Diogo!

Hi onjegolders. You could give CSS' nth-child a try, depending on what compatibility you need. Failing that, on the browser side I think jQuery has a similar selector that is universal, but it will fail for your visitors who don't have js enabled. If you need a solution in PHP you can keep an iteration counter and use the modulus operator to check for nth-ness...

if( 0 == $count % 2 ) {
// align one way
}
else {
// align the other
}

Edited to add: If you need to check for some other multiple, like every 5th iteration around a loop, just change the 2 in the example to 5.

Thanks netcarver, I don't really understand the % reference

Link to comment
Share on other sites

Thanks Diogo,

I was hoping there was a more API way of doing this.

I've seen Ryan say many times that most differences between EE and PW boil down to template language versus native PHP.

Is there no "in-house" way of referencing "count" within a loop?

Eg:

if ($page->children->count(1)) {

echo "class=\"first_box\"";

} ?>

Link to comment
Share on other sites

In native PHP you can do as Soma said before.

$i = 0;
foroeach($pa as $p){
$i++;
if(0 == $i % 2) $class = ' class="last-row"';
echo "<div$class>$p->title</div>";
}

But I agree that it would be nice to have this built in the API. Let's wait that Ryan comes back to see what he will say about this :)

  • Like 1
Link to comment
Share on other sites

Thanks Diogo,

I tried using the examples of Soma and Netcarver, so far I have the following:

<?php include("./header.inc"); ?>

<?php
$testimonial = $page->children();
$i = 0;
$i++;
$class = "";

foreach ($testimonial as $item) {  
$i++;
if(0 == $i % 2) { $class = 'odd'; } ?>
<div class="testimonial_box<?php echo " $class"; ?>">
<?php echo $item->body; ?>
<p><?php echo $item->title; ?></p>
</div><!-- /.testimonial_box -->

<?php } ?>

<?php include("./footer.inc"); ?>

When I look at page source, I see that all the boxes end up with the "odd" class.

Link to comment
Share on other sites

I tested the suggestion of Netcarver and it works.

On your code, it would look like this:

if( 0 == $testimonial->getItemKey($item) % 2)

@Netcarver, getItemKey() is on the cheatsheet. Make sure you have the "advanced" checkbox checked

  • Like 1
Link to comment
Share on other sites

Woh, you guys are quick, just about to put that your code worked, so thanks!

Don't suppose there is any way similar code could be used to get 1st, last etc? Or would that be

->first();

?

Is there any difference between using getItemKey or not?

Thanks again both of you.

Link to comment
Share on other sites

onjegolders, look at my first example. ->first(), ->last() can also be used. But that limits it to first and last. With $pagearry->getItemKey($page) you can get the position index 0,1,2,3... So you can use $count = $array->getItemKey($page) + 1; to get the count for each loop.

Link to comment
Share on other sites

like this:

<?php
$testimonial = $page->children();

foreach ($tm as $item) {
$class = '';
$count = $tm->getItemKey($item) + 1;
if(!0 == $count % 2) $class = 'odd';
if($tm->first() === $item) $class .= ' first-item';
if($tm->last() === $item) $class .= ' last-item';
?>
<div class="testimonial_box<?php echo " $class"; ?>">
	<?php echo $item->body; ?>
	<p><?php echo $item->title; ?></p>
</div>

<?php } ?>
Link to comment
Share on other sites

Tacking on Diogo's suggestion, I think I have it all working, code's probably a bit messy compared with you guys but get's the job done!

<?php 
$testimonial = $page->children();
$i = 0;
   $i++;

foreach ($testimonial as $item) {  
$i++; 
if(!0 == $i % 2) { $class = 'testimonial_box_odd'; } else { $class = ''; } ?>
<div class="testimonial_box<?php echo " $class"; ?><?php if ($item == $testimonial->first()) { echo " first"; } ?><?php if ($item == $testimonial->last()) { echo " last"; } ?>">
<?php echo $item->body; ?>
<p><?php echo $item->title; ?></p>
</div><!-- /.testimonial_box -->

<?php } ?>
Link to comment
Share on other sites

like this:

<?php
$testimonial = $page->children();

foreach ($tm as $item) {
$class = '';
$count = $tm->getItemKey($item) + 1;
if(!0 == $count % 2) $class = 'odd';
if($tm->first() === $item) $class .= ' first-item';
if($tm->last() === $item) $class .= ' last-item';
?>
<div class="testimonial_box<?php echo " $class"; ?>">
	<?php echo $item->body; ?>
	<p><?php echo $item->title; ?></p>
</div>

<?php } ?>

Thanks Soma, I just changed $testimonial to $tm as per rest of your code and that works great.

The +1 is just making it odd right?

Link to comment
Share on other sites

I would do it maybe like this:

foreach ($testimonial as $item) {
  echo "<div class='testimonial_box";
  if($testimonial->getItemKey($item) % 2) { echo " even"; } else { echo " odd"; };
  if($item == $testimonial->first()) { echo " first"; };
  if($item == $testimonial->last()) { echo " last"; };
  echo "'>";
  echo $item->body;
  echo "<p>$item->title</p>;
  echo "</div><!-- /.testimonial_box -->";
}

edit: oh, late again...

corrected some quote marks

Link to comment
Share on other sites

Thanks Diogo, using your method adapted to my code, I'm getting "Parse Error syntax error, unexpected $end"

I've obviously left something out but can't see it!

<?php include("./header.inc"); ?>

<?php

$testimonial = $page->children();

foreach ($testimonial as $item) {
   echo "<div class='testimonial_box";
   if($testimonial->getItemKey($item) % 2) { 
   echo " testimonial_box_even"; } else { echo " testimonial_box_odd"; };
   if($item == $testimonial->first()) { 
   echo " testimonial_box_first"; };
   if($item == $testimonial->last()) { 
   echo " testimonial_box_last"; };
  echo ">";
  echo $item->body;
  echo "<p>$item->title</p>";
  echo "</div><!-- /.testimonial_box -->";

?>

<div class="clear"></div><!-- /.clear -->

<?php include("./footer.inc"); ?>
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...