简体   繁体   中英

How to pass multiple parameters in SELECT query in jquery?

I am using below textbox to add tags.

在此处输入图片说明

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 (ie '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 ie 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.

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.

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM