Output Problem or my Logic is broken :) need some help
Okay here's the problem. I have Event Section and on my frontpage i want to Display Events for Today. and if there's no event only show 1 field with text "sorry no events today".
Here's the code:
@{ //get all Posts dynamic eventList = Model.AncestorOrSelf(1).DescendantsOrSelf("eventItem");
} @foreach(dynamic n in eventList.Take(10).OrderBy("Name desc")) { var crop = n.eventImg.Find("@name","Event Square").url; var excerpt = umbraco.library.StripHtml(@n.eContentBody.ToString()); var date1 = n.eStartDate.ToString("ddMMMyyyy"); var date2 = DateTime.Now.ToString("ddMMMyyyy"); if(date1 == date2) { <li> <div class="image"> <a href="@n.Url"><img src="@crop" alt="category pic"></a> </div><!--image--> <div class="details"> <h5><a href="@n.Url">@MyHelpers.Truncate(excerpt, 61)</a></h5> <span class="date">@datum, <a href="#">8 Comments</a></span> </div><!--details--> </li> } else { <li><p>Sorry no events for today</p></li> } }
Okay everythin works fine, but when the else statement goes on, it's outputting 10 times ( i know foreach is looping 10 times) the sorry no events text.
A quick fix is simply put a counter within your if statement. Once you're finished with your loop, just check if < 1 and output "Sorry, no events..." accordingly.
int i = 0;
@foreach...{...
if... i++; ...
...}
if (i < 1) { <h3>Sorry, no events for today</h3> }
Output Problem or my Logic is broken :) need some help
Okay here's the problem. I have Event Section and on my frontpage i want to Display Events for Today.
and if there's no event only show 1 field with text "sorry no events today".
Here's the code:
Okay everythin works fine, but when the else statement goes on, it's outputting 10 times ( i know foreach is looping 10 times) the sorry no events text.
Is there a solution to only output it 1 time?
A quick fix is simply put a counter within your if statement. Once you're finished with your loop, just check if < 1 and output "Sorry, no events..." accordingly.
int i = 0;
@foreach...{...
if...
i++;
...
...}
if (i < 1)
{
<h3>Sorry, no events for today</h3>
}
Often i do not see the wood for the trees. Thanks for the little hint in the right direction.
is working on a reply...