2013-04-27 4 views
0

사용자가 첫 번째 페이지에서 다른 옵션을 선택할 때 다른 데이터를 표시하려고 시도했습니다 ... 그리고 자바 스크립트를 사용하여 정렬 기능을 제공하려고 시도했지만 표시되는 데이터는 동일합니다. 사용자가 다른 옵션을 선택하더라도 ...선택 옵션을 사용하여 데이터베이스의 다른 데이터 표시

이 내 코드 내 메인 페이지

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> 

을위한이가 연결된 데이터베이스에서 데이터를 검색하려면 코드입니다 내 자바 스크립트에

012 3,516,

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

국가 별 코드는 orderby가 설정되어있는 경우에만 확인되므로 else 문을 치는 것이므로 설정하지 않으면 else를 수행 할 때마다 동일한 코드가 적용됩니다. 또한

,이 정리에 너무 많은 코드이지만, 설정되어 주문한 경우 난 당신이 아마 75 %로

  1. 확인하는 코드를 정리하기 위해 무엇을 할 수있는 당신을 위해 목록을 만들 수 있습니다 자신의 경우 정렬이 문
  2. 예를 들어 모든 옵션을 사용하여 쿼리 구성하는 형태로 선정 된 국가 찾을 경우 자체에 설정되어있는 경우 문

  3. 확인 :

    $ sql = "SELECT column1, column2 등 FROM auip_wipo_sample $ countryWhereStatement $ sort $ orederedby";

올바르게 논리를 할 경우, $ 국가 변수 럼 이름

에 의해 키워드 ORDER를 포함 할 국가 = '미국'는 $ 정렬 변수에 의해 ASC 또는 DESC 및 순서를 포함 할 키워드가 포함됩니다

공간을 절약하고 읽기 쉽고 더 동적으로 만들 수 있습니다.

또한 절대적으로 필요한 경우가 아니면 백틱을 사용하지 마십시오. 그것들은 MySQL의 정말로 오래된 버전 (필자는 정말로 오래되었다는 뜻)과 필드 나 테이블 이름이 MySQL 키워드와 동일 할 경우에 필요합니다. 그렇지 않으면 필요가 없습니다.

관련 문제