我想根据之前的下拉菜单查询一个下拉菜单的项目列表
如何根据另一个查询结果查询下拉列表
,以便我有一个函数 getMakes()
,它返回我放置在下拉列表中的车辆品牌列表,
我在函数中创建了一个新查询getModels()
获取每个车辆品牌的所有可能模型,我想制作一个 where 语句,仅获取所选车辆品牌的品牌
我想拥有该功能 getModels($make)
其中 $make是我们在where子句中想要的值,但是如何使where子句中的值动态化呢?
How do I query a drop down according to another queries result
so I have a function getMakes()
that returns a list of vehicle makes that i have placed in a dropdown,
I created a new query in the function getModels()
that gets all the possible models for every vehicle make, i want to make a where statement that only grabs the makes for the vehicle make that was selected
I was thinking to have the function getModels($make)
where $make is the value we want in the where clause, but how do i make the value in the where clause dynamic?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您希望根据第一个下拉列表中的选择填充第二个下拉列表,则需要使用 ajax 或 javascript。在这种情况下,您不能让 PHP 完成这项工作; PHP 将在页面加载时运行一次,仅此而已。
您可以拥有主控制器:
然后是返回模型的另一个控制器:
[假设您在 url 中传递了 make],
然后让 ajax 函数根据初始选择调用模型页面;你可以用 jQuery 来做到这一点,如下所示:
不确定我的所有细节是否正确,但这至少应该为你指明正确的方向。
You will need to use ajax or javascript if you want to have the second dropdown populate based on a selection from the first dropdown. You can't have PHP do the work in this case; PHP will run once when the page loads and that's it.
You can have your main controller:
then another controller that returns the models:
[assuming you pass the make in the url]
then have an ajax function call the models page based on the initial selection; you can do it with jQuery like so:
not sure I got all the details correct, but this should at least point you in the right direction.
您不能让
$sql
字符串使用某种字符串连接来生成 SQL 查询吗?应该进行一些检查来防止 SQL 注入攻击,因为从安全角度来看,这样做可能有点风险。如果您需要更多帮助,PHP 字符串连接 将是一个教程。
Couldn't you make the
$sql
string use some string concatenation to produce the SQL query? There should be some checking to prevent a SQL injection attack as doing this can be a little risky from a security perspective.PHP string concatenation would be a tutorial if you want some more help with that.