0

I am trying to get back into the foreach statement, as seen in the example code below. is there a way to do that?

<?php 
    foreach($boxes as $box) 
    {
       foreach($box as $thing) 
       {
?>
        <img src="<?php echo $thing ?>"/>
<?php
       }  
    }
?>

<!-- more html code here outside of foreach statement that don't want to be loop -->

// want to go back in to the foreach statement     

<?php echo $thing; ?>

so the output will be

<img src="1">
<img src="2">
<img src="3">

<div>this only appear once</div>

<img src="1"><p>1</p>
<img src="2"><p>2</p>
<img src="3"><p>3</p>
3
  • I want to print out the same thing in the same order again. not really sure what do you mean by interpolate. Commented Dec 5, 2014 at 4:05
  • Wouldn't you just run the loops again? (Have another foreach loop in the next section you required it?) Commented Dec 5, 2014 at 4:06
  • save the foreach result to a variable and print the variable afterwards at the places you want it? Commented Dec 5, 2014 at 4:06

2 Answers 2

1

I want to print out the same thing in the same order again

By that logic, you could define a function:

function outputBoxes($boxes) {
    foreach($boxes as $box) {
        foreach($box as $thing) { // you can make the next two lines valid with ?>
            <!-- html code here -->
            <img src="<?php echo $thing ?>"/>
        <?php } // and now we're back in PHP
    }
}

Then use outputBoxes($boxes) anytime you want that to make that foreach loop happen again.

@Prix also brings a valid argument since we like to avoid frivolous looping as programmers:

function outputBoxes($boxes) {
    $out = '';
    foreach ($boxes as $box) {
        foreach ($box as $thing) {
            $out .= '<!-- html code here -->' .
                    '<img src=' . $thing . ' />';
        }
    }
    return $out;
}

Then you can echo outputBoxes($boxes); or $boxHtml = outputBoxes($boxes); and just echo $boxHtml; as much as we want. Dealer's choice!

Sign up to request clarification or add additional context in comments.

2 Comments

I like the function ideas thank you. i guess i just didn't really think it through.
@wlin Cheers! Make sure you accept the answer that you feel has best solved your question.
0

if you mean the foreach will print the html code n times, just put the curly bracket under the html code. But you have 2 foreach there so I don't know which one. I'll just put both close bracket down.

    <?php 
        foreach($boxes as $box) {
                  foreach($box as $thing) {
                        <!-- html code here -->
                        <img src='<?php echo $thing ?>'/>
    ?>

    <!-- more html code here outside of foreach statement that don't want to be loop -->

    // want to go back in to the foreach statement     

    <?php 
echo $thing;
                  }  
        }

?>

As far as I know, every html code outside

<?php ?>

tag will be treated as echo "html code" when the PHP intepreter reads PHP file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.