gpt4 book ai didi

php - 每个循环 PHP 的多输入和多列

转载 作者:行者123 更新时间:2023-11-30 21:32:18 26 4
gpt4 key购买 nike

目前正在通过foreach函数获取所有的get请求。查看名为 season 的列是否包含它们。下一步是再添加 1 列以检查是否有任何 get 请求与下一列值中的任何一个相似。第二列必须是 AND 而不是和 OR,这意味着如果第一列 season 包含任何获取请求并且第二列包含任何 GET 请求。

目前:

$array_name = array();
foreach ($_GET as $key => $value) {
$array_name[] = "'%" . escape_string($value) . "%'";
};

$string = implode(' OR season LIKE ', $array_name);

$tank = "SELECT * FROM shrubs2 WHERE season LIKE {$string}";
echo $tank;

第一次编辑:

      function searchByColumn($values, $columnName) {
$string = implode(" OR $columnName LIKE ", $values);
return "SELECT * FROM shrubs2 WHERE $columnName LIKE $string";
}

$array_name = array();
foreach ($_GET as $key => $value) {
$array_name[] = "'%".escape_string($value)."%'";
}

$colNames = array("season", "日照"); // can add here more column names
foreach($colNames as $colName) {
$str = searchByColumn($array_name, $colName);
}

echo $str;


///// creating the query with the variable $str

最佳答案

您可以定义函数来搜索每一列:

function searchByColumn($values, $columnName) {
$string = implode(" OR $columnName LIKE ", $values);
return "SELECT * FROM shrubs2 WHERE $columnName LIKE $string";
}

然后将其用作:

$array_name = array();
foreach ($_GET as $key => $value) {
$array_name[] = "'%".escape_string($value)."%'";
}

$colNames = array("season"); // can add here more column names
foreach($colNames as $colName) {
$str = searchByColumn($array_name, $colName);
echo $str; // or run it
}

关于php - 每个循环 PHP 的多输入和多列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55571782/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com