将where子句添加到datatables过滤

时间:2012-07-07 00:01:10

标签: php jquery mysql datatables

嗨我正在尝试在我的数据表服务器端处理文件中添加自定义WHERE子句。我已设法在页面加载时使其工作但在过滤后忽略where子句并返回所有结果。

代码:

$sWhere = "WHERE customers_id= ".$customersId;
if ( isset($_GET['sSearch']) && $_GET['sSearch'] != "" )
{
    $sWhere = "WHERE (";
    for ( $i=0 ; $i<count($aColumns) ; $i++ )
    {
        if ( isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true" )
        {
            $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子句,以便在beign过滤时也能正常工作。

3 个答案:

答案 0 :(得分:2)

删除第一行并在最后添加(iffor之后):

$sWhere .= ($sWhere ? ' AND ' : ' WHERE ') . 'customers_id = ' . $customersId;

答案 1 :(得分:0)

最终在这里解决它是解决方案:

$sWhere = "WHERE customers_id=".$customersId;
if ( isset($_GET['sSearch']) && $_GET['sSearch'] != "" )
{
    $sWhere = "WHERE (";
    for ( $i=0 ; $i<count($aColumns) ; $i++ )
    {
        if ( isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true" )
        {
            $sWhere .= "`".$aColumns[$i]."` LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%' OR ";
        }
    }
    $sWhere = substr_replace( $sWhere, "", -3 );
    $sWhere .= ') AND customers_id='.$customersId;
    //var_dump($sWhere);
}

仅在这2个地点需要代码

答案 2 :(得分:0)

试试这个

/* LINE 86
    $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 .= " AND customer_id='".$_GET['customer_id']."'";
                $sWhere .= ')';
        }else{
                $sWhere = "WHERE customer_id='".$_GET['customer_id']."'";
        }

我希望这能解决你的目的。