使用php中的按钮对数据进行排序


sorting data using button in php

我试图创建一个按钮,其功能是对数据进行降序或升序排序。然而,我不知道如何做到

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

有人知道如何做到这一点,或者知道一些可以作为参考的源代码吗???

这是我的代码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>

showDB.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>";
?>

更改此项:

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']))
<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";   
    }

?>