【问题标题】:DataTables | How to do your own select?数据表 |如何进行自己的选择?
【发布时间】:2014-07-29 12:13:05
【问题描述】:

您好,我使用的是 tablesorter-plugin DataTables。 我将它与服务器端处理和 ajax 管道一起使用。

这是我实际的服务器端脚本:

                <?php
            // Datenbank-Tabelle, die verwendet wird
            $table = 'loginlogs';

            // Der Primary key, der Tabelle
            $primaryKey = 'id';

            // Das "datetime"-Format aus der Datenbank extrahieren, nur das Datum (in das Deutsche-Format)
            function getonlydate($datetime){
                $exploded = explode("-", $datetime);
                $explodemeagain = explode(" ", $exploded[2]);
                $mergeme = $explodemeagain[0].".".$exploded[1].".".$exploded[0];
                return $mergeme;
            }

            // Das "datetime"-Format aus der Datenbank extrahieren, nur die Uhrzeit
            function getonlytime($datetime){
                $exploded = explode("-", $datetime);
                $explodemeagain = explode(" ", $exploded[2]);
                $mergeme = $explodemeagain[1];
                return $mergeme;
            }

            // Array of database columns which should be read and sent back to DataTables.
            // The `db` parameter represents the column name in the database, while the `dt`
            // parameter represents the DataTables column identifier. In this case simple indexes.
            $columns = array(
                array( 'db' => 'ip', 'dt' => 0 ),
                array(
                    'db'        => 'status',
                    'dt'        => 1,
                    'formatter' => function( $d, $row ) {
                        if($d == 1){
                            return "Erfolgreich";
                        }else{
                            return "Fehlgeschlagen";
                        }
                    }
                ),
                array(
                    'db'        => 'stayloggedin',
                    'dt'        => 2,
                    'formatter' => function( $d, $row ) {
                        if($d == 1){
                            return "Ja";
                        }else{
                            return "Nein";
                        }
                    }
                ),
                array(
                    'db'        => 'date',
                    'dt'        => 3,
                    'formatter' => function( $d, $row ) {
                        return getonlydate($d);
                    }
                ),
                array(
                    'db'        => 'date',
                    'dt'        => 4,
                    'formatter' => function( $d, $row ) {
                        return getonlytime($d);
                    }
                )
            );

            // SQL server connection information
            require('../../phpfuncs/connection.php');
            $sql_details = array(
                'user' => $user,
                'pass' => $pw,
                'db'   => $db,
                'host' => $host
            );

            require('ssp.class.php');

            echo json_encode(
                SSP::simple( $_GET, $sql_details, $table,  $primaryKey, $columns )  
            );
            ?>

现在我的问题是如何进行特定选择? 一个特定的选择,例如:

"SELECT * FROM ".$table." WHERE userid=16"

我已经在他们的网站上搜索了一些文档,但我只能找到有关过滤等的文档。客户端的东西,但没有关于特定的服务器端的可能性。

也许有人也使用 datatables 和 tablesorter,可以帮我举个例子吗?

【问题讨论】:

  • 您使用的SSP 类只是一个示例界面,用于向您展示它是如何完成的。如果你想要更多的灵活性,你需要自己动手。

标签: php jquery mysql sql datatables


【解决方案1】:

您可以使用另一种方法SSP::complex。来自代码中的cmets:

此方法与simple 的区别在于,您 可以对 SQL 查询应用额外的where 条件。这些可以 采用以下两种形式之一:

  • '结果条件' ($whereResult) - 这适用于结果集,但不适用于 整体寻呼信息查询——即不影响号码 用户看到他们可以访问的记录。这应该是 当您想要应用用户发送的过滤条件时使用。
  • 'All condition' ($whereAll) - 这适用于所有查询,并且 减少用户可以访问的记录数。这应该是 在您不希望用户访问的情况下使用 特定记录(例如,通过登录 ID 进行限制)。

函数接受以下参数:

SSP::complex ($request, $conn, $table, $primaryKey, $columns, $whereResult=null, $whereAll=null)

因此,为了应用带有WHERE 条件的 SQL 查询,您需要将代码更改如下:

echo json_encode(
    SSP::complex( $_GET, $sql_details, $table,  $primaryKey, $columns, null, "userid=16" )  
);

【讨论】:

    猜你喜欢
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 2012-03-27
    • 2017-11-19
    • 1970-01-01
    • 2013-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多