Welcome Guest, Not a member yet? Register   Sign In
[Solved] Codeigniter Query Misses First One Off List
#1

(This post was last modified: 12-30-2015, 02:33 AM by wolfgang1983.)

On my query2 foreach loop it produces a list as shown in image that is attached.

But for some reason it keeps on missing the first ones on both list from extra events table

It's missing Test 1 and Example 1


I am not sure why it does not show it on list seems to skip it.

PHP Code:
public function get_events($year$month) {
 
       $calendar = array();

 
       $this->db->select('*');
 
       $this->db->from('events');
 
       $this->db->where('year'$year);
 
       $this->db->where('month'$month);
 
       $query1 $this->db->get();

 
       foreach ($query1->result_array() as $event) {

 
           $this->db->select('*');
         
   $this->db->from('extra_events');
         
   $this->db->where('events_id'$event['events_id']);
         
   $this->db->where('year'$year);
         
   $this->db->where('month'$month);
         
   //$this->db->limit(2);
         
   $query2 $this->db->get();

         
   foreach ($query2->result_array() as $result) {

     
           if (array_key_exists($event['day'], $calendar)) {

     
               // Misses the first one on the list 

             
       $calendar[$event['day']] = $calendar[$event['day']] . '<br/>' $result['extra_event_id'] .' '$result['event'];
             
             
                
} else {

     
               $calendar[$event['day']] = $result['events_id'] .' '$event['event'];

     
           }

 
           }
 
       }

 
       return $calendar;

 
   

Any ideas on how to solve issue?

Attached Files Thumbnail(s)
   
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#2

I don't see exactly where the problem occurs, but you should consider using a join to retrieve the results in a single query, rather than making an additional query for each result of the first query. Regardless of whether it makes it easier to generate the output, it will speed things up, especially when you have more events in the database.
Reply
#3

(12-29-2015, 08:45 AM)mwhitney Wrote: I don't see exactly where the problem occurs, but you should consider using a join to retrieve the results in a single query, rather than making an additional query for each result of the first query. Regardless of whether it makes it easier to generate the output, it will speed things up, especially when you have more events in the database.

Yes I have used join before does not let me use the db limit correct and produces the same result.
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#4

(This post was last modified: 12-30-2015, 02:42 AM by wolfgang1983.)

(12-29-2015, 08:45 AM)mwhitney Wrote: I don't see exactly where the problem occurs, but you should consider using a join to retrieve the results in a single query, rather than making an additional query for each result of the first query. Regardless of whether it makes it easier to generate the output, it will speed things up, especially when you have more events in the database.

I have solved it here are the changes I have had to make

Code:
public function get_calendar_events($year, $month) {
   $calendar = array();

   $events = $this->get_events($year, $month);

   foreach($events as $event) {

       $calendar[$event['day']] = '<br/>' . $event['event'];

       $extra_events = $this->get_extra_events($event['events_id']);

       foreach($extra_events as $extra_event) {
           if (array_key_exists($extra_event['day'], $calendar)) {
               $calendar[$extra_event['day']] = $calendar[$extra_event['day']] . '<br/>' .  $extra_event['event'];
           } else {
               $calendar[$extra_event['day']] =  $extra_event['event'];
           }
       }
   }


   return $calendar;
}

public function get_events($year, $month) {
   $this->db->where('year', $year);
   $this->db->where('month', $month);
   $events = $this->db->get('events');
   return $events->result_array();
}

public function get_extra_events($events_id) {
   $this->db->limit(5);
   $this->db->where('events_id', $events_id);
   $extra_events = $this->db->get('extra_events');
   return $extra_events->result_array();
}
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB