0

好的,伙计们,如果可以的话,我需要您再帮忙一次吗?

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

当每一个被选中时,它将它添加到一个查询字符串中,并将一个等号 (=) 附加到选择中,所以如果我选择大,那么查询将是 size = large 的地方。

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

如果它保持在 = 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);
4

1 回答 1

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

感谢您查看草莓 :)

于 2013-06-19T12:50:07.257 回答