|
function cycle() { static $cycles = array(); $args = func_get_args(); $hash = join("",$args); if(!isset($cycles[$hash]) || $cycles[$hash] >= count($args)) $cycles[$hash] = 0; return $args[$cycles[$hash]++]; } Basically made for loops, so if you need to cycle back and forth between alternating class names, you can without having to do inline if statements.
|
|
|
|
Got a demo of where this would be used? I fail to see a use! :X I could just be being stupid.
|
|
|
|
<table> <?php foreach($records as $record): ?> <tr class="<?php echo cycle("class1","class2"); ?>"><td><?php echo $record['field']; ?></td></tr> <?php endforeach; ?> </table>
|
|
|
|
Glad you made it usable for multiple calls. Then again, you're Eric.
|
|
|
|
|
Glad you made it usable for multiple calls. Then again, you're Eric. Haven't thought of a way to do a clean reset for it though.
|
|
|
|
Extra parameter can be passed in maybe?
|
|
|
|
|
Extra parameter can be passed in maybe? Yeah, it would require two calls though. Something like: <?php foreach($categories as $cat): cycle('class1','class2',1); ?> <table> <?php foreach($items[$cat->id] as $item): ?> <tr class="<?php echo cycle('class1','class2'); ?>">...</tr> <?php endforeach; ?> </table> <?php endforeach; ?>
And I'm not sure how I feel about it. I'd like for it to be as simple as possible.
Last Edit: Aug 29, 2010 1:05:26 GMT by Eric
|
|
|
|
Use a class with a variable and do reset()?
|
|
|
|
|
Use a class with a variable and do reset()? It would work, but definitely seems like over kill.
|
|
|
|
Use a class with a variable and do reset()? It would work, but definitely seems like over kill. I'm just throwing out ideas. It's on par with the other ideas I threw out there.
|
|
|
|
|
There's no clean way to reset it. You aren't passing any information to the function for it to know when to terminate $cycles. A reset within the loop would require knowing the counter and knowing the condition it's being checked against. And passing that info each time is just as dirty as just writing a reset() function. Then again, i'm slow. I may not even understand your function correctly. It's basically performing this operation, I assume? $counter = 0; array("class1", "class2", "class3")[$counter ++ % 3] Edit: Is that proper php? o.0
Last Edit: Aug 29, 2010 4:31:09 GMT by Aaron
|
|
|
|
There's no clean way to reset it. You aren't passing any information to the function for it to know when to terminate $cycles. A reset within the loop would require knowing the counter and knowing the condition it's being checked against. And passing that info each time is just as dirty as just writing a reset() function. Then again, i'm slow. I may not even understand your function correctly. It's basically performing this operation, I assume? $counter = 0; array("class1", "class2", "class3")[$counter ++ % 3] Edit: Is that proper php? o.0 Yeah that's essentially it, though that won't work in PHP. You'd have to declare the array then access it. But yeah, this function works for single cycles, but I need to find some way to allow it to work in multiple cases.
|
|
|
|
As ugly as it is, I still think a class is the easiest reset method. Also, what do you do if you want to cycle through two values at two separate locations? I.e. in a templating engine for a class.
|
|
|
|