0

我试图创建一个按钮,其功能是对数据进行降序或升序排序。但是,我不知道该怎么做

我在互联网上做了一些研究,但没有一个给出答案。

有谁知道怎么做或一些可以作为参考的源代码???

这是我的代码 test.html

<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>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>
        <input type="submit" name="formSubmit" value-"Submit">
        </td>
    </table>
    </form>

</body>
</html>

显示数据库.php

<?php
//connect to server
$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'];
}

//query the database
if($country == 'US') {  
    // query to get all US records  
    $query = mysql_query("SELECT * FROM auip_wipo_sample WHERE applicant1_country='US'");  
}  
elseif($country == 'NZ') {  
    // query to get all AUD records  
    $query = mysql_query("SELECT * FROM auip_wipo_sample WHERE applicant1_country='NZ'"); 
}elseif($country == 'JP') {  
    // query to get all AUD records  
    $query = mysql_query("SELECT * FROM auip_wipo_sample WHERE applicant1_country='JP'");  
} else {  
    // query to get all records  
    $query = mysql_query("SELECT * FROM auip_wipo_sample"); 
}  
//fetch the result
Print "<table border cellpadding=3>"; 
//ascending descending button
Print "<tr><th colspan='2'><input type='submit' name='asc_sort' value-'Asc'></input></th></tr>";

while($row = mysql_fetch_array($query))
{

    Print "<tr>";
    Print "<td>".$row['invention_title'] . "</td>"; 
    Print "<td>".$row['invention-title'] . " </td></tr>"; 
}
    //sorting the data, I got from internet but doesn't work
    if(isset($_POST['asc_sort']) && !empty($_POST['asc_sort']) && $_POST['asc_sort']==1)
    {
         $query = "SELECT * FROM auip_wipo_sample ORDER BY invention_title ASC";

    }else{

        $query = "SELECT * FROM auip_wipo_sample ORDER BY invention_title DESC";
    }

Print "</table>";
?>
4

2 回答 2

1

改变这个:

Print "<tr><th colspan='2'><input type='submit' name='asc_sort' value-'Asc'></input></th></tr>";

Print "<tr><th colspan='2'><input type='submit' name='asc_sort' value='Asc'></input></th></tr>";

和这个

if(isset($_POST['asc_sort']) && !empty($_POST['asc_sort']) && $_POST['asc_sort']==1)

if(isset($_POST['asc_sort']) && !empty($_POST['asc_sort']))
于 2013-04-12T15:16:01.577 回答
0
<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>
        <select name="country">
        <option value="US">United States</option>
        <option value="NZ">New Zealand</option>
        <option value="JP">Japan</option>
        </select> 
        </td>
        <td><input type="checkbox" name="asc" value="1" /> Ascending?</td>
      </tr>
     <tr> 
        <td colspan="3">
        <input type="submit" name="formSubmit" value-"Submit">
        </td>
       </tr>
    </table>
    </form>

</body>
</html>    

一些事情:

  • 您在中间提取了一条查询语句,此时您仍在检索前一条语句。这需要将数据拉入多个数组然后进行处理。
  • 您应该为此使用 pdo 或 mysqli 之类的东西,但这是您当前方法的示例。
  • 此外,您在 POST 上使用 isset ...如果 POST 中有任何内容,它将按设置返回。
  • 引用未设置的 POST 变量将导致错误,因此我正在检查以确保已设置国家/地区,然后我为开关提供了 NULL 答案。

示例代码:

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

    //connect to database
    //select the database
    mysql_select_db("fak_databases");

    //submit button
    if(!empty($_POST['formSubmit'])&&($_POST['formSubmit']=="Submit")&&(!empty($_POST['country']))){
        $country = $_POST['country'];
    } else {
        $country = '';  
    }

    if(!empty($_POST['asc']))
        {
             $append = " ORDER BY invention_title ASC";

        }else{

            $append = " ORDER BY invention_title DESC";
        }

    switch($country){
    case 'US':
    $query = "SELECT * FROM auip_wipo_sample WHERE applicant1_country='US'$append";
    break;
    case 'NZ':
    $query = "SELECT * FROM auip_wipo_sample WHERE applicant1_country='NZ'$append";
    break;
    case 'JP':
    $query = "SELECT * FROM auip_wipo_sample WHERE applicant1_country='JP'$append";
    break;
    default:
    //all records
    $query = "SELECT * FROM auip_wipo_sample$append";

    }

    //query the database
    if($result = mysql_query($query)){
    //fetch the result
    print "<table border cellpadding=3>"; 

    while($row = mysql_fetch_array($result))
    {

        print "<tr>";
        print "<td>".$row['invention_title'] . "</td>"; 
        print "<td>".$row['invention-title'] . " </td></tr>"; 
    }
        //sorting the data, I got from internet but doesn't work


    print "</table>";
    } else {
                    //For Testing
        echo "Query Failed:<br />$query";   
    }


?>
于 2013-04-12T15:24:44.830 回答