title: How to get the first and last login of an employee from large data in Codeigniter MYSQL?
body: <p>I have table called Daily_data2 in which i have login and logout details of biometric machine for users. Now a user  may login and go out for some work and come back again login again and may do this many time. So i want fetch only the first Login of a user and last Logout of that user for specific user.</p>
  
  <p>Now i have a table called users which has all the user names and i have mapped the EMP_ID from daily_data2 table so for each user so i can know which user is with which ID.</p>
  
  <p>I have a page called view in which i show the user, his emp_id and either login or logout. Above this i have kept a drop down in which i have passed names from database.</p>
  
  <p>I want select a name from drop down and show only his login/logout details.
  Also my code fetches all the login/logout details i just want it to fetch per day first login and last logout.</p>
  
  <p>This is my code:</p>
  
  <p>The View:</p>
  
  <pre><code>        &lt;div class="row"&gt;
              &lt;div class="col-lg-12"&gt;
                  &lt;div class="panel panel-primary"&gt;
                      &lt;div class="panel-heading"&gt;
                       All Calls Records
                      &lt;/div&gt;
                      &lt;!-- /.panel-heading --&gt;
                      &lt;div class="panel-body"&gt;
                          &lt;div class="dataTable_wrapper"&gt;
                              &lt;table class="table table-striped table-bordered table-hover" id="dataTables-example"&gt;
  
  
                                  &lt;thead&gt;
                                      &lt;tr&gt;
                                          &lt;th&gt;Emp_ID&lt;/th&gt;
                                           &lt;th&gt;Name&lt;/th&gt;
                                          &lt;th&gt;Date&lt;/th&gt;
                                          &lt;th&gt;Entry&lt;/th&gt;
  
  
  
                                      &lt;/tr&gt;
                                  &lt;/thead&gt;
  
  
  
                                  &lt;tbody&gt;
  
                                  &lt;?php if(count($attendances)): foreach($attendances as $attendance): ?&gt;
  
  
                                      &lt;tr class="odd gradeX"&gt;
                                         &lt;td&gt;&lt;?php echo trim($attendance-&gt;emp_id,'"&gt;');  ?&gt;&lt;/td&gt;  
                                          &lt;td&gt;&lt;?php echo trim($attendance-&gt;name,'"&gt;');  ?&gt;&lt;/td&gt;  
                                         &lt;td&gt;&lt;?php echo trim($attendance-&gt;date_data,'"&gt;');  ?&gt;&lt;/td&gt;
                                         &lt;td&gt;&lt;?php  trim($attendance-&gt;entry,'"&gt;');  ?&gt;  &lt;?php if($attendance-&gt;entry &gt;100)
                                          {
                                              echo "Logged In";
                                          }
                                          else
                                          {
                                              echo"Logged Out";
                                          }?&gt;
                                      &lt;/td&gt;
  
  
                                      &lt;/tr&gt;
  
                                  &lt;?php endforeach; ?&gt;
  
                                  &lt;?php else: ?&gt;
  
                                      &lt;tr&gt;
                                          &lt;td colspan="3"&gt;We could not find any Data.&lt;/td&gt;
                                      &lt;/tr&gt;
  
                                  &lt;?php endif; ?&gt; 
  
  
  
                                  &lt;/tbody&gt;
                              &lt;/table&gt;
                          &lt;/div&gt;
                          &lt;!-- /.table-responsive
  </code></pre>
  
  <p>Now a friend recommended this code to get first and last login/logout. But i failed to understand it:</p>
  
  <p>The Model Code:</p>
  
  <pre><code>function get_attendance_history($emp_id) {
  
      $hours = $this-&gt;time = date('Y-m-d H:i:s', strtotime('-1 month', time()));
      //desc will print the newest records &amp;&amp; asc the latest.
      $sql = "SELECT * FROM daily_data2 WHERE emp_id = '{$emp_id}' AND date_data &gt; '{$hours}' ORDER BY `date_data` desc /*LIMIT 1*/";
  
      $result = $this-&gt;db-&gt;query($sql); //run the query
      //print_r( $result-&gt;result_array() ); //debug the result
      //return $result-&gt;row()-&gt;emp_id; //for one record show..
      return $result-&gt;result_array();
      }
  </code></pre>
  
  <p>The Controller Code:</p>
  
  <pre><code>  public function login_details() 
         {
  
          $this-&gt;data['attendances'] = $this-&gt;attendance_m-&gt;don();
  
          $login_data =$this-&gt;data['attends']= $this-&gt;attendance_m-&gt;get_attendance_history('21');
  
          $this-&gt;data['subview'] = 'admin/attendance/view';
          $this-&gt;load-&gt;view('admin/_layout_main', $this-&gt;data);
        var_dump($login_data);
      }
  </code></pre>
  
  <p>I want to show something like this:</p>
  
  <p><a href="https://i.stack.imgur.com/vNFSz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vNFSz.png" alt="output"></a></p>