onjegolders Posted April 8, 2012 Share Posted April 8, 2012 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 More sharing options...
Soma Posted April 8, 2012 Share Posted April 8, 2012 CSS: .box::first-child{} .box::last-child{} in Processwire API PHP foreach($pagearray as $p){ if($p === $pagearray->first()) ....; if($p === $pagearray->last()) ....; echo ... } 1 Link to comment Share on other sites More sharing options...
onjegolders Posted April 8, 2012 Author Share Posted April 8, 2012 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. Link to comment Share on other sites More sharing options...
netcarver Posted April 8, 2012 Share Posted April 8, 2012 (edited) 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 April 8, 2012 by netcarver 1 Link to comment Share on other sites More sharing options...
Soma Posted April 8, 2012 Share Posted April 8, 2012 http://caniuse.com/#search=first If you still support IE6 it's not, but there's polyfills in case. there's common techniques in programming that goes beyond PW API or php. something like this for example? $i = 0; foroeach($pa as $p){ $i++; if(0 == $i % 2) $class = ' class="last-row"'; echo "<div$class>$p->title</div>"; } Link to comment Share on other sites More sharing options...
diogo Posted April 8, 2012 Share Posted April 8, 2012 i wonder if this works... if($p->index % 2) //class odd Edit: so many answers while i was writing this 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 More sharing options...
onjegolders Posted April 8, 2012 Author Share Posted April 8, 2012 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 More sharing options...
onjegolders Posted April 8, 2012 Author Share Posted April 8, 2012 i wonder if this works... if($p->index % 2) //class odd Edit: so many answers while i was writing this 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 More sharing options...
diogo Posted April 8, 2012 Share Posted April 8, 2012 Thanks netcarver, I don't really understand the % reference % is the modulus operator. you can see if a number is even, by checking that it's modulus is zero http://newsourcemedi...d-even-numbers/ and you can also check if a number is odd simply like I did above if( $count % 2 ) because zero is false Link to comment Share on other sites More sharing options...
netcarver Posted April 8, 2012 Share Posted April 8, 2012 Are these divs generated from child pages by any chance? If so, you can get the index of each using PW's getItemKey() method. Whilst this does work, I don't see that method on the official API Cheatsheet so I don't know if this is going to be a stable solution. YMMV. Link to comment Share on other sites More sharing options...
onjegolders Posted April 8, 2012 Author Share Posted April 8, 2012 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 More sharing options...
diogo Posted April 8, 2012 Share Posted April 8, 2012 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 1 Link to comment Share on other sites More sharing options...
onjegolders Posted April 8, 2012 Author Share Posted April 8, 2012 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 More sharing options...
netcarver Posted April 8, 2012 Share Posted April 8, 2012 You will see that unless you reset the $class to '' each time around the loop. Try something like... ... foreach ($testimonial as $item) { $i++; if(0 == $i % 2) { $class = 'odd'; } else { $class = ''; } ?> ... 1 Link to comment Share on other sites More sharing options...
diogo Posted April 8, 2012 Share Posted April 8, 2012 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 1 Link to comment Share on other sites More sharing options...
netcarver Posted April 8, 2012 Share Posted April 8, 2012 Netcarver, getItemKey() is on the cheatsheet. Make sure you have the "advanced" checkbox checked Diogo, thanks, I forgot about that. Link to comment Share on other sites More sharing options...
onjegolders Posted April 8, 2012 Author Share Posted April 8, 2012 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 More sharing options...
Soma Posted April 8, 2012 Share Posted April 8, 2012 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 More sharing options...
diogo Posted April 8, 2012 Share Posted April 8, 2012 if($item == $testimonial->first()) and if($item == $testimonial->last()) Link to comment Share on other sites More sharing options...
onjegolders Posted April 8, 2012 Author Share Posted April 8, 2012 Thanks Soma, I don't suppose it would be possible to do a quick example of a div with a class set for first, last and odd using getItemKey? That would really help me understand the whole thing together! Link to comment Share on other sites More sharing options...
Soma Posted April 8, 2012 Share Posted April 8, 2012 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 More sharing options...
onjegolders Posted April 8, 2012 Author Share Posted April 8, 2012 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 More sharing options...
onjegolders Posted April 8, 2012 Author Share Posted April 8, 2012 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 More sharing options...
diogo Posted April 8, 2012 Share Posted April 8, 2012 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 More sharing options...
onjegolders Posted April 8, 2012 Author Share Posted April 8, 2012 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now