Ruchan
Ruchan

Reputation: 3194

Show Records In Table

I have records from database like this.

My Records

I Want to Show a table like this Table to show Like

the Days goes up to 32 and is by default 1 to 32. I have to insert the records where the days match.

Because the method i tried is too much messy and also not working. Can you guys suggest me some ways to Do it? I need to show it in HTML table. any language is Good.. I just need the idea. preferably MVC C#

UPDATE Thank you both... I got idea from both of you (unlimit and Joe Enos) and I did it. Can you help me with one more same problem? What if Area 1 has another record of 2 hours on same day. I need to show them separately. How will I show the record in same day?

enter image description here

Upvotes: 1

Views: 162

Answers (2)

unlimit
unlimit

Reputation: 3752

You may try something like this, I have not checked for correctness of syntax etc.

Dictionary<string,int[]> data = new Dictionary<string,int[]>();

foreach (var record in database)
{
        if (!data.ContainsKey(record["location"]))
        {
            int[] days = new int[32];
            days[record["day"]] = record["hour"];                
            data.Add(record["location"], days);
        }
        else
        {
            int[] days = data[record["location"]];
            days[record["day"]] = record["hour"]; 
            //data[record["location"]] = days;
        }
}

The variable data should have your data in the format you want. You then loop through this and populate the html table.

EDIT in response to the comment by OP

If there are more than one set of hours per day and the number of this set is not fixed, then you can use something like this.

 Dictionary<string,ArrayList> day = new Dictionary<string,ArrayList>();
 Dictionary<string,day> location = new Dictionary<string,day>();

Here day dictionary will hold keys like Day 1, Day 2, Day 3, etc... and the value ArrayList will hold an array of hours in that day.

The location dictionary will hold keys like Area 1, Area 2, etc... and the value will be the day dictionary.

Maybe this will give you some hints on how to go about solving this.

Upvotes: 2

Joe Enos
Joe Enos

Reputation: 40393

You're going to want to convert your data into something more usable. Once you do that, turning it into rows is easy.

Build a class representing an area, which contains an array of values, for each of the 32 slots. As you're reading the data, populate the appropriate values in your array, something like:

class Area {
    public Area() { Values = new int[32]; }
    public int[] Values { get; set; }
    public int AreaNumber { get; set; }
}

var areas = new List<Area>();
foreach (var record in databaseRecords) {
    var area = // find area from list based on AreaNumber. If not there, add it
    area[record.Day - 1] = record.Hour;
}

Now you've got a list of Areas, each with a 32-item array. Now your HTML table can be built with a two-level loop, first looping through all areas, then looping through the 32 items in the array.

Upvotes: 2

Related Questions