title: sorting particular data from table php
body: <p>I tried to sort the particular data from my database. I have done to show the particular data according to the user input from the first page. However when I tried to sort the table, I got some errors which are undefined variable</p>
  
  <p>this is my code to show the data in table and sort the table</p>
  
  <pre><code>&lt;?php
  //connect to server
  $connect = mysql_connect("localhost", "root", "") or die('no database');
  
  //connect to database
  //select the database
  mysql_select_db("fak_databases");
  //submit button
  if($_POST['formSubmit'] == "Submit")
  {
      $country = $_POST['country'];
  }
  
  //query the database
  if($country == TRUE) {
  
      $order = "";
      $sort = "asc"; 
  
      if(isset($_GET['orderby'])){
          $order = $_GET['orderby']; 
          $sort = $_GET['sort'];  
  
          //limiting the possible values of order/sort variables
          if($order != 'wipo_applicant1_city' &amp;&amp; $order != 'applicant1_addr1')$order = "applicant1_addr1";
              if($sort != 'asc' &amp;&amp; $sort != 'desc')$sort = "asc";
                  $sql = "SELECT wipo_applicant1_city, applicant1_addr1 FROM auip_wipo_sample WHERE applicant1_country='$country' ORDER BY ".mysql_real_escape_string($order)." ".$sort; 
  
                  //here we reverse the sort variable
                  if($sort == "asc"){
                      $sort = "desc";
                  }
              else{
                  $sort = "asc";
              }
          }       
  } 
      $sql = "SELECT wipo_applicant1_city, applicant1_addr1 FROM auip_wipo_sample WHERE applicant1_country='$country'";
      $result = mysql_query($sql) or die('failed to run');
      $num_rows = mysql_num_rows($result);
      $row_counter = 0; 
  
      $icon = "";
      echo "&lt;table  border=\"1\" cellspacing=\"0\"&gt;\n";
      echo "&lt;tr&gt;\n"; 
  
      // first column
      echo "&lt;th&gt;";
      $icon = "";
      if($order == "wipo_applicant1_city"){
          if($sort == "asc"){
              $icon = "&lt;img src=\"images/up.png\" class=\"arrowSpace\"/&gt;";
          }
          if($sort == "desc"){
              $icon = "&lt;img src=\"images/down.png\" class=\"arrowSpace\"/&gt;";
          }
      }
  
      //print the result
      echo "&lt;a href='showDB1.php?orderby=wipo_applicant1_city&amp;sort=".$sort."'&gt;City&lt;/a&gt;".$icon;
      echo "&lt;/th&gt;\n";
  
  
      // second column
      echo "&lt;th&gt;";
      $icon = "";
      if($order == "applicant1_addr1"){
          if($sort == "asc"){
              $icon = "&lt;img src=\"images/up.png\" class=\"arrowSpace\"/&gt;";
          }
          if($sort == "desc"){
              $icon = "&lt;img src=\"images/down.png\" class=\"arrowSpace\"/&gt;";
          }
      }
      echo "&lt;a href='showDB1.php?orderby=applicant1_addr1&amp;sort=".$sort."'&gt;Address&lt;/a&gt;".$icon;
      echo "&lt;/th&gt;\n";
      echo "&lt;/tr&gt;";
  
  //fetch the result
  
  while($row = mysql_fetch_array($result))
  {
      if($row_counter % 2){
              $row_color="bgcolor='#FFFFFF'";
          }else{
              $row_color="bgcolor='#F3F6F8'";
          }
      echo "&lt;tr class=\"TrColor\" ".$row_color."&gt;";
      echo "&lt;td&gt;" . $row['wipo_applicant1_city'] . "&lt;/td&gt;\n";
      echo "&lt;td&gt;" . $row['applicant1_addr1'] . "&lt;/td&gt;\n";
      echo "&lt;/tr&gt;";
      $row_counter++;
  }
  
  Print "&lt;/table&gt;";
  ?&gt;
  </code></pre>
  
  <p>I got errors on</p>
  
  <pre><code> Undefined index: formSubmit in C:\xampp\htdocs\fak_ict1999\sorting\showDB1.php
  
   Undefined variable: country in C:\xampp\htdocs\fak_ict1999\sorting\showDB1.php
  
   Undefined variable: country in C:\xampp\htdocs\fak_ict1999\sorting\showDB1.php 
  
   Undefined variable: order in C:\xampp\htdocs\fak_ict1999\sorting\showDB1.php
  
   Undefined variable: sort in C:\xampp\htdocs\fak_ict1999\sorting\showDB1.php 
  </code></pre>
  
  <p>and when I analyze my code I think I got a logic error on the</p>
  
  <pre><code> if($_POST['formSubmit'] == "Submit")
  {
      $country = $_POST['country'];
  }
  
  //query the database
  if($country == TRUE) {
  
      $order = "";
      $sort = "asc"; 
  
      if(isset($_GET['orderby'])){
          $order = $_GET['orderby']; 
          $sort = $_GET['sort'];  
  
          //limiting the possible values of order/sort variables
          if($order != 'wipo_applicant1_city' &amp;&amp; $order != 'applicant1_addr1')$order = "applicant1_addr1";
              if($sort != 'asc' &amp;&amp; $sort != 'desc')$sort = "asc";
                  $sql = "SELECT wipo_applicant1_city, applicant1_addr1 FROM auip_wipo_sample WHERE applicant1_country='$country' ORDER BY ".mysql_real_escape_string($order)." ".$sort; 
  
                  //here we reverse the sort variable
                  if($sort == "asc"){
                      $sort = "desc";
                  }
              else{
                  $sort = "asc";
              }
          }       
  } 
      $sql = "SELECT wipo_applicant1_city, applicant1_addr1 FROM auip_wipo_sample WHERE applicant1_country='$country'";
  </code></pre>
  
  <p>because when I tried to to sort the table, the sorting function is trying to access the formsubmit again which is the the form submit is only accessed on the first page when the user choose their option.</p>
  
  <p>is anyone know how to solve this?</p>
  
  <p>my html code</p>
  
  <pre><code> &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
  &lt;title&gt;Data Mining&lt;/title&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
      &lt;form action="showDB.php" method="post"&gt;
      &lt;table border="0"&gt;
      &lt;tr&gt;
          &lt;th colspan="3"&gt;test&lt;/th&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;Select Foreign Agent Country&lt;/td&gt;
          &lt;td&gt;&lt;/td&gt;
          &lt;td&gt;
          &lt;select name="country"&gt;
          &lt;option value="US"&gt;United States&lt;/option&gt;
          &lt;option value="NZ"&gt;New Zealand&lt;/option&gt;
          &lt;option value="JP"&gt;Japan&lt;/option&gt;
          &lt;/select&gt; 
          &lt;/td&gt;
        &lt;/tr&gt;
          &lt;td colspan="3"&gt;
          &lt;input type="submit" name="formSubmit" value="Submit"&gt;
          &lt;/td&gt;
      &lt;/table&gt;
      &lt;/form&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>here is my javascript code</p>
  
  <pre><code>function SelectAll(btn) {
      var blnVal = false;
      if (btn.value == "Select All") {
          btn.value = "Unselect All";
          blnVal = true;
      }else {
          btn.value = "Select All";
          blnVal = false;
      }
      var d = document.forms["auip_wipo_sample"];
      if(d["auip_wipo_sample[]"] == null)
      {}
      else if (d["auip_wipo_sample[]"].length == null) {
          d["auip_wipo_sample[]"].checked = blnVal;
      }
      else {
          for (var i = 0; i &lt; d["auip_wipo_sample[]"].length; i++) {
              d["auip_wipo_sample[]"][i].checked = blnVal;
          }
      }
  }
  </code></pre>