douna5529 2015-05-18 07:11
浏览 36
已采纳

通过函数调用PHP编辑

I am wrapping functions in php pages in the functions.php and to access these pages, i put an action and a value in the url. I also have a function_calls.php which requests the action and its value and passes it to a switch case which calls the appropriate function. The problem i have is that i case i want to edit a field . I dont have an appropriate case(for the switch) which i want to look something like admin.php?action=edit?id=some_id.

function_calls.php

 <?php
    require_once "functions.php";


    if (isset($_REQUEST['action'])) {
    $option = $_REQUEST['action'];

    echo $option;

    switch ($option) {
        case "complaints":
            complaint();
            break;
        case "users":
            users();
            break;
        case "officers":
            officers();
            break;
        case "register_officer":
            register_officer();
            break;
        case "register_student":
            register_student();
            break;
        case "add_event":
            add_event();
            break;

        default:
            complaint();
            break;
    }
} else {
    echo '<div class="jumbotron">
  <h1>Welcome To The Administration Panel </h1>
  <p>You can manage accounts and view the complaints here.</p>

</div>';
}

functions.php

<?php

include_once "include/db_connect.php";

function register_student()
{
    ?>

    <section>
        <div class="row">
            <div class="col-md-7 col-md-offset-1">
                <form class="" method="post" action="">
                    <div class="row">
                        <div class="col-md-6 ">
                            <div class="input-group margin-bottom-20">
                                <span class="input-group-addon"><i class="fa fa-user"></i></span>
                                <input type="text" placeholder="Firstname" name="firstname" required=""
                                       class="form-control">
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="input-group margin-bottom-20">
                                <span class="input-group-addon"><i class="fa fa-user"></i></span>
                                <input type="text" placeholder="Lastname" name="lasttname" required=""
                                       class="form-control">
                            </div>
                        </div>
                    </div>
                    <div class="input-group margin-bottom-20">
                        <span class="input-group-addon"><i class="fa fa-envelope"></i></span>
                        <input type="text" placeholder="Email" name="email" required="" class="form-control">
                    </div>

                    <div class="input-group margin-bottom-20">
                        <span class="input-group-addon"><i class="fa fa-user"></i></span>
                        <input type="text" placeholder="Registration Number" name="reg_no" required=""
                               class="form-control">
                    </div>
                    <div class="input-group margin-bottom-20">
                        <span class="input-group-addon"><i class="fa fa-phone"></i></span>
                        <input type="text" placeholder="Phone" name="phone" required="" class="form-control">
                    </div>
                    <div class="input-group margin-bottom-20">
                        <span class="input-group-addon"><i class="fa fa-lock"></i></span>
                        <input type="password" placeholder="Password" name="password" required="" class="form-control">
                    </div>
                    <div class="input-group margin-bottom-20">
                        <span class="input-group-addon"><i class="fa fa-lock"></i></span>
                        <input type="password" placeholder="Confirm Password" name="conf_password" required=""
                               class="form-control">
                    </div>

                    <div class="row">
                        <!--<div class="col-md-6 checkbox">
                            <label><input type="checkbox">Remember me</label>
                        </div>-->
                        <div class="col-md-12">
                            <button class="btn btn-success btn-block pull-right" type="submit" name="register">Register
                                Student
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </section>

<?php

}

function register_officer()
{
    ?>
    <section>
        <div class="row">
            <div class="col-md-7 col-md-offset-1">
                <form class="" method="post" action="register.php">
                    <div class="row">
                        <div class="col-md-6 ">
                            <div class="input-group margin-bottom-20">
                                <span class="input-group-addon"><i class="fa fa-user"></i></span>
                                <input type="text" placeholder="Firstname" name="firstname" required=""
                                       class="form-control">
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="input-group margin-bottom-20">
                                <span class="input-group-addon"><i class="fa fa-user"></i></span>
                                <input type="text" placeholder="Lastname" name="lastname" required=""
                                       class="form-control">
                            </div>
                        </div>
                    </div>
                    <div class="input-group margin-bottom-20">
                        <span class="input-group-addon"><i class="fa fa-envelope"></i></span>
                        <input type="text" placeholder="Email" name="email" required="" class="form-control">
                    </div>

                    <div class="input-group margin-bottom-20">
                        <span class="input-group-addon"><i class="fa fa-user"></i></span>
                        <input type="text" placeholder="Rank" name="rank" required="" class="form-control">
                    </div>
                    <div class="input-group margin-bottom-20">
                        <span class="input-group-addon"><i class="fa fa-phone"></i></span>
                        <input type="text" placeholder="Phone" name="phone" required="" class="form-control">
                    </div>
                    <div class="input-group margin-bottom-20">
                        <span class="input-group-addon"><i class="fa fa-lock"></i></span>
                        <input type="password" placeholder="Password" name="password" required="" class="form-control">
                    </div>
                    <div class="input-group margin-bottom-20">
                        <span class="input-group-addon"><i class="fa fa-lock"></i></span>
                        <input type="password" placeholder="Confirm Password" name="conf_password" required=""
                               class="form-control">
                    </div>

                    <div class="row">
                        <!--<div class="col-md-6 checkbox">
                            <label><input type="checkbox">Remember me</label>
                        </div>-->
                        <div class="col-md-12">
                            <button class="btn btn-success btn-block pull-right" type="submit" name="register_officer">
                                Register Officer
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </section>
<?php
}
  • 写回答

1条回答 默认 最新

  • duanjiong2021 2015-05-18 07:16
    关注

    This would be a bit more elegant solution than yours:

    <?php
    
    require_once 'functions.php';
    
    if (isset($_GET['action'])) {
        $option = $_GET['action'];
    
        echo $option;
    
        if(in_array($option, get_defined_functions()['user']) === true) {
            call_user_func($option);
        } else {
            complaint();
        }
    } else {
        echo '<div class="jumbotron">
      <h1>Welcome To The Administration Panel </h1>
      <p>You can manage accounts and view the complaints here.</p>
      </div>';
    }
    

    So then you can define a function edit() which will be automatically called by the call_user_func without altering the switch each time you add a new function.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器