【问题标题】:add where clause to datatables filtering将 where 子句添加到数据表过滤
【发布时间】:2012-07-07 10:31:39
【问题描述】:

您好,我正在尝试在我的数据表服务器端处理文件中添加自定义 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 子句才能使其在被过滤时也起作用。

【问题讨论】:

    标签: php jquery mysql datatables


    【解决方案1】:

    删除第一行并将其添加到末尾(在iffor 之后):

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

    【讨论】:

    • 刚刚尝试过,它只是卡在处理过程中,当我尝试搜索时出现 500 错误
    • 当我执行 var_dump 时,我无法进行搜索,因为 json 格式对额外的 var_dump 信息有误
    • @arrowill12 抱歉,我知道我做错了什么,我的回答应该是.=。 (更新)
    【解决方案2】:

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

    $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 个位置需要代码

    【讨论】:

      【解决方案3】:

      试试这个

      /* 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']."'";
              }
      

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-07
        • 1970-01-01
        相关资源
        最近更新 更多