【问题标题】:sorting data using button in php使用 php 中的按钮对数据进行排序
【发布时间】:2013-04-12 15:09:50
【问题描述】:

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

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

任何人都知道如何做或一些可以作为参考的源代码???

这是我的代码 测试.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>";
?>

【问题讨论】:

    标签: php html sorting button


    【解决方案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']))
    

    【讨论】:

      【解决方案2】:
      <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";   
          }
      
      
      ?>
      

      【讨论】:

      • 谢谢人..它正在工作..但在我的情况下,我想在我的下一页添加按钮,按钮将执行升序和降序...可以在 php 中执行吗? ??或者我应该使用 javascript,因为我做了一些研究,说 javascript...
      • 您可以使用其中任何一种进行排序。大多数人会创建一个 Javascript 按钮,该按钮将调用 PHP 并通过 Ajax 返回更新后的排序以替换其中的任何内容。
      • 我需要查看输出示例和用于提供另一个示例的表。
      猜你喜欢
      • 2014-07-16
      • 1970-01-01
      • 2013-09-22
      • 1970-01-01
      • 2013-04-19
      • 1970-01-01
      • 1970-01-01
      • 2013-09-10
      • 1970-01-01
      相关资源
      最近更新 更多