title: Prevent Multiple Entry in Table
body: <p>I am confused and facing issue in getting output like below image
  Required Output</p>
  
  <p><a href="https://i.stack.imgur.com/EWEhc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/EWEhc.png" alt="Required Output"></a></p>
  
  <p>I have database table called number_status in which I am storing number online and offline history with timestamp. I required to get json response for display data in my android application like above image. I am able to get it like below function</p>
  
  <pre><code>  public function compareNumber($firstNumber, $secondNumber, $email, $date) {
          $response = array('code' =&gt; 0, 'error' =&gt; false);
          $endDate = date('Y-m-d', strtotime("+1 day", strtotime($date))) . " " . explode(" ", $date)[1];
  
          $stmt = $this-&gt;conn-&gt;prepare("SELECT id FROM user WHERE email = ?");
          $stmt-&gt;bind_param("s", $email);
          $stmt-&gt;execute();
          $result = $stmt-&gt;get_result();
          if ($result-&gt;num_rows) {
              $user =  $result-&gt;fetch_assoc();            
              $user_id = $user['id'];
              $stmt-&gt;close();
  
              $response["received"]   = $date;
              $response["created_at"] = $date;
              $response["end"]        = $endDate;
              $response["code"]       = 1;
  
              $arr = $this-&gt;helperNumber($firstNumber, $date, $endDate, $user_id);
              $arr1 = $this-&gt;helperNumber($secondNumber, $date, $endDate, $user_id);
  
              $response["logs"] = array_merge($arr, $arr1);
          }
  
          return $response;
      }
  
      public function helperNumber($numberToSearch, $date, $endDate, $user_id) {
          $stmt = $this-&gt;conn-&gt;prepare("SELECT number, number_status, status_time FROM number_status WHERE number = ? AND  user_id=? AND status_time &gt; ? AND status_time &lt; ? ORDER BY status_time DESC");
              $stmt-&gt;bind_param("iiss", $numberToSearch, $user_id, $date, $endDate);
              $stmt-&gt;execute();
              $result = $stmt-&gt;get_result();
  
              $number = array();
              if ($result-&gt;num_rows) {
                  $lastrow = null;
                  $i = 0;
                  while($row = $result-&gt;fetch_assoc()) {
                      if(!isset($number[$i]))
                          $number[$i] = array('number' =&gt; $row['number'], 'start_time' =&gt; false, 'end_time' =&gt; false);
  
                      if($lastrow == null){
                          // take offline as first entry
                          if($row['number_status'] == 0) {
                              $number[$i]['end_time'] = $row['status_time'];
                          }
                      } else {
                          // if two repeated entry for online/offline skip it
                          if($lastrow['number_status'] == 0 &amp;&amp; $row['number_status'] == 0) 
                              continue;
                          if($lastrow['number_status'] == 1 &amp;&amp; $row['number_status'] == 1)
                              continue;
  
                          if($row['number_status'] == 1){
                              $number[$i]['start_time'] = $row['status_time'];
                          }
                          else  {
                              $number[$i]['end_time'] = $row['status_time'];  
                          }
                          if($number[$i]['start_time'] &amp;&amp; $number[$i]['end_time'])
                              $i++;
  
                      }
  
                      $lastrow = $row;
  
                  }
  
              }
              $stmt-&gt;close();
              return $number;
      }
  </code></pre>
  
  <p>I am getting json response for above function like below</p>
  
  <pre><code>{"code":1,"error":false,"received":"2018-07-15 00:00:00","created_at":"2018-07-15 00:00:00","end":"2018-07-16 00:00:00","logs":[{"number":"919400000001","start_time":"2018-07-15 16:11:04","end_time":"2018-07-15 16:12:03"},{"number":"919400000001","start_time":"2018-07-15 10:35:47","end_time":"2018-07-15 10:37:34"},{"number":"919400000001","start_time":"2018-07-15 10:31:03","end_time":"2018-07-15 10:33:43"},{"number":"919400000001","start_time":"2018-07-15 10:27:28","end_time":"2018-07-15 10:27:46"},{"number":"919400000001","start_time":"2018-07-15 10:26:55","end_time":"2018-07-15 10:27:26"},{"number":"919400000001","start_time":"2018-07-15 10:25:38","end_time":"2018-07-15 10:25:50"},{"number":"919400000001","start_time":"2018-07-15 10:24:51","end_time":"2018-07-15 10:25:14"},{"number":"919400000000","start_time":"2018-07-15 10:55:18","end_time":"2018-07-15 16:11:04"},{"number":"919400000000","start_time":"2018-07-15 10:33:50","end_time":"2018-07-15 10:34:04"},{"number":"919400000000","start_time":"2018-07-15 10:27:20","end_time":"2018-07-15 10:27:38"},{"number":"919400000000","start_time":"2018-07-15 10:25:42","end_time":"2018-07-15 10:25:57"},{"number":"919400000000","start_time":"2018-07-15 10:24:57","end_time":"2018-07-15 10:25:22"}]}
  </code></pre>
  
  <p>But I want sort it by end time (offline). I do not have idea how I can do it. In my database table there separate entry for online(start_time) and offline(end_time). Let me know if someone can help me for get it.
  Thanks</p>