PHP:与多个函数相关的多个下拉选项
这更多的是一个 PHP 设计问题。基本上,我将有一个带有下拉菜单和提交按钮的表单。我将有无限数量的下拉选项;显然,不是无限的,但基本上会有一个带有很多选项的下拉菜单。这些“选项”是我想要运行的单独报告。
因此,用户从下拉菜单中选择,例如“未经批准的文章”,我想将其与我的functions.php 文件中的一个函数绑定,该函数为此运行SQL 并在表中很好地输出所有内容。
我的问题是,如何在我的主文档中没有一个巨大的 if/else 语句的情况下执行此操作,该语句基本上表示“如果提交按钮等于“未经批准的文章”,请执行此操作”,等等对于每个下拉选项。我需要一些动态的东西,而不需要一堆 if/else 语句。
我是不是想太多了?
This is more of a PHP design question. Basically, I'm going to have a form with a drop down menu and a submit button. I am going to have an infinite amount of drop down options; obviously, not an infinite, but there's basically going to be a drop down menu with ALOT of options. These "options" are individual reports that I want to run.
So, the user selects from the drop down menu, for example, "Articles without approval", which I want to tie to a function in my functions.php file that runs the SQL for this and outputs everything nicely in a table.
My problem is, how do I do this without have a huge if/else statement in my main document that that basically says "If the submit button is equal to "Articles without approval", do this", etc etc for every drop down option. I need something dynamic, without a bunch of if/else statements.
Am I overthinking this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将选项映射到函数,例如...
,然后您可以查找并运行该函数。
Have a mapping of options to functions, such as...
Which you can then look up and run the function.
除非您想将所有应用程序包装到可能更有利于此类事情的框架中,否则您可能会发现动态调用该函数更容易。
假设您有名为
articlesWithoutApproval()
和articlesWithApproval()
的函数。然后,您可以执行以下操作:然后,在 handler.php 中执行以下操作:
Unless you want to wrap all of your app into a framework that might be more conducive to this sort of thing, you'll probably find it easier to call the function dynamically.
Suppose you had functions called
articlesWithoutApproval()
andarticlesWithApproval()
. Then, you could do something like this:Then, in
handler.php
, do: