Loop to get mysql values with php codeigniter
<p>I'm stuck on the model side where I have to write a loop that select some certain values from mysql. The table looks like this:</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code>0      2016-11-06 08:36:19
7300   2016-11-06 08:43:11
19200  2016-11-06 08:43:12
35980  2016-11-06 08:43:17
45600  2016-11-06 08:43:21
45820  2016-11-06 08:43:27
45840  2016-11-06 08:43:28
45860  2016-11-06 08:43:29
26940  2016-11-06 08:44:34
19760  2016-11-06 08:44:36
3440   2016-11-06 08:44:38
0      2016-11-06 08:44:44
1400   2016-11-06 08:46:00
7120   2016-11-06 08:46:04
11240  2016-11-06 08:46:06
11220  2016-11-06 08:46:10
11240  2016-11-06 08:46:11
11220  2016-11-06 08:46:12
3940   2016-11-06 08:46:51
0      2016-11-06 08:46:56</code></pre>
</div>
</div>
</p>

<p>Basically this are entries generated from a weighting machine and inserted in my database works like this : </p>

<p>When something is on the weighing machine, the entries will start to update from 0 to the real weight of the object on the weight machine, but till they get to the real weight I get all the values going up. When the difference on 3-4 the entries is less than 50kg, we are at the real weight. Then the weight will drop to 0 again, and will start the same cicle when something is on it. From the above example, these are the entries I am interested in:</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code>45840  2016-11-06 08:43:28
11220  2016-11-06 08:46:10</code></pre>
</div>
</div>
</p>

<p>I was thinking to compare each entry with the following one, and when the difference is less than 50 kg for 3 entries, get one of the entries and continue like this for other ones, but I don't how exactly I can do this in PHP.</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code>$sql = "SELECT weight, added_date FROM balance_events WHERE added_date BETWEEN '".$from_date."' AND '".$to_date."'";
          $query = $this-&gt;db-&gt;query($sql);
          $result = $query-&gt;result();
          for weight in result:
          // TO DO the loop
          return $result;</code></pre>
</div>
</div>
</p>

<p>How do I make my for loop in order to get the desired output? Also is there a way to check the timestamp on the added_date in order constrain the time of the weight event (0 to 0) to be more than 5 minutes, and to ignore the ones that have the duration under 5min?</p>