0

I am trying to make a function that grabs all the days that have events that are in a database for a certain user. For instance if there were two events on Jan 23, 2013 it would add Jan 23, 2013 to the array. I got it to work so it adds all the days (without adding the same day twice) but now I want to be able to say how many dates are on each day. So on Jan 23, 2013 it would say they have two events in that day.

I hope this makes sense... I have some code for further aid.

PHP Function (grabbing each day that has events)

//gets upcoming days that have events for a user
    public function get_upcoming_days_with_events() {
        $return_array = array();
        $date_in=null;
        $user_id = $this->session->userdata('id');
        $query =$this->db->select()->from('events')->where('user_id', $user_id)->get();
        foreach ($query->result_array() as $event => $row) {
            $date = strtotime($row['date_due']);
            if (sizeof($return_array) != 0) {
                foreach ($return_array as $date_in_array => $row) {
                    $d = $row['full_date'];
                    if (date('Y-m-d', $date) == $d) {
                        //date is already in array
                        //increment the number of assignments on this day
                        $row['number_of_assignments'] += 1;
                        $date_in = true;
                    } else{
                        $date_in = false;
                    }
                }
            }
            if ($date_in == false) {
                $return_array[] = array(
                    'day' => date('d', $date),
                    'month' => date('m', $date),
                    'full_date' => date('Y-m-d', $date),
                    'number_of_assignments' => 1
                );
            }
        }
        return $return_array;
    }

So with this I want to be able to increment $return_array['number_of_assignments'] if my function notices that a certain day has more than one event.

Let me know if you need any more info...

Thanks! :)

2
  • Your return_array seems to be empty, How are you passing the values? Commented Jul 22, 2013 at 2:27
  • We can save the info in return_array by index of date, if the date info have not been set into return_array, we make an empty info. Each time, we simply increase number_of_assignments. Hope my code would be helpful. Any question, feel free to comment here. Commented Jul 22, 2013 at 2:33

2 Answers 2

2

We can save the info in return_array by index of date, if the date info have not been set into return_array, we make an empty info. Each time, we simply increase number_of_assignments.

 public function get_upcoming_days_with_events()
 {
     $return_array = array();
     $user_id = $this->session->userdata('id');
     $query =$this->db->select()->from('events')->where('user_id', $user_id)->get();
     foreach ($query->result_array() as $event => $row)
     {
         $date = strtotime($row['date_due']);
         $date_key = date('Y-m-d', $date);
         if (!isset($return_array[$date_key]))
         {
             $new_item = array(
                 'day' => date('d', $date),
                 'month' => date('m', $date),
                 'full_date' => $date_key,
                 'number_of_assignments' => 0,
             );
             $return_array[$date_key] = $new_item;
         }
         $return_array[$date_key]['number_of_assignments']++;
     }
     $return_array = array_values($return_array);
     return $return_array;
 }
Sign up to request clarification or add additional context in comments.

Comments

1

Function: array_count_values() can help you with this, it gives total number of occurance of a value in an array.

For example:

$a = array("apple", "banana", "apple");
var_dump(array_count_values($a)); 

Will output

array(
   [apple] => 2,
   [banana] => 1
);

So instead of trying to filter out duplicate events, add them all on the array and then use array_count_values() at last to know their occurenses.

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.