Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 按发布的数组筛选数据?_Php_Mysql_Sql_Database_Arrays - Fatal编程技术网

Php 按发布的数组筛选数据?

Php 按发布的数组筛选数据?,php,mysql,sql,database,arrays,Php,Mysql,Sql,Database,Arrays,我有一个包含如下数据的表: articles. id | author | title | content | type 1 | author1, author2 | thetitle1 | text1 | typeA 2 | author1 | thetitle2 | text2 | typeB 3 | author2

我有一个包含如下数据的表:

articles. id   | author                | title     | content  | type
             1 | author1, author2      | thetitle1 | text1    | typeA
             2 | author1               | thetitle2 | text2    | typeB
             3 | author2               | thetitle3 | text3    | typeA
if(isset($_POST['authors'])){ //empty, is_array and etc.
  $authors = $_POST['authors']; // [ author1, author2 ]
  $subC = array();
  $subC [] = " ( author IN ('".implode("','",$authors)."') ) ";
  foreach ($authors as $a){
      $subC [] = " ( author LIKE %$a% ) " ;
  }

  $subC = ' ( ' . implode (' OR ' , $subC) . ' ) ';

  $conditions[] = $subC;
}
Posted array是用于以下内容的筛选器:

$conditions = array();
$where = '';

if(isset($_POST['authors'])){ //empty, is_array and etc.
  $authors = $_POST['authors']; // [ author1, author2 ]
  $conditions[] = "author IN ('".implode("','",$authors)."')";
}
if(isset($_POST['types'])){
  $types = $_POST['types']; // [ typeA, typeB ]
  $conditions[] = "type IN ('".implode("','",$types)."')";
}

if(!empty($conditions)){
  $where = ' WHERE '.implode(' AND ', $conditions);
}

$sql = "SELECT * FROM articles".$where;
看起来一切正常,但是字段
author
可以包含几个作者,用逗号分隔,因此('author1')中的过滤器
author将无法工作。如何选择涉及作者1的所有文章(在本例中是第一条和第二条记录)?

试试这个。()


我认为您需要更改数据库结构。通过字符串搜索很慢(ish),现在可能可以使用,但当数据集增加时,这将成为一种阻力

我认为这样会更好:

author
--------
id  name  
1   author1
2   author2

books:
--------
id  title  
1   Some Book  
2   Some Other Book  

author_book:
--------
id  author_id  book_id
1     1         1
2     1         2
3     2         2
在我的示例中,author1编写了第1和第2本书,author2编写了第2本书 另一本是自传的:第1本是作者1写的,第2本是作者1和2写的


从长远来看,它更灵活。正确的数据库结构非常重要,我同意@Martijn,但如果您无法更改数据库,可以尝试以下方法:

articles. id   | author                | title     | content  | type
             1 | author1, author2      | thetitle1 | text1    | typeA
             2 | author1               | thetitle2 | text2    | typeB
             3 | author2               | thetitle3 | text3    | typeA
if(isset($_POST['authors'])){ //empty, is_array and etc.
  $authors = $_POST['authors']; // [ author1, author2 ]
  $subC = array();
  $subC [] = " ( author IN ('".implode("','",$authors)."') ) ";
  foreach ($authors as $a){
      $subC [] = " ( author LIKE %$a% ) " ;
  }

  $subC = ' ( ' . implode (' OR ' , $subC) . ' ) ';

  $conditions[] = $subC;
}

这远非十全十美,但应该能奏效

您需要规范化您的结构保存逗号分隔的值不是解决问题的好方法您可以使用
FIND\u IN\u SET

SELECT * FROM articles WHERE type IN (...) OR FIND_IN_SET("author1", author)
另外,您也可以使用LIKE来匹配所需的结果,但这不是一个好主意

SELECT * FROM articles WHERE type IN (...) OR  author LIKE '%author1%';

以下是参考资料

要么规范化数据库,使文章作者成为一个单独的表,要么使用MySQL函数。。。。然后阅读SQL注入和转义。。。更好的是,切换到使用MySQLi或PDOI编写的语句。我无法编辑太多数据库,之后我将不得不编辑很多内容。您的模式设计很糟糕,您会后悔在工作开始时没有对其进行规范化。这个项目会拖下去,以后可能会失败。这不是我的内疚,我有我所拥有的。