Reputation: 58
I am using JStree checkbox in codeigniter, I am able to print he checkbox jstree using following code.
<script>
$("#newtree").jstree({
"checkbox" : {
"keep_selected_style" : false
},
"plugins" : [ "checkbox" ]
});
</script>
What I want to do is to check for checked checkbox and accordingly alter my MySql SELECT statement in codeigniter model.
Example: If I check on Male, my sql statement must be Select * from students where gender=Male
else my sql statement should be Select * from students
.
Also if I check multiple checkbox SQLquery should append the checked result.
Example: If I checked Male and Science sql query Should be:
Select * from students where gender=male and subject=science
Upvotes: 0
Views: 428
Reputation: 7111
$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;
}
}
}
Upvotes: 1