title: PHP for-loop with MySQL rows depending on the MySQL query result
body: <p>I can't figure out if there's a way to do something like this:</p>
  
  <p>1 - Get user input (any signed/positive integer)</p>
  
  <p>2 - Iterate through each number (e.g. user input a positive integer like 10, so loop goes through 10 iterations)</p>
  
  <p>3 - PHP code loops through MySQL table in Database. MySQL table looks like this:</p>
  
  <pre><code>+----+------+------+---------+
  | ID | ITEM | USER | ACTIONS |
  +----+------+------+---------+
  |  1 | 1234 |    5 |       0 |
  |  2 | 1234 |    3 |       0 |
  |  3 | 1234 |    1 |       0 |
  |  4 | 1234 |    7 |       0 |
  +----+------+------+---------+
  </code></pre>
  
  <p>4 - PHP code should loop through each of those 4 <code>ACTIONS</code> rows to find the lowest number, and start updating the <code>ACTIONS</code> field until there is a total number of 10 'actions' in the table. So after the user had input a value of 10, this is how the above table would look, if we start from the lowest number:</p>
  
  <pre><code>+----+------+------+---------+
  | ID | ITEM | USER | ACTIONS |
  +----+------+------+---------+
  |  1 | 1234 |    5 |       3 |
  |  2 | 1234 |    3 |       3 |
  |  3 | 1234 |    1 |       2 |
  |  4 | 1234 |    7 |       2 |
  +----+------+------+---------+
  </code></pre>
  
  <p>5 - If another user inputs 10, this is how the table would look: (note that the starting point for this user would be the 3rd row i.e. the integer 2, since its the first occurrence of the smallest number):</p>
  
  <pre><code>+----+------+------+---------+
  | ID | ITEM | USER | ACTIONS |
  +----+------+------+---------+
  |  1 | 1234 |    5 |       5 |
  |  2 | 1234 |    3 |       5 |
  |  3 | 1234 |    1 |       5 |
  |  4 | 1234 |    7 |       5 |
  +----+------+------+---------+
  </code></pre>
  
  <p>Please advise how this can be done, I know sufficient PHP and MySQL code so you can suggest advanced syntax, but I can't figure <em>how</em> it should be done. I don't know where to start. Besides iterating through the MySQL rows and using a for loop to iterate through each number the user input, how should I go about getting the expected result?</p>