vinod
vinod

Reputation: 58

Call ajax in Jstree checkbox and pass checked data to controller (Codeigniter)

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

Answers (1)

Tpojka
Tpojka

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

Related Questions