动态PHP查询 - 过滤器选择

时间:2013-06-19 07:37:45

标签: php mysql dynamic filter

好的伙计我需要你的帮助再来一次请你好吗?

我有一个下拉选项列表作为一组过滤条件。

当选择每个时,它会将其添加到查询字符串并在选择中附加等号(=),因此如果我选择“大”,则查询将为size = large。

我有一个名为impacted的选项,但是当用户选择受影响时,我不希望查询追加=,我希望它成为影响LIKE'click'的地方,以便它将搜索受影响的数据库列中的任何位置

如果它仍然在= to,它将仅返回一个选项,其中只有一个受影响的系统与教程完全相对于选择存储。

 SELECT p.project_id 
      , p.project_name
      , p.department
      , p.size
      , p.programme
      , l.captured_by
      , l.stage
      , l.type
      , l.impacted
      , l.depts_inv
      , l.lesson_title
      , l.lesson_learned
      , l.lesson_added 
   FROM ll_project p
   JOIN ll_lessons l
     ON l.project_id = p.project_id 
    AND l.impacted = 'Click';

这是它当前如何提取代码但是如果有人对受影响的过滤器进行了选择,我希望查询不要在它之前添加=但是如果你知道我的意思那么'包含'或'喜欢'。

这是完整的代码。

<?php 
//Set all Variables pulled from POST of previous page
$pid = $_POST['project_id'] ;
$psize = $_POST['projectSize'] ;
$pdepts = $_POST['depts'] ;
$lstage = $_POST['stage'] ;
$ltype = $_POST['type'] ;
$impacted = $_POST['impacted'] ;
//Create Column Variable to hold Values in an array linked to the database columns
$columns = array('project_id'=>'ll_project.project_id','projectSize'=>'size','depts'=>'department','stage'=>'ll_lessons.stage','type'=>'ll_lessons.type','impacted'=>'ll_lessons.impacted');

$sqlString = null;
//Check all POSTED data is pulling through - Should be 6
//echo "Total Number Of Captured Post Variables is:";
//echo count($_POST);

$number = 0;
$queryStr = ""; 
$preStr = array(); 
//For Every POSTED Value Set the Name as Key and the Value against Each
foreach ($_POST as $key => $val ) {

if (!empty($_POST[$key])){
       if(!is_array($_POST[$key]))
       //Escape the VALUE by surrounding it with Quotes as it is a string the value would not be picked up on the query.
           $currentStr = $columns[$key]." = '".mysql_real_escape_string($val)."'"; 
       else
       $currentStr = $columns[$key]." IN (".implode(',',$_POST[$key]).")"; 
       $preStr[] = $currentStr; 
   }
 }

//set the Query String that i want to return from the Database and set the join on the BSKYB NUMBERS on both Tables
$queryStr = "SELECT ll_project.project_id, ll_project.project_name, ll_project.department, ll_project.size, ll_project.programme, ll_lessons.captured_by, ll_lessons.stage, ll_lessons.type, ll_lessons.impacted, ll_lessons.depts_inv, ll_lessons.lesson_title, ll_lessons.lesson_learned, ll_lessons.lesson_added FROM ll_project INNER JOIN ll_lessons ON ll_project.project_id = ll_lessons.project_id  WHERE ".implode(' AND ',$preStr);

1 个答案:

答案 0 :(得分:0)

答案是这样的!

if ($key == 'impacted') {
       $currentStr = $columns[$key] . " LIKE '%" . mysql_real_escape_string($val) . "%'";
     } else {
       $currentStr = $columns[$key] . " = '" . mysql_real_escape_string($val) . "'"; 
     }
       else
       $currentStr = $columns[$key]." IN (".implode(',',$_POST[$key]).")"; 
       $preStr[] = $currentStr; 
   }
}

感谢您查看草莓:)