简体   繁体   中英

how select a string in PHP

I have a code where I would like to choose between four different possibility:

Query1

Query2

Query3

Query4

Once an user has choose his/her query my code has to execute a specific function in a different php file, the specific function should be activated depending on which index (1,2,3,4 that comes from the relative query) the user has chosen.

For instance if the user has chosen the Query1 the function:

 function a($index){
 }

where $index is 1, obtained directly form the query chosen.

Could you help me?

Thanks in advance

The Code Below might come in handy. The Idea here is that you Check within PHP if the Form was Submitted.... if it was, then simply get the Value of the Query (like: 1, 2, 3, 4). Then using Switch statement, You may then determine for yourself which Function should be called based on those Values: 1, 2, 3 and 4 respectively. NOTE: Be sure those functions are defined in your PHP Script as well...

<?php
    if(isset($_POST['run'])){
        $queryKey   = isset($_POST['query']) ? $_POST['query'] : null;

        if($queryKey){
            switch($queryKey){
                case "1":
                    callableFunctionForQuery1();
                    break;
                case "2":
                    callableFunctionForQuery2();
                    break;
                case "3":
                    callableFunctionForQuery3();
                    break;
                case "4":
                    callableFunctionForQuery4();
                    break;
                default:

            }
        }
    }
?>

<html>
<head>
</head>
    <body>
        <form name="query-processor" action="" method="POST">
            <label for="query" class="lbl" id="lbl-query">Please Select a Query to Run</label>
            <select name="query" class="form-control" id="query">
                <option value="1">Query1</option>
                <option value="2">Query2</option>
                <option value="3">Query3</option>
                <option value="4">Query4</option>
            </select>
            <input type="submit" value="Run" id="run" name="run" />
        </form>
    </body>
</html>

OK so i think what you want is to assign a function to a variable

$function = function() 
{
    echo 'executing function';
};

then call the function by using the variable name

$function();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM