1

I am building a calendar application which will allow our admin team to input staff holidays, training days, sick days etc and all staff to view who is in/out on any given day. The calendar is built as is the mechanism to record days off. I'm struggling to display who is off when. Using print_r I get the following back from the server, showing name, date and reason for absence:

Array ( 
[0] => stdClass Object ( [name] => Becca [date] => 2012-10-10  [reason] => Sick ) 
[1] => stdClass Object ( [name] => Frank [date] => 2012-09-12 [reason] => Sic ) 
[2] => stdClass Object ( [name] => Frank [date] => 2012-10-14 [reason] => Sic ) 
[3] => stdClass Object ( [name] => Paola [date] => 2012-10-10 [reason] => Sic ) 
[4] => stdClass Object ( [name] => Clive [date] => 2012-10-14 [reason] => Hol ) ) 

I can iterate through and display some of the names and dates, but if more than one person is off on a given date (eg Becca, Paola), only the last name is displayed, I believe because the key must be unique. The following code does this, where 'date("Y-m-d", $date)' is the means of printing the dates on the calendar :

foreach ($absenceArray as $dateofabsence=>$name){
    if (date("Y-m-d", $date) == $dateofabsence){
        $showname[$x] = $name; 
    }   
}   

Can someone please demonstrate how I can iterate through the above array and link each name to a corresponding date in my calendar.

I apologise that I no doubt have not explained myself properly but my brain is frazzled.

2
  • 3
    what does the variable $x mean? Commented Sep 12, 2012 at 16:38
  • I'm using a for ($x = 0; $x < 365; $x++){ } to iterate through the days of the year. Using the $x here makes $showname unique for each day Commented Sep 13, 2012 at 7:16

2 Answers 2

2

If you want to see what persons where absent on a given day I would first parse the array to have an array based on date as key. Especially in an environment where you want to add this stuff to a calendar it may prove useful.

$absenceByDate = array();
foreach ($absenceArray as $index => $record) {
    if (!array_key_exists($record->date, $absenceByDate)) {
        $absenceByDate[$record->date] = array();
    }

    $absenceByDate[$record->date][] = $record; 
}

Now you will have a nice array based on date (as key) and you can simply loop through your calender and check whether there is an item with the key of the current date. If that is the case at least one person was absent. See the demo for a working example.

Sign up to request clarification or add additional context in comments.

1 Comment

Isn't record an object or am i missing something ?
0

When iterating through this array, the key will be numeric and the 'value' will be an object, as you can see in the print_r dump. So the way to go will be like:

$showname = array();
foreach($absenceArray as $object)
{
  if(date("Y-m-d", $date) === $object->date)
  {
    $showname[] = $object->name;
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.