0

我试图对从数据库中检索的一些数据进行排序

首先,用户将从选择选项菜单中选择他们想要显示的国家/地区的数据

之后,下一页将使用 sort asc/desc 函数显示表格中的特定数据

这是我的代码

第一页 test.html

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

这是我在用户选择他们想要显示的国家/地区的数据后显示数据的代码 showDB1.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 == TRUE) {
    $order = "";
    $sort = "asc"; 
    $sql = "SELECT wipo_applicant1_city, applicant1_addr1 FROM auip_wipo_sample";
    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";
            }
        }
    // query to get all US records   
} 
    $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='index.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='index.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>";

我的问题是当用户选择选择选项的任何国家时显示所有数据。

预期结果应该是只需要显示特定数据

任何人都可以解决这个问题?

谢谢

4

1 回答 1

0

通过查看您的代码,问题可能是您的输入按钮中有破折号而不是等号

<input type="submit" name="formSubmit" value-"Submit">

应该

value="Submit"

那么我会看下面的 if 语句

if(isset($_GET['orderby'])){

由于在您最初提交表单时,您发送的是 post 语句,因此不会调用您的 sql 语句,因为它包含在该 if 语句中。

将以下内容移到该语句之外

$order = "";
$sort = "asc"; 
$sql = "SELECT wipo_applicant1_city, applicant1_addr1 FROM auip_wipo_sample";

当然,添加这个以供您选择的国家/地区显示

 WHERE applicant1_country='$country'
于 2013-04-29T16:20:16.610 回答