2013-04-27 55 views
0

我试图显示不同的数据在表中时,用户从第一页选择不同的选项...我试图给排序功能使用JavaScript ..但是,显示的数据是相同的即使用户选择不同的选项...显示不同的数据从数据库使用选择选项

这是我的代码为我的主网​​页

的test.html

<form action="index2.php" method="post"> 
    <table border="0"> 
    <tr> 
     <th colspan="3">test</th> 
    </tr> 
    <tr> 
     <td>Select Foreign Agent Country</td> 
     <td></td> 
     <td> 
     <select name="country"> 
     <option value="US">United States</option> 
     <option value="NZ">New Zealand</option> 
     <option value="JP">Japan</option> 
     </select> 
     </td> 
     </tr> 
     <td colspan="3"> 
     <input type="submit" name="formSubmit" value-"Submit"> 
     </td> 
    </table> 
    </form> 

,这是我的代码,以获取来自数据库,连接数据到我的javascript

index2.php

<script language="JavaScript" type="text/javascript" src="js/select-all.js"></script> 
</head> 
<body> 

<!--id class --> 
<div id="contentHolder"> 

    <?php 
    $connect = mysql_connect("localhost", "root", ""); 

    //connect to database 
    //select the database 
    mysql_select_db("fak_databases"); 
    //submit button 
    if($_POST['formSubmit'] == "Submit") 
    { 
     $country = $_POST['country']; 
    } 

    if(isset($_GET['orderby'])){ 
     $order = $_GET['orderby']; 
     $sort = $_GET['sort']; 

     if($country == 'US') { 
     // query to get all US records 
      if($order != 'wipo_applicant1_city' && $order != 'applicant1_addr1' && $order != 'wipo_applicant1_state')$order = "wipo_applicant1_city"; 
       if($sort != 'asc' && $sort != 'desc')$sort = "asc"; 
        $sql = "SELECT `wipo_applicant1_city`, `applicant1_addr1`, `wipo_applicant1_state`, `invention-title` FROM `auip_wipo_sample` WHERE applicant1_country='US' ORDER BY ".mysql_real_escape_string($order)." ".$sort; 

        //here we reverse the sort variable 
        if($sort == "asc"){ 
         $sort = "desc"; 
        } 
       else{ 
        $sort = "asc"; 
       } 

     } 
     else if($country == 'NZ') { 
     // query to get all US records 
     //$query = mysql_query("SELECT * FROM auip_wipo_sample WHERE applicant1_country='US'"); 
      if($order != 'wipo_applicant1_city' && $order != 'applicant1_addr1' && $order != 'wipo_applicant1_state')$order = "wipo_applicant1_city"; 
       if($sort != 'asc' && $sort != 'desc')$sort = "asc"; 
        $sql = "SELECT `wipo_applicant1_city`, `applicant1_addr1`, `wipo_applicant1_state`, `invention-title` FROM `auip_wipo_sample` WHERE applicant1_country='NZ' ORDER BY ".mysql_real_escape_string($order)." ".$sort; 

        //here we reverse the sort variable 
        if($sort == "asc"){ 
         $sort = "desc"; 
        } 
       else{ 
        $sort = "asc"; 
       } 

     } 
     else if($country == 'JP') { 
     // query to get all US records 
     //$query = mysql_query("SELECT * FROM auip_wipo_sample WHERE applicant1_country='US'"); 
      if($order != 'wipo_applicant1_city' && $order != 'applicant1_addr1' && $order != 'wipo_applicant1_state')$order = "wipo_applicant1_city"; 
       if($sort != 'asc' && $sort != 'desc')$sort = "asc"; 
        $sql = "SELECT `wipo_applicant1_city`, `applicant1_addr1`, `wipo_applicant1_state`, `invention-title` FROM `auip_wipo_sample` WHERE applicant1_country='JP' ORDER BY ".mysql_real_escape_string($order)." ".$sort; 

        //here we reverse the sort variable 
        if($sort == "asc"){ 
         $sort = "desc"; 
        } 
       else{ 
        $sort = "asc"; 
       } 

     } 
    }else { 
     $order = ""; 
     $sort = "asc"; 
     $sql = "SELECT `wipo_applicant1_city`, `applicant1_addr1`, `wipo_applicant1_state`, `invention-title` FROM `auip_wipo_sample`"; 
    } 
    $result = mysql_query($sql); 
    $num_rows = mysql_num_rows($result); 
    $row_counter = 0; 

    $icon = ""; 
    echo "<table border=\"1\" cellspacing=\"0\">\n"; 
    echo "<tr>\n"; 

    // first column 
    echo "<th>"; 
    $icon = ""; 
    if($order == "wipo_applicant1_city"){ 
     if($sort == "asc"){ 
      $icon = "<img src=\"images/up.png\" class=\"arrowSpace\"/>"; 
     } 
     if($sort == "desc"){ 
      $icon = "<img src=\"images/down.png\" class=\"arrowSpace\"/>"; 
     } 
    } 

    //print the result 
    echo "<a href='index2.php?orderby=wipo_applicant1_city&sort=".$sort."'>City</a>".$icon; 
    echo "</th>\n"; 


    // second column 
    echo "<th>"; 
    $icon = ""; 
    if($order == "applicant1_addr1"){ 
     if($sort == "asc"){ 
      $icon = "<img src=\"images/up.png\" class=\"arrowSpace\"/>"; 
     } 
     if($sort == "desc"){ 
      $icon = "<img src=\"images/down.png\" class=\"arrowSpace\"/>"; 
     } 
    } 
    echo "<a href='index2.php?orderby=applicant1_addr1&sort=".$sort."'>Address</a>".$icon; 
    echo "</th>\n"; 


    // third column 
    echo "<th>"; 
    $icon = ""; 
    if($order == "wipo_applicant1_state"){ 
     if($sort == "asc"){ 
      $icon = "<img src=\"images/up.png\" class=\"arrowSpace\"/>"; 
     } 
     if($sort == "desc"){ 
      $icon = "<img src=\"images/down.png\" class=\"arrowSpace\"/>"; 
     } 
    } 
    echo "<a href='index2.php?orderby=wipo_applicant1_state&sort=".$sort."'>State</a>".$icon; 
    echo "</th>\n"; 
    echo "</tr>"; 

    //fetch the result 
    while($row = mysql_fetch_array($result)){ 

     //give a different color for row 
     if($row_counter % 2){ 
      $row_color="bgcolor='#FFFFFF'"; 
     }else{ 
      $row_color="bgcolor='#F3F6F8'"; 
     } 
     echo "<tr class=\"TrColor\" ".$row_color.">"; 
     echo "<td>" . $row['wipo_applicant1_city'] . "</td>\n"; 
     echo "<td>" . $row['applicant1_addr1'] . "</td>\n"; 
     echo "<td>" . $row['wipo_applicant1_state'] . "</td>\n"; 
     echo "</tr>"; 
     $row_counter++; 
    } 
    echo "</table>"; 
    ?> 
</div> 

</body> 

显示-all.js

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 < d["auip_wipo_sample[]"].length; i++) { 
      d["auip_wipo_sample[]"][i].checked = blnVal; 
     } 
    } 
} 

有代码时执行没有错误,但结果总是相同

人知道什么是错的我的代码?

回答

0

它打你的else语句,因为你所有的国家特定的代码只有在orderby被设置时才被检查,所以如果没有设置,它会做你的其他的,每次都是相同的代码。

此外,还有太多的代码来清理,但我会让你的你可以做什么来清除该代码同比增长也许75%

  1. 检查清单,如果在设定下令自己的if语句
  2. 检查排序是在其自己的一套if语句
  3. 找出形式中选择哪个国家
  4. 让使用所有这些选项例如查询:

    $ sql =“SELECT column1,column2,etc FROM auip_wipo_sample $ countryWhereStatement $ sort $ orederedby”;

如果你做正确的逻辑,在$国家变量将包含WHERE国家=“美国”,在$排序的变量将会被包含ASC或DESC和秩序的关键字将包含关键字的ORDER BY列名

这可以做到节省空间,使事情更容易阅读,甚至更动态。

此外,尽量不要使用反引号,除非绝对必要。它们对于真正旧版本的MySQL(我的意思是旧的)是必要的,如果字段或表名与MySQL关键字相同。否则,你不应该需要它们。

相关问题