解析PHP数组以构建SQL字符串

时间:2017-03-14 22:11:51

标签: php arrays

我正在传递来自select的数据数组,如下所示:

<div style="margin:10px;">
<select name="selectlist[]" multiple style="min-width: 200px;" id="selectlist">
  <option value="userid">User ID</option>
  <option value="username">User Name</option>
</select></div>
<div><input type="submit" value="Submit" id="btnYes" onclick="Scriptit()"></div>
<div id="displaypart"></div>
<script>
function Scriptit(){
var selectlist = $('#selectlist').val();
jQuery.ajax({ 
url: "<?php echo JURI::base();?>Terminator.php",
type: 'POST',
dataType: "html",
data: { selectlist: selectlist},
success : function(result) {
 $('#displaypart').empty();
$('#displaypart').append(result);
} ,
error: function(){}});}
</script>

现在在我的页面 Terminator.php 我如何检查选择了哪些元素(数组中的I.E.),以便我知道如何构建我的查询字符串。像这样的东西(pseudocdoe not php)

$selectlist = implode(',',$_REQUEST['selectlist']);

$sql = "Select";
if $selectlist.contains("userid") then $sql = .$sql & a.userid;
if $selectlist.contains("username") then $sql = .$sql & b.username;

if $selectlist.contains("userid") then $sql .$sql & from useridtable a;
if $selectlist.contains("username") then $sql = .$sql & inner join usernametable b on a.usralra = b.usralra;

1 个答案:

答案 0 :(得分:1)

您不必内爆数组然后搜索字符串。只需搜索您发布的数组。

获取数据为$ _POST ['selectdata']。

然后在数组中搜索

if(in_array('username', $selectdata)) $sql ='';