PHP - How to use a div to wrap all items Inside a foreach loop
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
|
show 1 more comment
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
1
what is the reason to not put thisdivto 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
|
show 1 more comment
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
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
php html
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 thisdivto 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
|
show 1 more comment
1
what is the reason to not put thisdivto 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
|
show 1 more comment
3 Answers
3
active
oldest
votes
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>';
}
?>
add a comment |
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>";
}
}
?>
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
add a comment |
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>";
}
}
1
Seems wrong unless you do$i = 1;.
– nkkollaw
Nov 23 '18 at 18:32
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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>';
}
?>
add a comment |
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>';
}
?>
add a comment |
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>';
}
?>
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>';
}
?>
answered Nov 23 '18 at 18:19
C2486C2486
19.1k32767
19.1k32767
add a comment |
add a comment |
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>";
}
}
?>
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
add a comment |
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>";
}
}
?>
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
add a comment |
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>";
}
}
?>
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>";
}
}
?>
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
add a comment |
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
add a comment |
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>";
}
}
1
Seems wrong unless you do$i = 1;.
– nkkollaw
Nov 23 '18 at 18:32
add a comment |
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>";
}
}
1
Seems wrong unless you do$i = 1;.
– nkkollaw
Nov 23 '18 at 18:32
add a comment |
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>";
}
}
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>";
}
}
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
what is the reason to not put this
divto 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