Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7904533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:07:01+00:00 2026-06-03T10:07:01+00:00

(Original Questions) I am using jquery ui’s selectable script to control specific active keywords

  • 0

(Original Questions) I am using jquery ui’s selectable script to control specific active keywords in my webapp. View here: http://www.rickymason.net/letschat/main/home for reference

I have very little experience with javascript and I’m trying to figure out how to launch a function I have in my main model.

Updated function based on answers:

I have updated my code to support the new JSON/AJAX format. This required me to create an active/inactive session filter so that the user can add filters normally, and always use AJAX to update the thread list. This just made more sense to me.

Here is the code I have currently, which still is not working. I am attempting to make it so when the user clicks on a selectable category (through Jquery UI), the divID associated with the selection is passed through AJAX and returns a threadlist array that updates the div id =”board”.

Here is my current Controller set up:

public function home($page = 'home')
    {
        $data['user_id'] = $this->tank_auth->get_user_id();
        $data['username'] = $this->tank_auth->get_username();
        $data['threads'] = $this->thread_model->session_load();
        $data['title'] = ucfirst($page); // Capitalize the first letter
        $data['page'] = $page;
        $this->load->view('templates/head', $data);
        $this->load->view('templates/nav', $data);
        $this->load->view('main/newthread', $data);
        $this->load->view('main/addfilter', $data);
        $this->load->view('main/checkbox', $data);
        $this->load->view('main/displayfilter',$data);
        $this->load->view('main/board', $data);
        $this->load->view('templates/footer');
    }

    public function updatefilters($filters)
    {
        $filterarray = split("|", $filters);
        $this->thread_model->create_session_filter($filterarray); 
        $threadarray = $this->thread_model->get_threads();
        $data['json'] = '{"content":' + $threadarray + '}';
        $this->load->view('json_view', $data); // See step 4!!!
    }

Here is my current model code:

    public function get_threads()
    {
            $filter = $this->session->userdata('filter');
            $num_tags = count($filter);
            if ($num_tags > 0 && $num_tags <= 8) {
                $sql_select = "SELECT DISTINCT t.* ";
                $sql_from = " FROM ";
                $sql_where = " WHERE ";
                $sql_joins = "";
                $sql_order = "ORDER BY t.timestamp DESC";
                for ($i=0;$i<$num_tags;++$i) {
                    if ($i==0) {
                    $sql_from .= " filter AS f ";
                    $sql_where .= " f.tag LIKE '%" . $filter[0] . "%'";
                    $sql_joins .= " INNER JOIN filter_thread AS ft ON ft.filter_id = f.filter_id
                                    INNER JOIN thread AS t ON ft.thread_id = t.thread_id";
                    }
                    else {
                    $sql_where .= " OR f.tag LIKE '%" . $filter[$i] . "%'";
                    }
                }
            } else {
                break; 
            }
            $sql = $sql_select . $sql_from . $sql_joins . $sql_where . $sql_order;

            $query = $this->db->query($sql);
            $thread = $query->result_array();
        return json_encode($thread); //I am aware this is not correct
    }

    public function create_session_filter($filterstring)
    {
        $filterarray[] = $filterstring;
        $filter['filter'] = $filterarray;
        if ($this->session->userdata('filter') == TRUE) {
            $sessionfilter = $this->session->userdata('filter');
            $new = array_merge($sessionfilter, $filter['filter']);
            $this->session->unset_userdata('filter');
            $filter['filter'] = $new;
            $this->session->set_userdata($filter);
        } else {
            if (!$filterstring) {} else {
            $this->session->set_userdata($filter);
            }
        }
    }

    public function create_session_inactive_filter($filterstring)
    {
        $filterarray[] = $filterstring;
        $filter['inactivefilter'] = $filterarray;
        if ($this->session->userdata('inactivefilter') == TRUE) {
            $sessionfilter = $this->session->userdata('inactivefilter');
            $new = array_merge($sessionfilter, $filter['inactivefilter']);
            $this->session->unset_userdata('inactivefilter');
            $filter['inactivefilter'] = $new;
            $this->session->set_userdata($filter);
        } else {
            if (!$filterstring) {} else {
            $this->session->set_userdata($filter);
            }
        }
    }

And here is my current view code:

application/main/json_view.php

<?php
header("Content-Type: application/json");
echo $json;
?>

aplication/main/bdisplayfilter.php

<script>
    $(function() {
        $( "#selectable" ).selectable({
            selected: updateFilters,
            unselected: updateFilters
        });
        function updateFilters(ev, ui){    
        alert ("hello");
        // get the selected filters
        var $selected = $('#selectable').children('.ui-selected');
        // create a string that has each filter separated by a pipe ("|")
        var filters = $selected.map(function(){return this.id;}).get().join("|");
        $.ajax({
            url: '/main/updateFilters', //see step 2
            data: { filters: filters },
            success: function(data){
                // data is whatever json you decide to return from the server.
                // An easy way to do things is have data look like this:
                // { content: "<div>All my new threads that I want to show up</div>" }
                // then, you can replace some element on the page with the new content
                // For example, say your container has an id of threadContainer:
                $('#select').replaceWith(data.content);
            }
        }); }
    });
</script>
<ol id="selectable">
    <li class="ui-state-default" id="everything">Everything!</li>
    <li class="ui-state-default" id="entertainment">Entertainment</li>
    <li class="ui-state-default" id="sci/tech">Sci/Tech</li>
    <li class="ui-state-default" id="news">News</li>
    <?php 

    if ($this->session->userdata('inactivefilter') == true) {
            $inactivefilter = $this->session->userdata('inactivefilter');
            foreach ($inactivefilter as $new)
                {
                    echo "<li class='ui-state-default' id='custom'>$new</li>";
                }
        }
    ?>
</ol>
<?php
if ($this->session->userdata('inactivefilter') == true) {
echo "<form action='".base_url()."main/clear_filter'><input type='submit' value=clear></form>";
} ?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T10:07:07+00:00Added an answer on June 3, 2026 at 10:07 am

    EDIT: I’ve updated the url and data parts of the ajax call and added an additional step to enable query string parameters.

    1) Make the AJAX call

    You will want to make the same call for selected and unselected, since you can have multiple filters and you need things to update accordingly on both events. So, I’ll define a common function that both events can call.

    $(function() {
        $( "#selectable" ).selectable({
            selected: updateFilters,
            unselected: updateFilters
        });   
        function updatefilters(ev, ui){
            // get the selected filters
            var $selected = $('#selectable').children('.ui-selected');
            // create a string that has each filter separated by a pipe ("|")
            var filters = $selected.map(function(){return this.id;}).get().join("|");
            $.ajax({
                url: '/index.php',
                data: { c: main, m: updateFilters, filters: filters },
                success: function(data){
                    // data is whatever json you decide to return from the server.
                    // An easy way to do things is have data look like this:
                    // { content: "<div>All my new threads that I want to show up</div>" }
                    // then, you can replace some element on the page with the new content
                    // For example, say your container has an id of threadContainer:
                    $('#threadContainer').replaceWith(data.content);
                }
            });
        }
    });
    

    2) Enable query string parameters in application/config.php

    The section called Enabling Query Strings at the bottom of this article explains how to do that:
    http://codeigniter.com/user_guide/general/urls.html

    3) Create an action that will receive the filters

    Note that I’m using a controller called Page (which would live in /application/controllers/page.php). This action (updateFilters) could live in any controller you want.

    class Page extends CI_Controller {
    
        function __construct()
        {
            parent::__construct();
        }
    
        function index()
        {
        }
    
        function updateFilters($filters)
        {
            $filterarray = split("|", $filters);
            create_session_filter($filterarray); 
    
            $articlesHTML = getThreadList($filterarray); // See step 4!!!
            $data['json'] = '{"content":' + $articlesHTML + '}';
            $this->load->view('json_view', $data); // See step 5!!!
        }
    
        /* I've updated this slightly to accept an array */
        public function create_session_filter($filterarray)
        {
            $filter['filter'] = $filterarray;
            //... the rest of your stuff you already had
        }
    }
    

    4) Implement getThreadList method

    I don’t think you mentioned if you already had something set up for this. This would basically take an array of filters and then render a thread list based off that.

    5) Create json_view (if not already there)

    This will set the content type so that the browser knows the content is json.

    In /application/views/json_view.php:

    <?php
    header("Content-Type: application/json");
    echo $json;
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My original question can be found here , for which I've gotten some great
This question is sort of a follow-up to my original question here . Let's
Ok, here was my original question; Table one contains ID|Name 1 Mary 2 John
I'am maintaining a GUI built using JQuery. In one part of the GUI, multiple
Ok I have looked at a few questions on here concerning this topic. But
Scenario: I am using jQuery to lazy load some html and change the relative
I'm currently using jquery ajax to call a pagemethod (which works great); $.ajax({ type:
I need to attach a single event handler to multiple DOM elements using jQuery.
I am using jQuery resizable from interface.eyecon.ro/docs/resizable for a menu on a page that
I'm playing with mobile app creation with jQuery and an api I built using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.