PHP - How to use a div to wrap all items Inside a foreach loop












-2















I have been scratching my head for half hour now trying to figure this out

I have a wrapper div inside a foreach loop and I'm trying to force it to echo one and not duplicate



    foreach ($apples as $apple) {
//echo only once bellow
<div class="Wrapper">
//echo only once Above

echo $apple;

//echo only once bellow
</div>
//echo only once Above
}


I do not wish to move my Wrapper div outside the foreach, It is very important for the div to be inside the foreach and to be without duplicates.










share|improve this question




















  • 1





    what is the reason to not put this div to outside of loop ?

    – C2486
    Nov 23 '18 at 18:08











  • That makes no sense. A wrapper is precisely the sort of thing you would do outside the loop, for the very fact it needs to be output once, not iteratively. If you really must do it inside, you need to set up a tracker variable which is set once it's output, then output the wrapper tags only if that variable has not yet been set.

    – Utkanos
    Nov 23 '18 at 18:10











  • use condition {for index} to print starting of div at element 1 and for last element close div?

    – Wasim Sayyed
    Nov 23 '18 at 18:10








  • 1





    Can you not post pseudo code please? What you have now isn't proper syntax. You're injecting pure HTML into PHP.

    – Funk Forty Niner
    Nov 23 '18 at 18:15













  • When you say you don't want to move the wrapper outside the loop, you're basically saying you don't want to do this the really obvious and straightforward way. Unless we know why you don't want to do that, we can't give you any decent answers.

    – iainn
    Nov 23 '18 at 18:24
















-2















I have been scratching my head for half hour now trying to figure this out

I have a wrapper div inside a foreach loop and I'm trying to force it to echo one and not duplicate



    foreach ($apples as $apple) {
//echo only once bellow
<div class="Wrapper">
//echo only once Above

echo $apple;

//echo only once bellow
</div>
//echo only once Above
}


I do not wish to move my Wrapper div outside the foreach, It is very important for the div to be inside the foreach and to be without duplicates.










share|improve this question




















  • 1





    what is the reason to not put this div to outside of loop ?

    – C2486
    Nov 23 '18 at 18:08











  • That makes no sense. A wrapper is precisely the sort of thing you would do outside the loop, for the very fact it needs to be output once, not iteratively. If you really must do it inside, you need to set up a tracker variable which is set once it's output, then output the wrapper tags only if that variable has not yet been set.

    – Utkanos
    Nov 23 '18 at 18:10











  • use condition {for index} to print starting of div at element 1 and for last element close div?

    – Wasim Sayyed
    Nov 23 '18 at 18:10








  • 1





    Can you not post pseudo code please? What you have now isn't proper syntax. You're injecting pure HTML into PHP.

    – Funk Forty Niner
    Nov 23 '18 at 18:15













  • When you say you don't want to move the wrapper outside the loop, you're basically saying you don't want to do this the really obvious and straightforward way. Unless we know why you don't want to do that, we can't give you any decent answers.

    – iainn
    Nov 23 '18 at 18:24














-2












-2








-2








I have been scratching my head for half hour now trying to figure this out

I have a wrapper div inside a foreach loop and I'm trying to force it to echo one and not duplicate



    foreach ($apples as $apple) {
//echo only once bellow
<div class="Wrapper">
//echo only once Above

echo $apple;

//echo only once bellow
</div>
//echo only once Above
}


I do not wish to move my Wrapper div outside the foreach, It is very important for the div to be inside the foreach and to be without duplicates.










share|improve this question
















I have been scratching my head for half hour now trying to figure this out

I have a wrapper div inside a foreach loop and I'm trying to force it to echo one and not duplicate



    foreach ($apples as $apple) {
//echo only once bellow
<div class="Wrapper">
//echo only once Above

echo $apple;

//echo only once bellow
</div>
//echo only once Above
}


I do not wish to move my Wrapper div outside the foreach, It is very important for the div to be inside the foreach and to be without duplicates.







php html






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 18:14







Nippledisaster

















asked Nov 23 '18 at 18:06









NippledisasterNippledisaster

13010




13010








  • 1





    what is the reason to not put this div to outside of loop ?

    – C2486
    Nov 23 '18 at 18:08











  • That makes no sense. A wrapper is precisely the sort of thing you would do outside the loop, for the very fact it needs to be output once, not iteratively. If you really must do it inside, you need to set up a tracker variable which is set once it's output, then output the wrapper tags only if that variable has not yet been set.

    – Utkanos
    Nov 23 '18 at 18:10











  • use condition {for index} to print starting of div at element 1 and for last element close div?

    – Wasim Sayyed
    Nov 23 '18 at 18:10








  • 1





    Can you not post pseudo code please? What you have now isn't proper syntax. You're injecting pure HTML into PHP.

    – Funk Forty Niner
    Nov 23 '18 at 18:15













  • When you say you don't want to move the wrapper outside the loop, you're basically saying you don't want to do this the really obvious and straightforward way. Unless we know why you don't want to do that, we can't give you any decent answers.

    – iainn
    Nov 23 '18 at 18:24














  • 1





    what is the reason to not put this div to outside of loop ?

    – C2486
    Nov 23 '18 at 18:08











  • That makes no sense. A wrapper is precisely the sort of thing you would do outside the loop, for the very fact it needs to be output once, not iteratively. If you really must do it inside, you need to set up a tracker variable which is set once it's output, then output the wrapper tags only if that variable has not yet been set.

    – Utkanos
    Nov 23 '18 at 18:10











  • use condition {for index} to print starting of div at element 1 and for last element close div?

    – Wasim Sayyed
    Nov 23 '18 at 18:10








  • 1





    Can you not post pseudo code please? What you have now isn't proper syntax. You're injecting pure HTML into PHP.

    – Funk Forty Niner
    Nov 23 '18 at 18:15













  • When you say you don't want to move the wrapper outside the loop, you're basically saying you don't want to do this the really obvious and straightforward way. Unless we know why you don't want to do that, we can't give you any decent answers.

    – iainn
    Nov 23 '18 at 18:24








1




1





what is the reason to not put this div to outside of loop ?

– C2486
Nov 23 '18 at 18:08





what is the reason to not put this div to outside of loop ?

– C2486
Nov 23 '18 at 18:08













That makes no sense. A wrapper is precisely the sort of thing you would do outside the loop, for the very fact it needs to be output once, not iteratively. If you really must do it inside, you need to set up a tracker variable which is set once it's output, then output the wrapper tags only if that variable has not yet been set.

– Utkanos
Nov 23 '18 at 18:10





That makes no sense. A wrapper is precisely the sort of thing you would do outside the loop, for the very fact it needs to be output once, not iteratively. If you really must do it inside, you need to set up a tracker variable which is set once it's output, then output the wrapper tags only if that variable has not yet been set.

– Utkanos
Nov 23 '18 at 18:10













use condition {for index} to print starting of div at element 1 and for last element close div?

– Wasim Sayyed
Nov 23 '18 at 18:10







use condition {for index} to print starting of div at element 1 and for last element close div?

– Wasim Sayyed
Nov 23 '18 at 18:10






1




1





Can you not post pseudo code please? What you have now isn't proper syntax. You're injecting pure HTML into PHP.

– Funk Forty Niner
Nov 23 '18 at 18:15







Can you not post pseudo code please? What you have now isn't proper syntax. You're injecting pure HTML into PHP.

– Funk Forty Niner
Nov 23 '18 at 18:15















When you say you don't want to move the wrapper outside the loop, you're basically saying you don't want to do this the really obvious and straightforward way. Unless we know why you don't want to do that, we can't give you any decent answers.

– iainn
Nov 23 '18 at 18:24





When you say you don't want to move the wrapper outside the loop, you're basically saying you don't want to do this the really obvious and straightforward way. Unless we know why you don't want to do that, we can't give you any decent answers.

– iainn
Nov 23 '18 at 18:24












3 Answers
3






active

oldest

votes


















0














You didn't mention but If you need to put Wrapper div only if you have any count in $apples variable then simply check with if(count($apples)) and then put Wrapper



<?php
if(count($apples)){
echo '<div class="Wrapper">';
foreach ($apples as $apple) {
echo $apple;
}
echo '</div>';
}
?>





share|improve this answer































    0














    Note: I know this is not good way to write but as per his condition I am suggesting this code.





    I don't know why you don't want to put div outside foreach loop but anyway you can use the following code to achieve that...



    <?php
    foreach ($apples as $apple) {
    static $i = 0;
    if ($i == 0) {
    echo "<div class='Wrapper'>";
    }
    echo $apple;
    $i++;
    if ($i == count($apples)){
    echo "</div>";
    }
    }
    ?>





    share|improve this answer


























    • This will only wrap the first item in the <div> all others will be on there own.

      – Nigel Ren
      Nov 23 '18 at 19:17











    • @nigel thanks for pointing. I updated my answer.

      – Niraj Kaushal
      Nov 24 '18 at 2:33



















    0














    Maybe this will help since you want to keep it inside foreach loop



    $numItems = count($arr);

    $i = 0;

    foreach($arr as $ar) {

    $i = $i+1;

    if ($i == 1) {
    echo '<div class="class">';
    }

    // do your stuff

    if($i == $numItems) {
    echo "</div>";
    }
    }





    share|improve this answer





















    • 1





      Seems wrong unless you do $i = 1;.

      – nkkollaw
      Nov 23 '18 at 18:32











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53451242%2fphp-how-to-use-a-div-to-wrap-all-items-inside-a-foreach-loop%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You didn't mention but If you need to put Wrapper div only if you have any count in $apples variable then simply check with if(count($apples)) and then put Wrapper



    <?php
    if(count($apples)){
    echo '<div class="Wrapper">';
    foreach ($apples as $apple) {
    echo $apple;
    }
    echo '</div>';
    }
    ?>





    share|improve this answer




























      0














      You didn't mention but If you need to put Wrapper div only if you have any count in $apples variable then simply check with if(count($apples)) and then put Wrapper



      <?php
      if(count($apples)){
      echo '<div class="Wrapper">';
      foreach ($apples as $apple) {
      echo $apple;
      }
      echo '</div>';
      }
      ?>





      share|improve this answer


























        0












        0








        0







        You didn't mention but If you need to put Wrapper div only if you have any count in $apples variable then simply check with if(count($apples)) and then put Wrapper



        <?php
        if(count($apples)){
        echo '<div class="Wrapper">';
        foreach ($apples as $apple) {
        echo $apple;
        }
        echo '</div>';
        }
        ?>





        share|improve this answer













        You didn't mention but If you need to put Wrapper div only if you have any count in $apples variable then simply check with if(count($apples)) and then put Wrapper



        <?php
        if(count($apples)){
        echo '<div class="Wrapper">';
        foreach ($apples as $apple) {
        echo $apple;
        }
        echo '</div>';
        }
        ?>






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 18:19









        C2486C2486

        19.1k32767




        19.1k32767

























            0














            Note: I know this is not good way to write but as per his condition I am suggesting this code.





            I don't know why you don't want to put div outside foreach loop but anyway you can use the following code to achieve that...



            <?php
            foreach ($apples as $apple) {
            static $i = 0;
            if ($i == 0) {
            echo "<div class='Wrapper'>";
            }
            echo $apple;
            $i++;
            if ($i == count($apples)){
            echo "</div>";
            }
            }
            ?>





            share|improve this answer


























            • This will only wrap the first item in the <div> all others will be on there own.

              – Nigel Ren
              Nov 23 '18 at 19:17











            • @nigel thanks for pointing. I updated my answer.

              – Niraj Kaushal
              Nov 24 '18 at 2:33
















            0














            Note: I know this is not good way to write but as per his condition I am suggesting this code.





            I don't know why you don't want to put div outside foreach loop but anyway you can use the following code to achieve that...



            <?php
            foreach ($apples as $apple) {
            static $i = 0;
            if ($i == 0) {
            echo "<div class='Wrapper'>";
            }
            echo $apple;
            $i++;
            if ($i == count($apples)){
            echo "</div>";
            }
            }
            ?>





            share|improve this answer


























            • This will only wrap the first item in the <div> all others will be on there own.

              – Nigel Ren
              Nov 23 '18 at 19:17











            • @nigel thanks for pointing. I updated my answer.

              – Niraj Kaushal
              Nov 24 '18 at 2:33














            0












            0








            0







            Note: I know this is not good way to write but as per his condition I am suggesting this code.





            I don't know why you don't want to put div outside foreach loop but anyway you can use the following code to achieve that...



            <?php
            foreach ($apples as $apple) {
            static $i = 0;
            if ($i == 0) {
            echo "<div class='Wrapper'>";
            }
            echo $apple;
            $i++;
            if ($i == count($apples)){
            echo "</div>";
            }
            }
            ?>





            share|improve this answer















            Note: I know this is not good way to write but as per his condition I am suggesting this code.





            I don't know why you don't want to put div outside foreach loop but anyway you can use the following code to achieve that...



            <?php
            foreach ($apples as $apple) {
            static $i = 0;
            if ($i == 0) {
            echo "<div class='Wrapper'>";
            }
            echo $apple;
            $i++;
            if ($i == count($apples)){
            echo "</div>";
            }
            }
            ?>






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 24 '18 at 5:32

























            answered Nov 23 '18 at 18:19









            Niraj KaushalNiraj Kaushal

            779511




            779511













            • This will only wrap the first item in the <div> all others will be on there own.

              – Nigel Ren
              Nov 23 '18 at 19:17











            • @nigel thanks for pointing. I updated my answer.

              – Niraj Kaushal
              Nov 24 '18 at 2:33



















            • This will only wrap the first item in the <div> all others will be on there own.

              – Nigel Ren
              Nov 23 '18 at 19:17











            • @nigel thanks for pointing. I updated my answer.

              – Niraj Kaushal
              Nov 24 '18 at 2:33

















            This will only wrap the first item in the <div> all others will be on there own.

            – Nigel Ren
            Nov 23 '18 at 19:17





            This will only wrap the first item in the <div> all others will be on there own.

            – Nigel Ren
            Nov 23 '18 at 19:17













            @nigel thanks for pointing. I updated my answer.

            – Niraj Kaushal
            Nov 24 '18 at 2:33





            @nigel thanks for pointing. I updated my answer.

            – Niraj Kaushal
            Nov 24 '18 at 2:33











            0














            Maybe this will help since you want to keep it inside foreach loop



            $numItems = count($arr);

            $i = 0;

            foreach($arr as $ar) {

            $i = $i+1;

            if ($i == 1) {
            echo '<div class="class">';
            }

            // do your stuff

            if($i == $numItems) {
            echo "</div>";
            }
            }





            share|improve this answer





















            • 1





              Seems wrong unless you do $i = 1;.

              – nkkollaw
              Nov 23 '18 at 18:32
















            0














            Maybe this will help since you want to keep it inside foreach loop



            $numItems = count($arr);

            $i = 0;

            foreach($arr as $ar) {

            $i = $i+1;

            if ($i == 1) {
            echo '<div class="class">';
            }

            // do your stuff

            if($i == $numItems) {
            echo "</div>";
            }
            }





            share|improve this answer





















            • 1





              Seems wrong unless you do $i = 1;.

              – nkkollaw
              Nov 23 '18 at 18:32














            0












            0








            0







            Maybe this will help since you want to keep it inside foreach loop



            $numItems = count($arr);

            $i = 0;

            foreach($arr as $ar) {

            $i = $i+1;

            if ($i == 1) {
            echo '<div class="class">';
            }

            // do your stuff

            if($i == $numItems) {
            echo "</div>";
            }
            }





            share|improve this answer















            Maybe this will help since you want to keep it inside foreach loop



            $numItems = count($arr);

            $i = 0;

            foreach($arr as $ar) {

            $i = $i+1;

            if ($i == 1) {
            echo '<div class="class">';
            }

            // do your stuff

            if($i == $numItems) {
            echo "</div>";
            }
            }






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 29 '18 at 12:03

























            answered Nov 23 '18 at 18:17









            Wasim SayyedWasim Sayyed

            434417




            434417








            • 1





              Seems wrong unless you do $i = 1;.

              – nkkollaw
              Nov 23 '18 at 18:32














            • 1





              Seems wrong unless you do $i = 1;.

              – nkkollaw
              Nov 23 '18 at 18:32








            1




            1





            Seems wrong unless you do $i = 1;.

            – nkkollaw
            Nov 23 '18 at 18:32





            Seems wrong unless you do $i = 1;.

            – nkkollaw
            Nov 23 '18 at 18:32


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53451242%2fphp-how-to-use-a-div-to-wrap-all-items-inside-a-foreach-loop%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Feedback on college project

            Futebolista

            Albești (Vaslui)