Hello everyone, I am trying to create dynamic dropdown of category and when a particular category is selected then it should show sub-category automatically from the database. I have used this using javascript in OOP PHP but having difficulty while using it with codeignitor.

So please anyone, help me with this code. As I am newbie, please show me to do this from the beginning.
Thanks in advance. Desperately, need some suggestion asap :(.

Recommended Answers

All 3 Replies

inside the controller create a function to retrieve the sub-category, and then point your javascript to that link, I'm supposing you will use ajax for this. So inside the controller you write a simple function:

public function get_subcat()
    $cat = $this->uri->segment(3);
    $d['query'] = $this->db->get('table_to_query');

inside the view you can return plain text or json, in this example you will get json:

if($query->num_rows() != 0)
    $result = array();
    foreach($query->result() as $row)
        $result[] = $row->subcategory;


to access the data just point the ajax call to /controller/get_subcat/category_name

hey actually I am doing something like this which is not working in codeignitor but works in PHP.

// JavaScript Document
var enableCache = false;
var jsCache = new Array();
var AjaxObjects = new Array(new sack(),new sack());

function ShowContent(divId,ajaxIndex,url)
    document.getElementById(divId).innerHTML = AjaxObjects[ajaxIndex].response;
        jsCache[url] =  AjaxObjects[ajaxIndex].response;
    AjaxObjects[ajaxIndex] = false;
 document.getElementById("ajax_container").innerHTML = '';


function ShiftChanger(divId,url,id) {
    //to show the div
    if(enableCache && jsCache[url]){
        document.getElementById(divId).innerHTML = jsCache[url];
    var ajaxIndex = AjaxObjects.length;
    // document.getElementById("ajax_container").innerHTML = '<img src=images/ajax-loader-pink.gif width=16 height=16 hspace=10 vspace=10 />';
    AjaxObjects[ajaxIndex] = new sack();
    AjaxObjects[ajaxIndex].requestFile = url+"?id="+id;
    document.getElementById("ajax_container").innerHTML = '<img src=ajax_loader.gif hspace=10 vspace=10 />';     AjaxObjects[ajaxIndex].onCompletion = function(){ ShowContent(divId,ajaxIndex,url+"?id="+id);};

function callAjaxFunction(value)


This is a javascript that I need to change for codeignitor. Here you can see a filename called ajax_category.php so I need to know how to change this to the controller.
Anyone with some ideas??

If you add a function to the controller as my previous example:


you can change ShiftChanger() to this:


and at line 28 change:

AjaxObjects[ajaxIndex].requestFile = url+"/"+id;

then I think it should work, the resulting link will be something like: /categories/ajax_category/12

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.