Jump to content

Code Efficiency - Repeating Data Question


Anaconda
Go to solution Solved by Barand,

Recommended Posts

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.

Link to comment
Share on other sites

  • Solution

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 by Barand
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 by Christian F.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.