使我的表可以按四列排序

时间:2017-01-28 09:08:20

标签: php postgresql sorting

我有一个工作正常的用户日志表并按时间戳排序,但我还需要它可以按IP ADDRESS和USER排序,但无法使其正常工作。我希望用户能够选择一个ORDER BY列,整个表将按该列求助。我找到了一个使用库的解决方案,但我无法这样做,我需要一些简单而独立的东西。请协助。

<table border="1" cellspacing="0" cellpadding="0">
<tr bgcolor="#D4E9FC">
<td> 
Timestamp 
<a href="?OrderBy=user_log.stamp&SortBy=DESC">&#9660;</a>
<a href="?OrderBy=user_log.stamp&SortBy=ASC">&#9650;</a> 
</td> 
<td> 
User
<a href="?OrderBy=user_staff.user_name&SortBy=DESC">&#9660;</a>
<a href="?OrderBy=user_staff.user_name&SortBy=ASC">&#9650;</a>
</td>
<td> 
IP Address
<a href="?OrderBy=user_log.userip&SortBy=DESC">&#9660;</a> 
<a href="?OrderBy=user_log.userip&SortBy=ASC">&#9650;</a>
</td> 
<td> 
Transfer
<a href="?OrderBy=user_log.transfer_id&SortBy=DESC">&#9660;</a> 
<a href="?OrderBy=user_log.transfer_id&SortBy=ASC">&#9650;</a>
</td> 
<td> 
Action
</td> 
</tr>

    $orderBy = array('user_log.stamp&SortBy=DESC', 'user_staff.user_name&SortBy=DESC', 'user_log.userip&SortBy=DESC', 'user_log.transfer_id&SortBy=DESC');

    $order = 'user_log.stamp';
if (isset($_GET['orderBy']) && in_array($_GET['orderBy'], $orderBy)) {
$order = $_GET['orderBy']  . ' ' . $_GET['SortBy'];
}

 $query = "SELECT user_log.stamp, user_staff.user_name, user_log.userip, user_log.transfer_id, user_log.action
    FROM user_log
    INNER JOIN user_staff
    ON user_staff.user_id=user_log.user_id 
    ORDER BY $order
    LIMIT 200";

    $result = pg_query($query); 
    if (!$result) { 
        echo "Problem with query " . $query . "<br/>"; 
        echo pg_last_error(); 
        exit(); 
    } 

    while($myrow = pg_fetch_assoc($result)) { 
        printf ("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>", $myrow['stamp'], htmlspecialchars($myrow['user_name']), htmlspecialchars($myrow['userip']), htmlspecialchars($myrow['transfer_id']), htmlspecialchars($myrow['action']));
    } 
    ?>
    </table><br>

2 个答案:

答案 0 :(得分:1)

你可以像这样制作表格:

<table border="1" cellspacing="0" cellpadding="0">
<tr bgcolor="#D4E9FC">
    <td> 
    Timestamp 
    <a href="?OrderBy=user_log.stamp&SortBy=DESC">&darr;</a> 
    <a href="?OrderBy=user_log.stamp&SortBy=ASC">&uarr;</a>
    </td> 
    <td> 
    User
    <a href="?OrderBy=user_staff.user_name&SortBy=DESC">&darr;</a> 
    <a href="?OrderBy=user_staff.user_name&SortBy=ASC">&uarr;</a>
    </td>
    <td> 
    IP Address
    <a href="?OrderBy=user_log.userip&SortBy=DESC">&darr;</a> 
    <a href="?OrderBy=user_log.userip&SortBy=ASC">&uarr;</a>
    </td> 
    <td> 
    Transfer
    </td> 
    <td> 
    Action
    </td> 
</tr>
<table>

然后我在php你可以找到GET参数:

$orderBy = 'user_log.stamp';

if ($_SERVER['REQUEST_METHOD'] === 'GET') {
    $orderBy = isset($_GET['OrderBy']) ? $_GET['OrderBy'] : 'user_log.stamp';
    $orderBy = isset($_GET['SortBy']) ? $_GET['SortBy'] : 'DESC';

}
     $query = "SELECT user_log.stamp, user_staff.user_name, user_log.userip, user_log.transfer_id, user_log.action
            FROM user_log
            INNER JOIN user_staff
            ON user_staff.user_id=user_log.user_id 
            ORDER BY $orderBy 
            DESC LIMIT 200";

我正在使用你的逻辑 - 但是你在做什么根本就不是保存...更好的是使用PDO语句!

答案 1 :(得分:0)

我认为它应该有效:

$query = "SELECT user_log.stamp, user_staff.user_name, user_log.userip, user_log.transfer_id, user_log.action
        FROM user_log
        INNER JOIN user_staff
        ON user_staff.user_id=user_log.user_id 
        ORDER BY user_log.stamp DESC, user_log.userip DESC, user_staff.user_name ASC  
        LIMIT 200";