Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Toni Becker 146 posts 425 karma points
    Jul 30, 2011 @ 15:17
    Toni Becker
    0

    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.

    Is there a solution to only output it 1 time?

  • Ted Jardine 98 posts 120 karma points
    Jul 31, 2011 @ 04:20
    Ted Jardine
    0

    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>
    }

  • Toni Becker 146 posts 425 karma points
    Jul 31, 2011 @ 11:18
    Toni Becker
    0

    Often i do not see the wood for the trees. Thanks for the little hint in the right direction.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies