通过函数调用PHP编辑

时间:2015-05-18 07:11:26

标签: php url-rewriting function-calls

我在functions.php的php页面中包装函数并访问这些页面,我在url中放置了一个动作和一个值。我还有一个function_calls.php,它请求动作及其值,并将其传递给调用适当函数的switch case。我遇到的问题是,我想编辑一个字段。我没有合适的案例(对于交换机),我希望看起来像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 个答案:

答案 0 :(得分:0)

这将是比你更优雅的解决方案:

<?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>';
}

那么您可以定义一个函数edit()call_user_func将自动调用该函数,而不会在每次添加新函数时更改switch