title: PHP logic problem
body: <p>I have a sql table <code>images</code> which has columns <code>id</code>,<code>imagePath</code>,<code>caption``count</code>.</p>
  
  <p>To view those images what i do is, I ask for the page number and query the iamge</p>
  
  <pre><code>$limit=$page-1;
  $sel=$mysqli-&gt;prepare("SELECT id,imagePath,caption,count from images".
                          " order by id desc limit ".$limit.",1");
  </code></pre>
  
  <p>This shows images correctly.</p>
  
  <p>But i have another function which shows 4 random thumbnail of that images out of the total images from the database.With those images i also give a href tag so that when a user clicks on that link he is taken to the page of that image.</p>
  
  <p>Now the problem is with those links.
  Suppose i have images like these in the database</p>
  
  <pre><code>id=1
  id=2
  id=3
  id=4
  id=5
  </code></pre>
  
  <p>To find the page number of the specific id what i do is</p>
  
  <pre><code>$newid=$idOfTheCurrentImage-($idOfTheLastImage-1);
  </code></pre>
  
  <p>Using this logic i get</p>
  
  <pre><code>id=1  page=5
  id=2  page=4
  id=3  page=3
  id=4  page=2
  id=5  page=1
  </code></pre>
  
  <p>This gives correct results if all the ids are in order. But if anyone of the id is deleted this logic fails.</p>
  
  <p>Is there any other solution? </p>