Php inside foreach loop if $i == 1 echo “ok”, else echo “ok” JUST one time and continue
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
its been a while (5 years) since my last time here making questions.
I need help, I'm developing an event calendar and I'm stuck in this.
I want to have event header and event body, so far so good, but I just want one header if there are more than one event in each day.
So today I've a dinner at 21h, my event should have Friday , November 23 and Dinner at 21h.
Perfect, I can do this, but what about if I do have a meeting before dinner, at 19h?
I want just one time the same header ... Friday, November 23 but 2 bodies...
thanks!
my piece of code
foreach($eventos[$data_do_dia] as $evento) {
$i = count($eventos[$data_do_dia]);
if($i == 1) {
$agenda .= " dia ".date("d-m-Y",strtotime($evento['dia']))."</b>";
add the event
} else {
repeat the same but just one time
$agenda .= " dia ".date("d-m-Y",strtotime($evento['dia']))."</b>";
add event one
add event two
...
}
}
php
add a comment |
its been a while (5 years) since my last time here making questions.
I need help, I'm developing an event calendar and I'm stuck in this.
I want to have event header and event body, so far so good, but I just want one header if there are more than one event in each day.
So today I've a dinner at 21h, my event should have Friday , November 23 and Dinner at 21h.
Perfect, I can do this, but what about if I do have a meeting before dinner, at 19h?
I want just one time the same header ... Friday, November 23 but 2 bodies...
thanks!
my piece of code
foreach($eventos[$data_do_dia] as $evento) {
$i = count($eventos[$data_do_dia]);
if($i == 1) {
$agenda .= " dia ".date("d-m-Y",strtotime($evento['dia']))."</b>";
add the event
} else {
repeat the same but just one time
$agenda .= " dia ".date("d-m-Y",strtotime($evento['dia']))."</b>";
add event one
add event two
...
}
}
php
If$eventos[$data_do_dia]
is an array of events for a single day, then can't you just output the header before the loop?
– iainn
Nov 23 '18 at 18:19
I think I can't... this is the print_r of my test array Array ( [0] => Array ( [dia] => 2018-11-19 [hora] => 09:30 [titulo] => Título do evento para o dia 19/11/2018 [evento] => Texto do evento do dia 19/11/2018 ) ) Array ( [0] => Array ( [dia] => 2018-11-20 [hora] => 11:30 [titulo] => Título do evento do dia 20/11/2018 [evento] => Texto do evento do dia 20/11/2018 ) [1] => Array ( [dia] => 2018-11-20 [hora] => 14:00 [titulo] => Novo evento no dia 20 [evento] => Novo evento para o dia 20 de Novembro de 2018 ) ). I loop the array and get $evento['hora'], $evento['evento'].
– Pluda
Nov 23 '18 at 18:32
add a comment |
its been a while (5 years) since my last time here making questions.
I need help, I'm developing an event calendar and I'm stuck in this.
I want to have event header and event body, so far so good, but I just want one header if there are more than one event in each day.
So today I've a dinner at 21h, my event should have Friday , November 23 and Dinner at 21h.
Perfect, I can do this, but what about if I do have a meeting before dinner, at 19h?
I want just one time the same header ... Friday, November 23 but 2 bodies...
thanks!
my piece of code
foreach($eventos[$data_do_dia] as $evento) {
$i = count($eventos[$data_do_dia]);
if($i == 1) {
$agenda .= " dia ".date("d-m-Y",strtotime($evento['dia']))."</b>";
add the event
} else {
repeat the same but just one time
$agenda .= " dia ".date("d-m-Y",strtotime($evento['dia']))."</b>";
add event one
add event two
...
}
}
php
its been a while (5 years) since my last time here making questions.
I need help, I'm developing an event calendar and I'm stuck in this.
I want to have event header and event body, so far so good, but I just want one header if there are more than one event in each day.
So today I've a dinner at 21h, my event should have Friday , November 23 and Dinner at 21h.
Perfect, I can do this, but what about if I do have a meeting before dinner, at 19h?
I want just one time the same header ... Friday, November 23 but 2 bodies...
thanks!
my piece of code
foreach($eventos[$data_do_dia] as $evento) {
$i = count($eventos[$data_do_dia]);
if($i == 1) {
$agenda .= " dia ".date("d-m-Y",strtotime($evento['dia']))."</b>";
add the event
} else {
repeat the same but just one time
$agenda .= " dia ".date("d-m-Y",strtotime($evento['dia']))."</b>";
add event one
add event two
...
}
}
php
php
edited Nov 23 '18 at 18:33
Pluda
asked Nov 23 '18 at 18:16
PludaPluda
1,04851543
1,04851543
If$eventos[$data_do_dia]
is an array of events for a single day, then can't you just output the header before the loop?
– iainn
Nov 23 '18 at 18:19
I think I can't... this is the print_r of my test array Array ( [0] => Array ( [dia] => 2018-11-19 [hora] => 09:30 [titulo] => Título do evento para o dia 19/11/2018 [evento] => Texto do evento do dia 19/11/2018 ) ) Array ( [0] => Array ( [dia] => 2018-11-20 [hora] => 11:30 [titulo] => Título do evento do dia 20/11/2018 [evento] => Texto do evento do dia 20/11/2018 ) [1] => Array ( [dia] => 2018-11-20 [hora] => 14:00 [titulo] => Novo evento no dia 20 [evento] => Novo evento para o dia 20 de Novembro de 2018 ) ). I loop the array and get $evento['hora'], $evento['evento'].
– Pluda
Nov 23 '18 at 18:32
add a comment |
If$eventos[$data_do_dia]
is an array of events for a single day, then can't you just output the header before the loop?
– iainn
Nov 23 '18 at 18:19
I think I can't... this is the print_r of my test array Array ( [0] => Array ( [dia] => 2018-11-19 [hora] => 09:30 [titulo] => Título do evento para o dia 19/11/2018 [evento] => Texto do evento do dia 19/11/2018 ) ) Array ( [0] => Array ( [dia] => 2018-11-20 [hora] => 11:30 [titulo] => Título do evento do dia 20/11/2018 [evento] => Texto do evento do dia 20/11/2018 ) [1] => Array ( [dia] => 2018-11-20 [hora] => 14:00 [titulo] => Novo evento no dia 20 [evento] => Novo evento para o dia 20 de Novembro de 2018 ) ). I loop the array and get $evento['hora'], $evento['evento'].
– Pluda
Nov 23 '18 at 18:32
If
$eventos[$data_do_dia]
is an array of events for a single day, then can't you just output the header before the loop?– iainn
Nov 23 '18 at 18:19
If
$eventos[$data_do_dia]
is an array of events for a single day, then can't you just output the header before the loop?– iainn
Nov 23 '18 at 18:19
I think I can't... this is the print_r of my test array Array ( [0] => Array ( [dia] => 2018-11-19 [hora] => 09:30 [titulo] => Título do evento para o dia 19/11/2018 [evento] => Texto do evento do dia 19/11/2018 ) ) Array ( [0] => Array ( [dia] => 2018-11-20 [hora] => 11:30 [titulo] => Título do evento do dia 20/11/2018 [evento] => Texto do evento do dia 20/11/2018 ) [1] => Array ( [dia] => 2018-11-20 [hora] => 14:00 [titulo] => Novo evento no dia 20 [evento] => Novo evento para o dia 20 de Novembro de 2018 ) ). I loop the array and get $evento['hora'], $evento['evento'].
– Pluda
Nov 23 '18 at 18:32
I think I can't... this is the print_r of my test array Array ( [0] => Array ( [dia] => 2018-11-19 [hora] => 09:30 [titulo] => Título do evento para o dia 19/11/2018 [evento] => Texto do evento do dia 19/11/2018 ) ) Array ( [0] => Array ( [dia] => 2018-11-20 [hora] => 11:30 [titulo] => Título do evento do dia 20/11/2018 [evento] => Texto do evento do dia 20/11/2018 ) [1] => Array ( [dia] => 2018-11-20 [hora] => 14:00 [titulo] => Novo evento no dia 20 [evento] => Novo evento para o dia 20 de Novembro de 2018 ) ). I loop the array and get $evento['hora'], $evento['evento'].
– Pluda
Nov 23 '18 at 18:32
add a comment |
1 Answer
1
active
oldest
votes
I've changed the loop so that there isn't any repeated code, this basically means that if there is only one event, it converts into an array, assuming that isset($evento[0])
will pick up there aren't sub-arrays, it wraps the one entry into a new array.
It then loops over the array and creates the string with the date/time/details. You will probably want to change the format, but the principle is the same...
$agenda = "";
foreach($eventos as $evento) {
if ( !isset($evento[0]) ) {
$evento = [$evento];
}
$agenda .= " dia ".date("d-m-Y",strtotime($evento[0]['dia'])).PHP_EOL;
foreach ( $evento as $time ) {
$agenda .= " time ".$time['hora']." ".
$time['titulo']." ".$time['evento'].PHP_EOL;
}
}
echo $agenda;
Thanks! works perfect, I haven't think on foreach inside foreach.
– Pluda
Nov 23 '18 at 19:21
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%2f53451363%2fphp-inside-foreach-loop-if-i-1-echo-ok-else-echo-ok-just-one-time-and-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I've changed the loop so that there isn't any repeated code, this basically means that if there is only one event, it converts into an array, assuming that isset($evento[0])
will pick up there aren't sub-arrays, it wraps the one entry into a new array.
It then loops over the array and creates the string with the date/time/details. You will probably want to change the format, but the principle is the same...
$agenda = "";
foreach($eventos as $evento) {
if ( !isset($evento[0]) ) {
$evento = [$evento];
}
$agenda .= " dia ".date("d-m-Y",strtotime($evento[0]['dia'])).PHP_EOL;
foreach ( $evento as $time ) {
$agenda .= " time ".$time['hora']." ".
$time['titulo']." ".$time['evento'].PHP_EOL;
}
}
echo $agenda;
Thanks! works perfect, I haven't think on foreach inside foreach.
– Pluda
Nov 23 '18 at 19:21
add a comment |
I've changed the loop so that there isn't any repeated code, this basically means that if there is only one event, it converts into an array, assuming that isset($evento[0])
will pick up there aren't sub-arrays, it wraps the one entry into a new array.
It then loops over the array and creates the string with the date/time/details. You will probably want to change the format, but the principle is the same...
$agenda = "";
foreach($eventos as $evento) {
if ( !isset($evento[0]) ) {
$evento = [$evento];
}
$agenda .= " dia ".date("d-m-Y",strtotime($evento[0]['dia'])).PHP_EOL;
foreach ( $evento as $time ) {
$agenda .= " time ".$time['hora']." ".
$time['titulo']." ".$time['evento'].PHP_EOL;
}
}
echo $agenda;
Thanks! works perfect, I haven't think on foreach inside foreach.
– Pluda
Nov 23 '18 at 19:21
add a comment |
I've changed the loop so that there isn't any repeated code, this basically means that if there is only one event, it converts into an array, assuming that isset($evento[0])
will pick up there aren't sub-arrays, it wraps the one entry into a new array.
It then loops over the array and creates the string with the date/time/details. You will probably want to change the format, but the principle is the same...
$agenda = "";
foreach($eventos as $evento) {
if ( !isset($evento[0]) ) {
$evento = [$evento];
}
$agenda .= " dia ".date("d-m-Y",strtotime($evento[0]['dia'])).PHP_EOL;
foreach ( $evento as $time ) {
$agenda .= " time ".$time['hora']." ".
$time['titulo']." ".$time['evento'].PHP_EOL;
}
}
echo $agenda;
I've changed the loop so that there isn't any repeated code, this basically means that if there is only one event, it converts into an array, assuming that isset($evento[0])
will pick up there aren't sub-arrays, it wraps the one entry into a new array.
It then loops over the array and creates the string with the date/time/details. You will probably want to change the format, but the principle is the same...
$agenda = "";
foreach($eventos as $evento) {
if ( !isset($evento[0]) ) {
$evento = [$evento];
}
$agenda .= " dia ".date("d-m-Y",strtotime($evento[0]['dia'])).PHP_EOL;
foreach ( $evento as $time ) {
$agenda .= " time ".$time['hora']." ".
$time['titulo']." ".$time['evento'].PHP_EOL;
}
}
echo $agenda;
answered Nov 23 '18 at 19:03
Nigel RenNigel Ren
28.9k62034
28.9k62034
Thanks! works perfect, I haven't think on foreach inside foreach.
– Pluda
Nov 23 '18 at 19:21
add a comment |
Thanks! works perfect, I haven't think on foreach inside foreach.
– Pluda
Nov 23 '18 at 19:21
Thanks! works perfect, I haven't think on foreach inside foreach.
– Pluda
Nov 23 '18 at 19:21
Thanks! works perfect, I haven't think on foreach inside foreach.
– Pluda
Nov 23 '18 at 19:21
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%2f53451363%2fphp-inside-foreach-loop-if-i-1-echo-ok-else-echo-ok-just-one-time-and-c%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
If
$eventos[$data_do_dia]
is an array of events for a single day, then can't you just output the header before the loop?– iainn
Nov 23 '18 at 18:19
I think I can't... this is the print_r of my test array Array ( [0] => Array ( [dia] => 2018-11-19 [hora] => 09:30 [titulo] => Título do evento para o dia 19/11/2018 [evento] => Texto do evento do dia 19/11/2018 ) ) Array ( [0] => Array ( [dia] => 2018-11-20 [hora] => 11:30 [titulo] => Título do evento do dia 20/11/2018 [evento] => Texto do evento do dia 20/11/2018 ) [1] => Array ( [dia] => 2018-11-20 [hora] => 14:00 [titulo] => Novo evento no dia 20 [evento] => Novo evento para o dia 20 de Novembro de 2018 ) ). I loop the array and get $evento['hora'], $evento['evento'].
– Pluda
Nov 23 '18 at 18:32