2013-05-18 3 views
0

데이터베이스에서 특정 데이터를 정렬하려고했습니다. 첫 번째 페이지의 사용자 입력에 따라 특정 데이터를 표시했습니다. 나는 테이블을 정렬하려고 할 때 그러나, 나는 이것이 내가테이블의 특정 데이터 정렬

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 
에 오류가있어

<?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' && $order != 'applicant1_addr1')$order = "applicant1_addr1"; 
      if($sort != 'asc' && $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 "<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='showDB1.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='showDB1.php?orderby=applicant1_addr1&sort=".$sort."'>Address</a>".$icon; 
    echo "</th>\n"; 
    echo "</tr>"; 

//fetch the result 

while($row = mysql_fetch_array($result)) 
{ 
    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 "</tr>"; 
    $row_counter++; 
} 

Print "</table>"; 
?> 

테이블을 테이블에 데이터를 표시하고 정렬하는 내 코드입니다

변수 정의되지 않은 일부 오류가있어 내 코드를 분석 할 때

와 나는 내가

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' && $order != 'applicant1_addr1')$order = "applicant1_addr1"; 
      if($sort != 'asc' && $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'"; 

에 논리 오류를 가지고 생각할 때 I를하기 때문에 테이블을 정렬하려고하면 정렬 기능이 formubmit에 다시 액세스하려고합니다. 사용자가 옵션을 선택하면 양식 제출이 첫 페이지에서만 액세스됩니다.

누구든지 이것을 해결하는 방법을 알고 있습니까?

내 HTML 코드

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Data Mining</title> 
</head> 

<body> 
    <form action="showDB.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> 

</body> 
</html> 

여기 내 자바 스크립트 코드

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

$ _get [ 'sort']이 설정되어 있는지 확인하지 마십시오. – bestprogrammerintheworld

+0

@bestprogrammerintheworld 실제로 자바 스크립트를 사용하여 테이블을 정렬하고 있습니다 ... 자바 스크립트를 사용한다면 $ _get [ 'sort']을 사용할 필요가 없습니다. –

+0

실제 HTML 양식과 자바 스크립트 코드가 어떻게 보이나요? 처럼? – bestprogrammerintheworld

답변

0

은 변수에 대한 문제는 당신이 예를 들어, 확인해야한다는 것을 당신이 $ 국가를 초기화하는 것을 잊었다하고있다 $ _POST 글로벌 값 "formSubmit"이 사용 가능한 경우 isset() 또는 empty(). 여기

예제 코드 :이 당신을 도와줍니다

<?php 

print_r($_POST); 

$country = ''; 

if (isset($_POST['submitted']) && $_POST['submitted'] === 'submit') 
{ 
    $country = 'Pangea'; 
} 

print $country; 

if ($country == true) 
{ 
    print ' => country is true' . PHP_EOL; 
} 
else 
{ 
    print ' => country is false' . PHP_EOL; 
} 

?> 

<!doctype html> 
<html> 
    <head> 
    </head> 
    <body> 
     <form id="myForm" action="test.php" method="post"> 
      <input type="submit" name="submitted" value="submit" /> 
     </form> 
    </body> 
</html> 

희망.

+0

응답에 감사드립니다 ... PHP_EOL의 기능은 무엇입니까 ??? –

+0

"\ n"(
, html)의 PHP const입니다.이 솔루션을 사용하면 pls를 수락하는 데 도움이됩니다. 감사 :) –

0

코드를 살펴보면서 나는 왜 그들이 작동하지 않는지 설명하려고했습니다. 물론 그들은 더 이상 사용되지 않으므로 mysql_ * 함수를 사용하면 안됩니다. 귀하의 전화가 js 기능에 대한 부분을 이해할 수 없으므로 지금은 그 부분을 남겨 두었습니다.

도움이 되었기를 바랍니다.

<?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 
/* skip this part. Only relevant to check name of submit button when having more submit-buttons in same form 
if($_POST['formSubmit'] == "Submit") 
{ 
    $country = $_POST['country']; 
} 
*/ 

//query the database 
//if($country == TRUE) { 
if (isset($_REQUEST['country']) { //do like this instead (because country can arrive from link or from form 
    $country = $_REQUEST['country']; 

    $order = ""; 
    $sort = "asc"; 

    //if(isset($_GET['orderby'])){ change to: 
    if(isset($_GET['orderby']) && isset($_GET['sort'])) { 

     $order = $_GET['orderby']; 
     $sort = $_GET['sort']; //You're getting the value from $_GET['sort'] but you never check if it is set (like you do with orderby) 

     //limiting the possible values of order/sort variables 
     if($order != 'wipo_applicant1_city' && $order != 'applicant1_addr1')$order = "applicant1_addr1"; 
      if($sort != 'asc' && $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"; 
      } 
     }  
//} End of check isset($_GET['country']) should be move down to assure that $country is set 


    $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 "<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='showDB1.php?orderby=wipo_applicant1_city&sort=".$sort."'>City</a>".$icon; //orderby and sort is defined but not $_POST['country'] 
    echo "<a href='showDB1.php?country=".$country."&orderby=wipo_applicant1_city&sort=".$sort."'>City</a>".$icon; //change to this instead so country is passed 

    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='showDB1.php?orderby=applicant1_addr1&sort=".$sort."'>Address</a>".$icon; //orderby and sort is defined but not $_POST['country'] 
    echo "<a href='showDB1.php?country=".$country."&orderby=applicant1_addr1&sort=".$sort."'>Address</a>".$icon; //change to this instead so country is passed 
    echo "</th>\n"; 
    echo "</tr>"; 

//fetch the result 

while($row = mysql_fetch_array($result)) 
{ 
    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 "</tr>"; 
    $row_counter++; 
} 

Print "</table>"; 


} //End of check isset($_GET['country']) is moved to the end 
?> 
관련 문제