0

I am using below textbox to add tags.

enter image description here

When I enter any tag(say 'Java' here) and then press enter, tagit() of jquery is called.

$(function () {

  var availableTagname = [<?=$testTagname?>];

  $('#demo4').tagit({tagSource:availableTagname, sortable:true, tagsChanged:function (a) {
      Counttagname(a);
  } });

'a' is a value of the textbox. a = java here. then Counttagname() is called.

function Counttagname(value)
{    
 if(value!="")
    {  
        if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
        else
        {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function()
        {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {                            document.getElementById("divcounttagname").innerHTML="";
                      document.getElementById("divcounttagname").innerHTML=xmlhttp.responseText;
            }
        }
 //         alert(value);
        xmlhttp.open("GET","<?=base_url()?>index.php/admins/joborders/bindcounttagname/"+value,true);
        xmlhttp.send();
    }
    else
    {
        document.getElementById("divcounttagname").innerHTML='';
        document.getElementById("divcounttagname").innerHTML='<div>There are no Candidate</div>';
    }

}

that value (i.e. 'java') is passed in "xmlhttp.open("GET","index.php/admins/joborders/bindcounttagname/"+value,true);"

Now, bindcounttagname() is called in controllder.

function bindcounttagname($value)
{   

    $this->load->model('mjoborders');
    $data['counttagname'] = $counttagname = $this->mjoborders-    >Getcounttagname($value);

and then Getcounttagname() is called in model.

function Getcounttagname($value)
{   
    $data = array();    
    $Q = $this->db->select('*,count(candidateid) as countcandidate FROM tbl_candidatetag where tagname = "'.$value.'"');
    $Q = $this->db->get();

    if ($Q->num_rows() > 0)
    {
        foreach ($Q->result_array() as $row)
        {   

            $data[] = $row;             
        }
    }       
    $Q->free_result();          
    return $data; 
}

Tag passes as a parameter in above query.

But, When I enter second tag that tag should also pass in query with earlier tag i.e. IN ('java','css3');

But it does not take more than one value as a parameter.

I tried to use array to pass more tags in query like below link but the query does not fetch any row.. 1. "Passing an array to a query using a WHERE clause" 2. passed static two values also in query but it does not fetch any row.

Please tell me how to pass more than one tag as a parameter in query.

Community
  • 1
  • 1
Advait
  • 5,771
  • 3
  • 18
  • 18

2 Answers2

0

If it's possible to pass multiple parameters to Getcounttagname, i would rewrite it like this:

function Getcounttagname($value1, $value2, ...)
{   
    $values = func_get_args();
    $val_str = implode("', '", $values);
    $data = array();    
    $Q = $this->db->select("*,count(candidateid) as countcandidate FROM tbl_candidatetag where tagname IN('".$val_str."'");
    $Q = $this->db->get();

    if ($Q->num_rows() > 0)
    {
        foreach ($Q->result_array() as $row)
        {   

            $data[] = $row;             
        }
    }       
    $Q->free_result();          
    return $data; 
}

What i'm doing is getting all parameters into a string which looks like this: 'a', 'b', 'c' and building sql query with them. So the final query looks like select * from tbl_candidatetag where tagname IN('a', 'b', 'c') and this way all of these tags will be included in result set.

keune
  • 5,779
  • 4
  • 34
  • 50
  • you are right Keune but myy Counttagname(a); is called when I write 'java' and press enter. I want to fire query on each tag entered. First when first tag is entered and then second time with two tags and so on. – Advait Aug 31 '13 at 10:30
  • Keune, my function arguments are not fixed. First there will be one arg1, second there will be arg1, arg2 and so on. – Advait Aug 31 '13 at 11:14
  • you can add as many arguments as you want. func_get_args() will get all of them. – keune Aug 31 '13 at 11:18
  • please help. I have found this code from stackoverflow post only. But how to use this code in my existing code? tagsChanged: function(tagValue,action,element){ list.push(tagValue); $.ajax({ url: "dostuff.php", type: "POST", data:{ items:list.join("::")}, success: function(data){ $('#wrap').append(data); } }); } – Advait Aug 31 '13 at 11:24
0

The answer referenced should work, however you have to make sure that you are surrounding each element in your string with quotes. That example would work for numbers, but if you dont surround your values with quotes, the query will think you're referencing variables and not string literals. So from the other question, instead of using:

$value = join(',',$value); 

you would want to use

$value = "'" . implode("','", $value) . "'";

So your function would be

 function Getcounttagname($value)
{
    $value = "'" . implode("','", $value) . "'";

    $data = array();    
    $Q = $this->db->select('*,count(candidateid) as countcandidate FROM tbl_candidatetag where tagname = "'.$value.'"');
    $Q = $this->db->get();

    if ($Q->num_rows() > 0)
    {
        foreach ($Q->result_array() as $row)
        {   

            $data[] = $row;             
        }
    }       
    $Q->free_result();          
    return $data; 
}

You can also make that ajax code much easier by using the jQuery .ajax function, since you are already half-utilizing jQuery.

http://api.jquery.com/jQuery.ajax/

xJoshWalker
  • 457
  • 2
  • 12