Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用选择选项显示来自数据库的不同数据_Php_Javascript_Html_Database_Sorting - Fatal编程技术网

Php 使用选择选项显示来自数据库的不同数据

Php 使用选择选项显示来自数据库的不同数据,php,javascript,html,database,sorting,Php,Javascript,Html,Database,Sorting,当用户从第一页选择不同的选项时,我尝试在表中显示不同的数据…我尝试使用javascript提供排序功能…但是,即使用户选择了不同的选项,显示的数据也是相同的 这是我主页的代码 test.html <form action="index2.php" method="post"> <table border="0"> <tr> <th colspan="3">test</th> </tr>

当用户从第一页选择不同的选项时,我尝试在表中显示不同的数据…我尝试使用javascript提供排序功能…但是,即使用户选择了不同的选项,显示的数据也是相同的

这是我主页的代码

test.html

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

测试
选择国外代理国家/地区
美国
新西兰
日本
这是我从连接到javascript的数据库中检索数据的代码

index2.php

 <script language="JavaScript" type="text/javascript" src="js/select-all.js"></script>
</head>
<body>

<!--id class -->
<div id="contentHolder">

    <?php
    $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'];
    }

    if(isset($_GET['orderby'])){
        $order = $_GET['orderby']; 
        $sort = $_GET['sort'];

        if($country == 'US') {  
        // query to get all US records
            if($order != 'wipo_applicant1_city' && $order != 'applicant1_addr1' && $order != 'wipo_applicant1_state')$order = "wipo_applicant1_city";
                if($sort != 'asc' && $sort != 'desc')$sort = "asc";
                    $sql = "SELECT `wipo_applicant1_city`, `applicant1_addr1`, `wipo_applicant1_state`, `invention-title` FROM `auip_wipo_sample` WHERE applicant1_country='US' ORDER BY ".mysql_real_escape_string($order)." ".$sort; 

                    //here we reverse the sort variable
                    if($sort == "asc"){
                        $sort = "desc";
                    }
                else{
                    $sort = "asc";
                }

        }
        else if($country == 'NZ') {  
        // query to get all US records  
        //$query = mysql_query("SELECT * FROM auip_wipo_sample WHERE applicant1_country='US'");   
            if($order != 'wipo_applicant1_city' && $order != 'applicant1_addr1' && $order != 'wipo_applicant1_state')$order = "wipo_applicant1_city";
                if($sort != 'asc' && $sort != 'desc')$sort = "asc";
                    $sql = "SELECT `wipo_applicant1_city`, `applicant1_addr1`, `wipo_applicant1_state`, `invention-title` FROM `auip_wipo_sample` WHERE applicant1_country='NZ' ORDER BY ".mysql_real_escape_string($order)." ".$sort; 

                    //here we reverse the sort variable
                    if($sort == "asc"){
                        $sort = "desc";
                    }
                else{
                    $sort = "asc";
                }

        }
        else if($country == 'JP') {  
        // query to get all US records  
        //$query = mysql_query("SELECT * FROM auip_wipo_sample WHERE applicant1_country='US'");   
            if($order != 'wipo_applicant1_city' && $order != 'applicant1_addr1' && $order != 'wipo_applicant1_state')$order = "wipo_applicant1_city";
                if($sort != 'asc' && $sort != 'desc')$sort = "asc";
                    $sql = "SELECT `wipo_applicant1_city`, `applicant1_addr1`, `wipo_applicant1_state`, `invention-title` FROM `auip_wipo_sample` WHERE applicant1_country='JP' ORDER BY ".mysql_real_escape_string($order)." ".$sort; 

                    //here we reverse the sort variable
                    if($sort == "asc"){
                        $sort = "desc";
                    }
                else{
                    $sort = "asc";
                }

        }
    }else {
        $order = "";
        $sort = "asc"; 
        $sql = "SELECT `wipo_applicant1_city`, `applicant1_addr1`, `wipo_applicant1_state`, `invention-title` FROM `auip_wipo_sample`";
    }
    $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='index2.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='index2.php?orderby=applicant1_addr1&sort=".$sort."'>Address</a>".$icon;
    echo "</th>\n";


    // third column
    echo "<th>";
    $icon = "";
    if($order == "wipo_applicant1_state"){
        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='index2.php?orderby=wipo_applicant1_state&sort=".$sort."'>State</a>".$icon;
    echo "</th>\n";
    echo "</tr>";

    //fetch the result
    while($row = mysql_fetch_array($result)){

        //give a different color for row
        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 "<td>" . $row['wipo_applicant1_state'] . "</td>\n";
        echo "</tr>";
        $row_counter++;
    }
    echo "</table>";
    ?>
</div>

</body>


它会命中else语句,因为只有在设置了orderby的情况下,才会检查所有特定于国家/地区的代码,因此如果未设置,它将执行else,每次都是相同的代码

此外,有太多的代码需要清理,但我会为您列出一个清单,列出您可以做些什么来清理这些代码,大约75%

  • 检查ordered by是否在其自己的if语句中设置
  • 检查sort是否在其自己的if语句中设置
  • 找出表格中选择的国家/地区
  • 使用所有这些选项进行查询,例如:

    $sql=“从auip_wipo_示例$countryWhereStatement$sort$OrederBy中选择第1列、第2列等”

  • 如果逻辑正确,$country变量将包含关键字,其中country='US',$sort变量将包含ASC或DESC,order by将包含关键字order by columnname

    这样做可以节省空间,使内容更易于阅读,甚至更具动态性

    此外,除非绝对必要,否则不要使用反勾号。如果字段或表名与MySQL关键字相同,那么在非常旧的MySQL版本(我指的是非常旧的版本)上它们是必需的。否则,你就不需要它们了

    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;
            }
        }
    }