php - 动态 PHP 查询 - 过滤器选择

标签 php mysql dynamic filter

好的,伙计们,如果可以的话,我再次需要你们的帮助?

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

当选择每个选项时,它会将其添加到查询字符串中,并向所选内容附加一个等于 (=),因此,如果我选择“大”,则查询将是“大小 = 大”。

我有一个名为“impacted”的选择,但是当用户选择“impacted”时,我不希望查询附加 = 到,我希望它成为受影响的位置,例如“单击”,以便它将搜索受影响的数据库列中的任何位置.

如果它保持在= to,则只会带回一个选择,其中只有一个受影响的系统是根据类(class)存储的,完全= 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);

最佳答案

答案是这样!

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; 
   }
}

感谢您查看草莓:)

关于php - 动态 PHP 查询 - 过滤器选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17185466/

相关文章:

PHP 函数 trim() 不去除字符串中间的空格

javascript - 如何区分在html表单中单击了哪个按钮并基于该按钮通过ajax传递不同的值?

php - 具有多行和删除的表

php - 使用 zend db select join 时选择表列

dynamic - 如何在另一个 SVG 文件中居中和缩放 SVG 文件

c# - 使用 Specflow.Assist .Dynamic 访问具有特殊字符的属性

css - 加载表动态丢失样式规则

php - 解析错误 : syntax error, 意外 ';' 在 C :\xampp\htdocs\webdev\auto-parts-db\submit_processor. php 第 6 行

php - 将具有单个键和字符串值的数组拆分为具有多个键和值的数组

php - MySQL Left Join - 多次给出相同的结果