2013-04-27 76 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關鍵字相同。否則,你不應該需要它們。

相關問題