【问题标题】:How do I only display results that do not have NULL values from a specified column?如何仅显示指定列中没有 NULL 值的结果?
【发布时间】:2016-12-16 03:14:38
【问题描述】:

我正在使用DataTables jQuery plugin 以图表格式显示来自 mySQL 数据库的结果。我的服务器端配置如下:

// DB table to use
$table = 'Estimates';

// Table's primary key
$primaryKey = 'CreatedDate';

// 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' => 'Client', 'dt' => 0 ),
array( 'db' => 'EstimateNumber',  'dt' => 1 ),
array( 'db' => 'Status',   'dt' => 2 ),
array( 'db' => 'CurrentEstimateTotal',     'dt' => 3 ),
array( 'db' => 'CreatedDate',     'dt' => 4 )
);

// SQL server connection information
$sql_details = array(
'user' => 'root',
'pass' => 'root',
'db'   => 'tm-charts',
'host' => 'localhost'
);


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* If you just want to use the basic configuration for DataTables with PHP
* server-side, there is no need to edit below this line.
*/

require( 'ssp.class.php' );

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

这个处理主要是使用DataTables提供的简单的服务端处理模板which you can view here.

我的问题是:如何编辑此模板以仅显示 CreatedDate 列中没有 NULL 值的结果?

感谢您的帮助!

【问题讨论】:

  • 如果我们知道ssp 类做了什么,我们可以告诉您构造一个从数据库中排除空值的查询?
  • 如果您有办法编辑查询,以便过滤结果然后找到它
  • @adeneo ssp.class.php 文件可以在 GitHub 上找到:github.com/DataTables/DataTables/blob/master/examples/…
  • 那看看怎么用filter ( $request, $columns, &$bindings )的方法说WHERE column IS NOT NULL

标签: php jquery mysql datatable datatables


【解决方案1】:

丽兹巴纳赫: 您需要包含 WHERE 条件,如下所示:

require( 'ssp.class.php' );
$where = "CreatedDate IS NOT NULL";
echo json_encode(
    SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns,$where )
);

希望这会有所帮助:)

【讨论】:

  • 感谢您的回复!我在这段代码中添加了,似乎 NULL 值仍然显示在前端。除了包含此代码,我还需要做些什么吗?
  • 您使用的是什么版本的数据表? CreatedDate 包含空值还是 NULL 值?
  • 我是通过 Bower 安装的,所以我相信它是最新的。我尝试将WHERE 查询更改为$where = "CreatedDate != ''",以防这些值为空而不是NULL,并且仍然在表中看到空值。我做错了吗?
  • 使用以下函数,而不是 SSP:simple... SSP::complex ( $request, $conn, $table, $primaryKey, $columns, $whereResult=null, $whereAll=null )
  • 当我这样做时,我收到以下错误消息:DataTables 警告:表 id=aging_projects - Ajax 错误。有关此错误的更多信息,请参阅datatables.net/tn/7。控制台说:Failed to load resource: the server responded with a status of 500 (Internal Server Error)
【解决方案2】:

我搞定了!

答案如下:

echo json_encode(
SSP::complex( $_GET, $sql_details, $table, $primaryKey, $columns, null, "CreatedDate IS NOT NULL" )
);

【讨论】:

    猜你喜欢
    • 2014-11-20
    • 2022-10-14
    • 2018-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多