Please login or register. Welcome to the Studio, guest!


Quick Links:


newBookmarkLockedFalling

Eric

Eric Avatar



1,442


November 2005
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.

Michael

Michael Avatar
*Has a custom title*



1,462


October 2007
Got a demo of where this would be used? I fail to see a use! :X I could just be being stupid.


Eric

Eric Avatar



1,442


November 2005
<table>
<?php foreach($records as $record): ?>
   <tr class="<?php echo cycle("class1","class2"); ?>"><td><?php echo $record['field']; ?></td></tr>
<?php endforeach; ?>
</table>


Chris

Chris Avatar

******
Head Coder

19,519


June 2005
Glad you made it usable for multiple calls. :P Then again, you're Eric.

Eric

Eric Avatar



1,442


November 2005
Chris Avatar
Glad you made it usable for multiple calls. :P Then again, you're Eric.
Haven't thought of a way to do a clean reset for it though.

Chris

Chris Avatar

******
Head Coder

19,519


June 2005
Extra parameter can be passed in maybe?

Eric

Eric Avatar



1,442


November 2005
Chris Avatar
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

Chris

Chris Avatar

******
Head Coder

19,519


June 2005
Use a class with a variable and do reset()?

Eric

Eric Avatar



1,442


November 2005
Chris Avatar
Use a class with a variable and do reset()?
It would work, but definitely seems like over kill.

Chris

Chris Avatar

******
Head Coder

19,519


June 2005
Eric Avatar
Chris Avatar
Use a class with a variable and do reset()?
It would work, but definitely seems like over kill.


I'm just throwing out ideas. :P It's on par with the other ideas I threw out there.

Aaron

Aaron Avatar
Bad Wolf

****
Dedicated Studio Member

859


November 2006
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

Eric

Eric Avatar



1,442


November 2005
Aaron Avatar
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.

Chris

Chris Avatar

******
Head Coder

19,519


June 2005
As ugly as it is, I still think a class is the easiest reset method. :P 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.

newBookmarkLockedFalling