title: More effecient way of changing PHP results displayed
body: <p>I have a PHP script that displays a bunch of database results in a nice grid and paginates them.</p>
  
  <p>I'm trying to figure out how I can create a way to change the number of results displayed per page using a drop down system so I started to play with AJAX.</p>
  
  <p>I have a variable that determines the amount of items to be displayed so what I've done is I've created a few more pages that have the exact same PHP code, but with a different number for that variable. It does what its intended to do, but I can't help but feel I've gone about doing this in the most inefficient way imaginable because I had to create three additional pages with the differing variable.</p>
  
  <p>How can this be improved? </p>
  
  <p>This is the php code. I used to have it in the page itself but have move it to another file which the ajax looks at.</p>
  
  <pre><code>&lt;?php
  
  
                  //Add the file that connects to the database
                  include("C://wamp/www/site/config.php");    
                  //Selects which database to get the data from
                  mysql_select_db("products");
  
                  //Variables for pagination 
  
                  $per_page = 24;                 
                  // get the number of items that actually have Oven in their name
                  $sql = "SELECT SUM(rowcount)
                          FROM (
                          SELECT COUNT(1) AS rowcount 
                          FROM brand1
                          WHERE description LIKE '%oven%' 
                          UNION ALL 
                          SELECT COUNT(1) 
                          FROM brand2
                          WHERE description LIKE '%oven%'
                          ) AS counts";
                  $pages_query = mysql_query ($sql) ;
                  //Ceil rounds up to the nearest number so that we don't get pages with decimals on the end
                  $pages = ceil(mysql_result($pages_query, 0) / $per_page);
  
                  //append a page=1,2,3 at the end of the URL to indicate which page the user is on
  
                  //set : 1 so that if a person doesn't pick a page it automatically sets them on page 1.
                  $page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
  
                  $start = ($page -1) * $per_page;
  
                  //Selects which table to extract the data from
                  $i = 0;
                  $result = mysql_query("SELECT * FROM brand1 WHERE description LIKE '%oven%' UNION ALL SELECT * FROM brand2 WHERE description LIKE '%oven%' LIMIT $start, $per_page") ;
                  $dyn_table = '&lt;table border ="1" cellpadding ="10"&gt;';
  
                  //create a variable to store the data in
                  while($row = mysql_fetch_array($result))
                      {
                          //Variables to store vendors information, product number and image as retrieved from the database
                          $brand = $row["Brand"];
                          $model_number = $row["Model Number"];
                          $product_image = $row["Image"];
  
                          //Set up a loop that puts the information into a grid rather than a single line
                          if($i%4==0) //First number sets the column number
                              {
                                  // Starts to draw the table. Adds the vendor name, the product number then breaks line to draw the image underneath the titles
                                  $dyn_table .= '&lt;tr&gt;&lt;td&gt;' .  $brand . " " . $model_number . '&lt;br&gt;' . $product_image .'&lt;/td&gt;';
                              }   
                          else
                              {
                                  //This line does the same thing, but once the first if condition hits 4, it jumps to this line to contine drawing.
                                  $dyn_table .= '&lt;td&gt;' . $brand . " " . $model_number . '&lt;br&gt;' . $product_image . '&lt;/td&gt;';
                              }
                          //Simply adds 1
                          $i++;
                      }
                      //Adds the ending of the table
                      $dyn_table .= '&lt;/tr&gt;&lt;/table&gt;';
  
                      //Draws the table
                      echo $dyn_table;
  
                      //Set the pagination links at the bottom of the page
                      if ($pages &gt;=1 &amp;&amp; $page &lt;= $pages)
                          {
                              for($x=1; $x&lt;=$pages; $x++)
                                  {
                                      echo ($x == $page) ? '&lt;strong&gt;&lt;a href ="?page='.$x.'"&gt;'.$x.'&lt;/a&gt;&lt;/strong&gt; ' : '&lt;a href ="?page='.$x.'"&gt;'.$x.'&lt;/a&gt; ';
                                  }
                          }
              ?&gt;  
  </code></pre>
  
  <p>And the AJAX (Which I just grabbed off an example from W3</p>
  
  <pre><code>  &lt;script&gt;
  function loadXMLDoc()
  {
      var xmlhttp;
      if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
      else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 &amp;&amp; xmlhttp.status==200)
      {
      document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
      }
      }
      xmlhttp.open("GET","http://localhost/site/products/ovens/products.php",true);
      xmlhttp.send();
  }
  &lt;/script&gt;
  </code></pre>