Anaconda Posted February 26, 2013 Share Posted February 26, 2013 I am trying to loop though an array, each element is an image tag. For each element I want to wrap it in a div tag that will either have a class of small, or large. - this will change the width of the image. It needs to show on the site as follows: Large image, Small image Clear Small Image, Large image Clear Large image, Small image etc... I can do this easily with the following code, however I wanted to know if there is a more efficient way of doing this using less code? Here is my code: $StringBuilder = ""; $Class = ""; $Counter = 1; $DivCount = 0; $isSmall = FALSE; foreach ($Data as $key => $val) { if ( $Counter == 2 ) { $isSmall = ! $isSmall; $Counter = 0; } $Class = ( $isSmall ) ? "small" : "large"; $StringBuilder .= ( $DivCounter == 2 ) ? "<div class='ClearBoth'></div>" : ""; $StringBuilder .= "<div class='$Class'>$val</div>"; $Counter ++; $DivCounter ++; } Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/274960-code-efficiency-repeating-data-question/ Share on other sites More sharing options...
AyKay47 Posted February 26, 2013 Share Posted February 26, 2013 Is the array a fixed or variable length? With a variable length array the above code will not work. Quote Link to comment https://forums.phpfreaks.com/topic/274960-code-efficiency-repeating-data-question/#findComment-1415094 Share on other sites More sharing options...
Solution Barand Posted February 26, 2013 Solution Share Posted February 26, 2013 (edited) I'd make use of the array key (assuming it runs from 0..N) and test for odd/even foreach ($Data as $key => $val) { if ($key%2==0) { if ($key) { $StringBuilder .= "<div class='ClearBoth'></div>\n"; $isSmall = !$isSmall; } $class = $isSmall ? 'small':'large'; } else $class = $isSmall ? 'large' : 'small'; $StringBuilder .= "<div class='$class'>$val</div>\n"; } Edited February 26, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/274960-code-efficiency-repeating-data-question/#findComment-1415098 Share on other sites More sharing options...
Anaconda Posted February 27, 2013 Author Share Posted February 27, 2013 AyKay47, I don't understand. Why would my original code not work if the array is variable length? I have the code in place and it does 'appear' to be working, and reading though my code it should work fine with a variable length array, or am I missing something? Thanks Barand, that looks like a good alternative, I will try and run a test to see if there is any difference in speed. I originally tried testing for odd/even with %2==0 but it alternated every loop. I wasn't aware you could do an if statement using an integer with 0==false and >0==true Quote Link to comment https://forums.phpfreaks.com/topic/274960-code-efficiency-repeating-data-question/#findComment-1415336 Share on other sites More sharing options...
Christian F. Posted February 27, 2013 Share Posted February 27, 2013 (edited) Unless you have a really huge array, the performance of such a simple loops are mostly an academical question. While the difference in percent might be huge, the actual time it takes to run through one such pageload is incredibly tiny. It would take literally millions of page loads, for it to even start to make a difference on the measured server load. Even then, we're probably talking about ± a few hundreds of a second, in total. As for the last part that is actually a part of the type auto-cast rules in PHP, quite a lot of nifty things you can do with (or subtle bugs caused by) it. Edited February 27, 2013 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/274960-code-efficiency-repeating-data-question/#findComment-1415337 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.