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 :(.

Hi,
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);
    $this->db->where('category',$cat);
    $d['query'] = $this->db->get('table_to_query');
    $this->load->view('subcat',$d);
}

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

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

    json_encode($result);
}
?>

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.

<script>
// 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;
    if(enableCache){
        jsCache[url] =  AjaxObjects[ajaxIndex].response;
    }
    AjaxObjects[ajaxIndex] = false;
 document.getElementById("ajax_container").innerHTML = '';

}

function ShiftChanger(divId,url,id) {
    //to show the div
    document.getElementById(divId).innerHTML="";
    if(enableCache && jsCache[url]){
        document.getElementById(divId).innerHTML = jsCache[url];
        return;
    }   
    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);};
    AjaxObjects[ajaxIndex].runAJAX();
}

function callAjaxFunction(value)
{   
if(value!=-1)
    ShiftChanger('shiftcontainer','ajax_category.php',value);
 }


 </script>

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:

/controller_name/ajax_category/category_id

you can change ShiftChanger() to this:

ShiftChanger('shiftcontainer','ajax_category',value);

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, networking, learning, and sharing knowledge.