Copied to clipboard

Flag this post as spam?

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


  • Eric McDonald 16 posts 137 karma points
    Sep 14, 2017 @ 14:37
    Eric McDonald
    0

    HELLLP!!...Display Values from Child Pages by Week and Year

    Hi Guys,

    Looking for some guru level help here.

    I have a friends fishing website which has a list of child pages with some data I wish to loop through and display in a table. I wish to show the total number of fish caught by Week Number and Year. I have attached an image below to show what I'm trying to acheive.

    Each child page has a date the catch was posted and a textbox with the total number of fish caught.

    enter image description here

    Here is the code I have written so far which I think I can get the current weeks total but not all the weeks from the total child pages, I appreciate this is probably wrong but any help or pointers in the right direction would be good.

    var latestCatches = Umbraco.Content(1157);
    var currentYear = DateTime.Now.Year;
    var culture = CultureInfo.GetCultureInfo("en-GB");
    var dateTimeInfo = DateTimeFormatInfo.GetInstance(culture);
    var dateTime = DateTime.Today;
    int weekNumber = culture.Calendar.GetWeekOfYear(dateTime, dateTimeInfo.CalendarWeekRule, dateTimeInfo.FirstDayOfWeek);
    int WeeklyCatchTotal = 0;
    DateTime startOfWeek = DateTime.Today;
    int delta = DayOfWeek.Monday - startOfWeek.DayOfWeek;
    startOfWeek = startOfWeek.AddDays(delta);
    DateTime endOfWeek = startOfWeek.AddDays(5);
    
    
    
    
    foreach (var latestCatch in latestCatches.Children.Where("catchDate >= @0", startOfWeek))
    {
        if (latestCatches.catchDate >= startOfWeek || latestCatches.catchDate <= endOfWeek)
        {
            int total = latestCatch.totalFish;
            WeeklyCatchTotal = WeeklyCatchTotal + total;
    
        }
    
    }
    <td>Week @weekNumber</td><td>@WeeklyCatchTotal</td>
    

    }

  • Eric McDonald 16 posts 137 karma points
    Sep 17, 2017 @ 22:14
    Eric McDonald
    0

    Hi Guys,

    Still looking for some help here, I think I need some help in how I can get the data I require to be displayed. Like I mentioned above I have a list of child pages with 2 values on each page I need to work with. One is the totalcatches value which is an Int and the other value is the date the catch was reported.

    So there is a catch report for every day Monday to Saturday. So what Im trying to do is loop through all the child pages and total each catch report report by week so there is 6 reports per week.

    What I want to do then is display each week number and the total catches for that week by year.

    Could anyone give me a code example I could work with.

    Kind Regards Eric

  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    Sep 17, 2017 @ 22:22
    Alex Skrypnyk
    0

    Hi Eric

    I changed your code little bit, made it strongly typed, try this code:

    var latestCatches = Umbraco.TypedContent(1157);
    //var currentYear = DateTime.Now.Year;
    var culture = CultureInfo.GetCultureInfo("en-GB");
    var dateTimeInfo = DateTimeFormatInfo.GetInstance(culture);
    var dateTime = DateTime.Today;
    int weekNumber = culture.Calendar.GetWeekOfYear(dateTime, dateTimeInfo.CalendarWeekRule, dateTimeInfo.FirstDayOfWeek);
    int WeeklyCatchTotal = 0;
    DateTime startOfWeek = DateTime.Today;
    int delta = DayOfWeek.Monday - startOfWeek.DayOfWeek;
    startOfWeek = startOfWeek.AddDays(delta);
    DateTime endOfWeek = startOfWeek.AddDays(5);
    
    
    
    
    foreach (var latestCatch in latestCatches.Children.Where(x => x.GetPropertyValue<DateTime>("catchDate") >= startOfWeek))
    {
        if (latestCatches.GetPropertyValue<DateTime>("catchDate") >= startOfWeek || latestCatches.GetPropertyValue<DateTime>("catchDate") <= endOfWeek)
        {
            int total = latestCatch.GetPropertyValue<int>("totalFish");
            WeeklyCatchTotal = WeeklyCatchTotal + total;
        }
    
    }
    <td>Week @weekNumber</td><td>@WeeklyCatchTotal</td>
    

    Thanks,

    Alex

  • Eric McDonald 16 posts 137 karma points
    Sep 18, 2017 @ 20:01
    Eric McDonald
    0

    Hi Alex,

    Thanks for your input. So when I use your code I only get the current week which is 38. What do I need to do to my code so it calculates all totalFish for each week i.e 1-37 and for each year.?

    I have attached a screenshot below of what I want to acheive but im not sure if its possible.

    When I fetch the var latestCatches = Umbraco.TypedContent(1157) it returns the 203 childpages. But how do I loop through all and add each child pages totalfish to the week it was caught.

    enter image description here

    Again I really appreciate you responding with any help you can offer.

    Eric

  • Eric McDonald 16 posts 137 karma points
    Sep 19, 2017 @ 22:17
    Eric McDonald
    0

    Hi Alex,

    Did you manage to get a look at my issue above when running your code? Your code works perfect but only returns the current week. Can you take a look at my last post as I try and explain what I need in more detail.

    Kind Regards Eric

  • Eric McDonald 16 posts 137 karma points
    Sep 20, 2017 @ 23:05
    Eric McDonald
    0

    Hi Guys,

    Can anyone else help me here? Really struggling with this, just want to know if it can be done and if so a code example?

    Kind Regards Eric

  • Eric McDonald 16 posts 137 karma points
    Sep 28, 2017 @ 23:32
    Eric McDonald
    101

    Hi Guys,

    Finally managed to sort this out. Went down the route of using a Dictionary a key value pairs. Here's the code below in case anyone else needs to do something similar.

    @using umbraco.MacroEngines
    

    @using System.Globalization; @inherits umbraco.MacroEngines.DynamicNodeContext @inherits Umbraco.Web.Macros.PartialViewMacroPage

    @ var latestCatches = Umbraco.Content(1157); //var currentYear = DateTime.Now.Year; var culture = CultureInfo.GetCultureInfo("en-GB"); var dateTimeInfo = DateTimeFormatInfo.GetInstance(culture); var dateTime = DateTime.Today; int weekNumber = 0; Dictionary

    foreach (var latestCatch in latestCatches.Children.Where("Visible").OrderBy("CatchDate desc"))
    {
    
        var catchDateTime = latestCatch.catchDate;
        var catchDateYear = latestCatch.catchDate.ToString("yyyy");
        catchDateYear = Convert.ToInt32(catchDateYear);
        if (!yearArray.ContainsKey(catchDateYear))
        {
            weekArray = new Dictionary<int, int>();
            yearArray.Add(catchDateYear, weekArray);
        }
        weekNumber = culture.Calendar.GetWeekOfYear(catchDateTime, dateTimeInfo.CalendarWeekRule, dateTimeInfo.FirstDayOfWeek);
        int total = latestCatch.GetPropertyValue<int>("totalFish");
    
        if (weekArray.ContainsKey(weekNumber))
        {
            weekArray[weekNumber] = weekArray[weekNumber] + total;
        }
        else
        {
            weekArray.Add(weekNumber, total);
        }
    }
    <table class="table">
        <thead class="data-grid">
            <tr>
                <th>&nbsp;</th>
                @for (int x = yearArray.Count - 1; x >= 0; x--)
                {
                    <th>
                        @yearArray.ElementAtOrDefault(x).Key
                    </th>
                }
            </tr>
    
        </thead>
        <tbody>
            @for (int i = 1; i <= 39; i++)
            {
                <tr>
                    <td>Week @i</td>
                    @for (int x = yearArray.Count - 1; x >= 0; x--)
                    {
                        <td>
                            @if (yearArray.ElementAtOrDefault(x).Value.ContainsKey(i))
                            {
                                @yearArray.ElementAtOrDefault(x).Value[i];
                            }
    
                            &nbsp;
    
                        </td>
    
                    }
                </tr>
    
            }
        </tbody>
    </table>
    

    }

    Cumbernauld Web Design

  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    Sep 29, 2017 @ 11:16
    Alex Skrypnyk
    0

    Hi Eric

    Thank you for sharing with the community, nice solution!

    Thanks,

    Alex

Please Sign in or register to post replies

Write your reply to:

Draft