php - PHP创建的排序表,不能使用查询排序

标签 php mysql sorting

我有一个想要排序的列。让我先解释一下代码的作用。

正如你所看到的,有来自三个不同表的三个 select 语句(表名在代码中)。第一个查询从表中选择数据,从而将 id 列中检索到的数据分配给 $device_id

第二个查询根据$device_id的值从另一个表中选择数据(仅一个值)。这将返回多个板 ID(多个值)。如果返回行,则会创建一个表。

在此表中,执行第三个查询来检查第三个表 ($table_tester_info) 中的每个 board_id。如果board_id存在,那么就会显示它。示例:如果有 40 个 board_id,而这 40 个中仅返回 20 行,则这些行将显示在表中。

现在问题来了。我想根据$table_tester_info(第三个表)中的列对表进行排序但是,我只能在第二个表中使用ORDER BY询问。这不是问题,但我想根据 $table_tester_info 中的列进行排序,而不是 $table_tester_config(第二个表)。

我也在使用 jQuery,但我不想使用任何插件来对 HTML 中的 DOM 元素进行排序,除非没有其他方法(但我确信有很多方法,我只是不知道)。我尝试对分配给它的 assoc 数组(?) $tester_name 或者更确切地说 $final_row['tester_name'] 进行排序。

当前,当我在第三个查询中添加 ORDER BY 时,我的表未排序(我知道这是错误的)。有什么帮助吗?

<?php

// Define database parameters
DEFINE ('DB_USER' ,'iqwdewqe');
DEFINE ('DB_PASSWORD', 'iqadeaqwe');
DEFINE ('DB_HOST', 'qadewe');
DEFINE ('DB_NAME', 'Tqweqwe');

// Connect to database
@mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die ('Could not connect' 
                 .'to Database: '. mysql_error());
@mysql_select_db (DB_NAME) OR die ('Could not select the Database: '. mysql_error()); 

function searchDevice($device_name, $tester_type)
{
    $table_device = "TBL_DEVICE_LIST";
    $table_tester_config = "TBL_TESTER_CONFIG";
    $table_tester_info = "TBL_TESTER_INFO";

    $query_string = "SELECT * FROM $table_device WHERE device = '$device_name'
                                AND tester_type = '$tester_type'";
    $device_result = @mysql_query($query_string) or die (mysql_error());
    $device_row = mysql_num_rows($device_result);

    if($device_row)
    {
        $device_row = mysql_fetch_array($device_result);
        $device_id = $device_row['id'];
       // echo "Device ID: $device_id <br>";

        $query_string = "SELECT * FROM $table_tester_config WHERE device_id = '$device_id'";
        $config_result = @mysql_query($query_string) or die(mysql_error());
        $config_row = mysql_num_rows($config_result);

        if($config_row)
        {


            while($config_row = mysql_fetch_assoc($config_result))
            {
                $board_id = $config_row['board_id'];
                $oboard = $config_row['configuration'];
                 //echo "Board ID: $board_id <br>";

                //Query to check if board_id exists, 
                if exists then display value in while loop
                $query_string = "SELECT * FROM $table_tester_info 
                WHERE board_id = '$board_id' ORDER BY tester_name";
                $final_result = @mysql_query($query_string) or die(mysql_error());
                $final_row = mysql_num_rows($final_result);

                    if($final_row)
                    {
                        echo "<table border='1'>";
                        echo "<tr>
                        <th>Board ID</th>
                        <th>Tester Name</th>
                        <th>Board Name</th>
                        <th>Configuration</th>
                        <th>Log Created</th>
                        <th>New</th>
                        </tr>";
                        while($final_row = mysql_fetch_assoc($final_result))
                        {
                            $board_id = $final_row['board_id'];
                            $tester_name = $final_row['tester_name'];
                            $board_name = $final_row['board_name'];
                            $config = $final_row['config'];
                            $log_created = $final_row['log_created'];

                            echo "<tr>
                                        <td>$board_id</td>
                                        <td>$tester_name</td>
                                        <td>$board_name</td>
                                        <td>$config</td>
                                        <td>$log_created</td>
                                        <td>$oboard</td>
                                     </tr>";
                        }
                    }
            }
            echo "</table>";
        }
    }
    else
    {
        echo "No Device Name: $device_name with Tester Type: $tester_type.";
    }
}

?>

<?php

$action = rtrim($_REQUEST['action']);

if($action=="search")
{
    $device_name = rtrim($_REQUEST['device_name']);
    $tester_type = rtrim($_REQUEST['tester_type']);

    echo searchDevice($device_name, $tester_type);
}

?>

最佳答案

通过联接组合查询 2 和 3。通过使用内部联接,如果联接成功,您只会获得一行。

即:

select tti.*
from $table_tester_config ttc
inner join $table_tester_info tti
    on tti.board_id = ttc.board_id
where td.device = '$device_name' and td.tester_type = '$tester_type'
order by tti.tester_name

然后循环这些结果。

关于php - PHP创建的排序表,不能使用查询排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30928596/

相关文章:

php - 如何检索 Doctrine2 实体的自定义列定义?

php - 同时使用 foreach 和 for work 制作表格

php - API 调用驻留在存储库模式 laravel 应用程序中的什么位置?

php - Mysql:与数据库的连接太多

php - 工厂方法和私有(private)变量

mysql - SQL COUNT 零问题

php mysql 服务器消失了

r - arrange() 把大写字母放在第一位

javascript - 按字符串值排序并打印成 HTML

c# - 为什么 List<T>.Sort 方法重新排序等于 IComparable<T> 元素?