gpt4 book ai didi

php - DataTables 1.9.4 - 限制结果

转载 作者:行者123 更新时间:2023-11-30 01:15:18 24 4
gpt4 key购买 nike

我正在使用 DataTables-1.9.4 进行服务器端处理,一切都运行良好,但我的表正在返回表中的所有内容!总共有 3,147 个条目,并且每天都在增长......

示例:www.hunterpdx.com/metro_new_copy/view-reports-test.php

有没有办法限制返回显示与特定特定相关的数据 用户:哪里公司 = $_SESSION['company']?我确信这是可以做到的,但我已经花了几天时间在这上面,但一无所获......

我正在使用基本的初始化代码(甚至保持表 ID 相同):

$(document).ready(function() {
$('#example').dataTable( {
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "../server_side/scripts/server_processing.php"
} );
} );

我在 server_processing.php 文件中更改的唯一内容是 aColumns 数组和数据库连接信息:

<?php
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Easy set variables
*/

/* Array of database columns which should be read and sent back to DataTables. Use a space where
* you want to insert a non-database field (for example a counter or static image)
*/
$aColumns = array( 'company', 'bldg', 'report', 'freq', 'report_date', 'file_path' );

/* Indexed column (used for fast and accurate table cardinality) */
$sIndexColumn = "report_id";

/* DB table to use */
$sTable = "uploads";

/* Database connection information */
$gaSql['user'] = "root";
$gaSql['password'] = "";
$gaSql['db'] = "members";
$gaSql['server'] = "localhost";

我假设它必须对 server_processing.php 的这一部分做一些事情:

/*
* Filtering
* NOTE this does not match the built-in DataTables filtering which does it
* word by word on any field. It's possible to do here, but concerned about efficiency
* on very large tables, and MySQL's regex functionality is very limited
*/
$sWhere = "";
if ( isset($_GET['sSearch']) && $_GET['sSearch'] != "" )
{
$sWhere = "WHERE (";
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
$sWhere .= "`".$aColumns[$i]."` LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%' OR ";
}
$sWhere = substr_replace( $sWhere, "", -3 );
$sWhere .= ')';
}

/* Individual column filtering */
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
if ( isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true" && $_GET['sSearch_'.$i] != '' )
{
if ( $sWhere == "" )
{
$sWhere = "WHERE ";
}
else
{
$sWhere .= " AND ";
}
$sWhere .= "`".$aColumns[$i]."` LIKE '%".mysql_real_escape_string($_GET['sSearch_'.$i])."%' ";
}
}

此处的目标是确保用户仅看到与其公司相关的数据(即使使用内置过滤搜索时也是如此):

WHERE company = '$_SESSION['company']' 

网站的上线实际上取决于这件事的完成,所以我非常需要帮助!这可以做到吗?又如何?

最佳答案

感谢 @Maximus2012 引导我完成这一过程!答案很简单:

调用 server_processing.php 页面顶部的 session_start();!

为了过滤特定 session ,我在 session 开始下方设置了一个特定于用户的变量:

$userCompany = $_SESSION['company'];

然后通过更改第一个来调用代码过滤部分中的变量:

$sWhere = "";

$sWhere = "WHERE company = '".$userCompany."'";

最后,为了确保搜索过滤器不会绕过初始过滤器,我更改了

$sWhere .= ')';

$sWhere .= ") AND company = '".$userCompany."'";

关于php - DataTables 1.9.4 - 限制结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19121921/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com