我在codeigniter中使用JStree复选框,我可以使用以下代码打印复选框jstree。
<script>
$("#newtree").jstree({
"checkbox" : {
"keep_selected_style" : false
},
"plugins" : [ "checkbox" ]
});
</script>
我想要做的是检查选中的复选框,并相应地在codeigniter模型中更改我的MySql SELECT语句。
示例:如果我检查Male,我的sql语句必须是Select * from students where gender=Male
,否则我的sql语句应该是Select * from students
。
此外,如果我选中多个复选框,SQLquery应该附加检查结果。
示例:如果我检查了Male和Science sql查询应该是:
Select * from students where gender=male and subject=science
答案 0 :(得分:1)
$sql = "SELECT * FROM `students`";
$addition = [];
if ($gender == 'male')
{
$addition[] = " WHERE `gender`='male'";
}
if ($subject == 'science')
{
$addition[] = " WHERE `subject`='science'";
}
/*
* other conditions if any
*/
if (count($addition))
{
foreach($addition as $k => $v)
{
if ($k < 1)
{
$sql .= $v;
}
else
{
$sql .= " AND" . $v;
}
}
}