gpt4 book ai didi

php - 根据下拉列表和表格选择数据

转载 作者:行者123 更新时间:2023-11-29 17:54:32 24 4
gpt4 key购买 nike

我对此已经摸不着头脑有一段时间了,但希望有人能指导我:

我的数据库中有一个表(标准),它具有不同的外键(年级、科目、分支)。在用户方面,用户可以输入关键字,并可以根据年级(1-12)、学科(数学、英语、科学)或分支(取决于学科选择)进一步过滤。这种额外的过滤是我面临的问题。根据所做的选择从数据中选择数据的推荐方法是什么?

这是当他们没有选择过滤数据时我所得到的:

function generalResult($keyword, $connection){

$sql = "SELECT standards_eng.Standard FROM standards_eng WHERE standards_eng LIKE '%".$keyword."%' ";

$query = $connection->query($sql);

if ($query->num_rows > 0) {
while ($row = $query->fetch_assoc()) {
echo '<p>' .$row['Standard']. '</p>';
}
}
}

我能想到的唯一解决方案是 If/else 语句,但这太多了,无法计数。例如:

if(Grade!== "" AND SUBJECT == "" AND BRANCH =="") {
//Selects data where Standard is like keyword AND Grade_ID is equal to value selected

} Else if (Grade!== "" AND SUBJECT !== "" AND BRANCH == "") {
//Selects data where Standard is like keyword Grade_ID and Subject is equal to value selected

}Else if{
...
}Else if{...}

最佳答案

我不完全确定你想要什么,但如果我是正确的,你想要可选的过滤器,可以留空?

我要做的是创建一个过滤器,并在需要额外的过滤器时附加/连接该字符串。请参阅下面的示例。

$filter="WHERE standards_eng LIKE '%".keyword."%'";

if(Grade!==""){
$filter."AND Grade="value here";
}
if(SUBJECT!==""){
$filter."AND SUBJECT="value here";
}
if(BRANCH!==""){
$filter."AND BRACH="value here";
}



$sql = "SELECT standards_eng.Standard FROM standards_eng".$filter;

关于php - 根据下拉列表和表格选择数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48981509/

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