clueless
clueless

Reputation: 869

A more efficient algorithm in PHP

I have 12 image links in the db which I can call with get_field('opt3_img_N'), where N can be any number from 1 -12. (I'm using wordpress for anyone familiar).

What I'm having trouble with is coming up with an efficient way to output the images the way I want.

I need the links to be distributed into 3 divs equally. Ideally, I wanted image 1 to be in in div 1, image 2 in div 2, image 3 in div 3, image 4 in div 1, etc.

But, I cannot for the life of me, figure it out. I know this has probably been done in lots of ways, but I couldn't find anything on the net.

What I have right now is:

<div class="div-1">
<?php for( $i = 1; $i <= 4; $i++ ) { 
    $img = get_field( "opt3_img_$i" ) ?: get_template_directory_uri() . "/images/default.jpg"; 
    $url = get_field( "opt3_url_$i" ) ?: "#"; ?>
     <a href="<?php echo $url ?>"><img src="<?php echo $img ?>" /></a>
<?php endfor ?>
</div>

and so on with divs 2 and 3. Essentially it's sort of the brute force way of doing it. This does it for now, but I would like to know if there's a better way to do this.

PS. I'm not sure if this is a good question to ask here, so please vote to be moved if needed.

Thank you.

Upvotes: 0

Views: 84

Answers (3)

vogomatix
vogomatix

Reputation: 5041

There are a number of ways of building the HTML. Thisn one builds the DIV blocks before printing them...

$myDivs = array();
$divNo = 4;  // set to the number of div blocks you want to split between
$imgCount = 12; // set to number of images.

for( $i = 1; $i <= $imgCount; $i++ ) { 
    $img = get_field( "opt3_img_$i" ) ?: get_template_directory_uri() . "/images/default.jpg"; 
    $url = get_field( "opt3_url_$i" ) ?: "#";
    // use modulo operator to decide which div to print in....
    $idx = ($i - 1) % ($divNo-1);
    // I like heredocs...
    $myDivs[ $idx][] = <<<EOF
<a href="$url"><img src="{$img}" /></a>
EOF;
}
// print the divs one after the other....
$j = 1;
foreach ($myDivs as $div) {
    $text = join("\n", $div);
    echo "<div class='div-{$j}'>{$text}</div>\n";
    $j++;
}

Upvotes: 1

Mike
Mike

Reputation: 1231

You can do it once :

<?php
    $dir=get_template_directory_uri(); // Get template dir
    for($i=1;$i < 4;++$i){
     echo '<div class="div-'.$i.'">';
     $img = get_field( "opt3_img_$i" ) ? : $dir . "/images/default.jpg";
     $url = get_field( "opt3_url_$i" ) ? : "#";
     echo '<a href="'.$url.'"><img src="'.$img.'" /></a>';
     echo'</div>';
    }
?>

Happy coding

Upvotes: 0

ddoor
ddoor

Reputation: 5963

There are lots of ways to do this, I am not sure what you mean by brute force, but that isn't brute force, it is just a loop.

You are on the right track with a loop here, what you need to start thinking about is a cursor.

//PSEUDO CODE
$div_pointer = 1;
for($i = 0; $i < 12; $i++) {
    <div class=$div_pointer>
    $div_pointer++;
    if($div_pointer > 3) $div_pointer = 1;
}

That is basically a pseudo example of using a variable to keep track of which div you want to write the image to. The $i variable is iterating over the main data set, while the cursor is keeping track of which of the 3 divs to write to. Once it gets to the forth fiv it resets back. Might help as a fresh way of looking at the problem.

That being said there are much better ways to get images into divs :P

Upvotes: 0

Related Questions