gpt4 book ai didi

php - 使用 URL 中的变量来过滤数据

转载 作者:行者123 更新时间:2023-11-29 07:33:20 24 4
gpt4 key购买 nike

我在 URL 中使用 2 个变量(varDept 和 varType)来过滤数据。像training.php?varDept=optical&varType=morning 这样的东西。我的表格看起来包括类(class)、培训师、类型,而 contact_info 包括培训师、部门。

SELECT DISTINCT schedule.Session
FROM schedule LEFT JOIN contact_info ON schedule.Trainer=contact_info.Trainer
WHERE contact_info.Department LIKE varDept AND schedule.Session LIKE varType
ORDER BY schedule.Session

一切工作正常,但有时我有一些没有部门 (VarDept) 的记录,因此 URL 看起来像training.php?varDept=&varType=morning。我想显示可能没有 varDept 但仍然有 session 、培训师、类型的记录。目前它显示了training.php?varDept=&varType=morning 的所有记录。我的 varDept 目前的 $_GET['Dept'] 的默认值为 %。

最佳答案

因此,请检查 varDept 空白并在 WHERE 子句中忽略它。像这样的事情:

$dept = $_GET["varDept"]

$qry = "SELECT DISTINCT schedule.Session ";
$qry .= "FROM schedule LEFT JOIN contact_info ON ";
$qry .= "schedule.Trainer=contact_info.Trainer ";
$qry .= "WHERE schedule.Session LIKE varType ";
if( strlen($dept) )
$qry .= " AND contact_info.Department LIKE $dept";

$qry .= "ORDER BY schedule.Session";

关于php - 使用 URL 中的变量来过滤数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31816957/

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