PHP:与多个函数相关的多个下拉选项

发布于 2024-11-01 02:43:15 字数 345 浏览 0 评论 0原文

这更多的是一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

魔法少女 2024-11-08 02:43:15

将选项映射到函数,例如...

$reportsToFunctions = array(
   'without_approval' => 'withoutApproval',
   ...
);

,然后您可以查找并运行该函数。

Have a mapping of options to functions, such as...

$reportsToFunctions = array(
   'without_approval' => 'withoutApproval',
   ...
);

Which you can then look up and run the function.

独守阴晴ぅ圆缺 2024-11-08 02:43:15

除非您想将所有应用程序包装到可能更有利于此类事情的框架中,否则您可能会发现动态调用该函数更容易。

假设您有名为 articlesWithoutApproval()articlesWithApproval() 的函数。然后,您可以执行以下操作:

<form action="handler.php" method="post">
    <select name="reportName">
        ...
        <option value="articlesWithoutApproval">Articles without approval</option>
        <option value="articlesWithApproval">Articles with approval</option>
        ...
    </select>
</form>

然后,在 handler.php 中执行以下操作:

<?php
function articlesWithoutApproval(){ ... }
function articlesWithApproval(){ ... }

// clean variables, etc

if( isset( $_POST['submit'] ) && $_POST['submit'] ) ) {

    $func_name = $_POST['reportName']; // equal to "articlesWithoutApproval"
    echo $func_name(); // call the function dynamically
    // above evaluates to `echo articlesWithoutApproval();` etc
}
?>

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() and articlesWithApproval(). Then, you could do something like this:

<form action="handler.php" method="post">
    <select name="reportName">
        ...
        <option value="articlesWithoutApproval">Articles without approval</option>
        <option value="articlesWithApproval">Articles with approval</option>
        ...
    </select>
</form>

Then, in handler.php, do:

<?php
function articlesWithoutApproval(){ ... }
function articlesWithApproval(){ ... }

// clean variables, etc

if( isset( $_POST['submit'] ) && $_POST['submit'] ) ) {

    $func_name = $_POST['reportName']; // equal to "articlesWithoutApproval"
    echo $func_name(); // call the function dynamically
    // above evaluates to `echo articlesWithoutApproval();` etc
}
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文