title: Pagination in HTML code embeded in PHP
body: <p>I want to add pagination to the script below which searches the database and outputs the images in a folder into rows and columns. But I want the output to be in pages, not all on the same page. How can I do it?</p>
  
  <pre><code>&lt;?php
      echo '&lt;table border="0" cellpadding="15" cellspacing="15"&gt;';
      $getImages = mysql_query("SELECT * FROM yaamembers");
      if(!$getImages)
          die("Cannot execute query. " . mysql_error());
      $countRows = mysql_num_rows($getImages);
      $i = 0;
      if ($countRows &gt; 0)
      {
          while ($row_rsYaamembers = mysql_fetch_assoc($getImages))
          {
              if ($i % 3 == 0) echo ($i &gt; 0? '&lt;/tr&gt;' : '') . '&lt;tr&gt;';
                  echo '&lt;td valign="top"&gt;&lt;a target="_blank" href="yimagelarge.php?yaaid=' . $row_rsYaamembers['yaaID'] . '"&gt;&lt;img src="/home/youngatart/yaamembers/'.$row_rsYaamembers['photo'].'" border="0" width="120" /&gt;&lt;/td&gt;';
              if ($i == $countRows - 1)
                  echo '&lt;/tr&gt;';
              $i++;
          }
      }
      echo '&lt;/table&gt;';
  ?&gt;
  </code></pre>
  
  <p>I have modified the code and I get three rows of three columns of images and the last row on the page filled with one image. This is the same for all the pages generated by the pagination links. Its supposed to fill up evenly before going to the next page. What could i be doing wrong?</p>
  
  <p><em><strong>Modified code below:</em></strong></p>
  
  <pre><code>&lt;?Php
      $getImages = mysql_query("SELECT * FROM yaamembers");
      if(!$getImages)
          die("Cannot execute query. " . mysql_error());
  
      $output = "";
  
      $getImages = get_images($start,$limit);
      if ($getImages &amp;&amp; mysql_num_rows($getImages) &gt; 0)
      {
          /* Pagination section2 */
          $getAllImages = get_images();
          $total_items = mysql_num_rows($getAllImages);
          $paginate = paginate($targetpage,$total_items,$limit,$pagenum);
          $paginate = trim($paginate);
          /* Pagination section2 End */
  
          echo '&lt;table border="0" cellpadding="15" cellspacing="15"&gt;';
          $countRows = mysql_num_rows($getImages);
          $i = 0;
          if ($countRows &gt; 0)
          {
              while ($row_rsYaamembers = mysql_fetch_assoc($getImages))
              {
                  if ($i % 3 == 0) echo ($i &gt; 0) . '&lt;tr&gt;';
                      echo '&lt;td valign="top"&gt;&lt;a target="_blank" href="yimagelarge.php?yaaid=' . $row_rsYaamembers['yaaID'] . '"&gt;&lt;img src="http://localhost/youngatart/yaamembers/'.$row_rsYaamembers['photo'].'" border="0" width="120" /&gt;&lt;/a&gt;&lt;div id="text2"&gt;&lt;div class="clear_4"&gt;&lt;/div&gt;&lt;div align="center" id="text2"&gt;&lt;a target="_blank" href="yimagelarge.php?yaaid=' . $row_rsYaamembers['yaaID'] . '"&gt;&lt;div align="center" id="text2"&gt; '.$row_rsYaamembers['school'].'&lt;/a&gt;&lt;/div&gt;&lt;div class="clear_4"&gt;&lt;/div&gt;&lt;div align="center" id="text2"&gt;&lt;a target="_blank" href="yimagelarge.php?yaaid=' . $row_rsYaamembers['yaaID'] . '"&gt; '.$row_rsYaamembers['year'].'&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;/td&gt;';
                  if ($i == $countRows - 1)
                      echo '&lt;/tr&gt;';
                  $i++;
              }
          }
          echo '&lt;/table&gt;';
  
          $output .= $paginate;
      }
      echo $output;
  ?&gt;
  </code></pre>