0
我想让取数据的ajax请求在不同的表上执行连接操作取决于参数和表名。如何使用函数名称和参数作为变量调用适当的函数? PHP
$.ajax({ url: '/my/site',
data: {fun_name: 'join_user_and_state',
param: '12,1'
},
type: 'post',
success: function(output) {
alert(output);
}
});
在服务器端,行动之后的参数和功能的名称应该是阅读和相应的值应指向方法来调用,例如:
if(!empty($_POST['param']) && !empty($_POST['fun_name'])) {
$param= $_POST['param']; $fun_name= $_POST['fun_name'];
// want to call proper function name with parametters from $fun_name and $param
// parameters could be 2 or 3 or more its depends upon user choice
// how to call
}
//definitions
join_user_and_state($user_id,$state_id)
{
// will do join query here with 2 tables
}
join_user_and_city($user_id,$city_id)
{
// will do join query here with 2 tables
}
join_user_and_state_and_city($user_id,$state_id,$city_id)
{
// will do join query here with 3 tables
}
我如何可以调用相应的功能?
或有任何其他方式来达到同样的,我必须执行不同的不同连接查询取决于用户选择像USER_STATE,user_city,user_education,user_salary..etc(以及这些所有组合可能的)?和这些列存在于它们自己的表
是这种做法将是快或使用的if else给我快的结果? –
@amitgupta它很容易,因为它只是一条线。如果你后来添加更多的函数,这将是更好的方法,因为如果你有更多的函数,你需要越来越多的if语句。 – Gogolex
好的,谢谢,这是一个很好的方法来处理差异连接操作意味着每个连接的不同功能,或者我应该只做一个查询,并通过同一个查询所有变量?请参阅我的问题的最后3 4行。 –