1

I have this code:

<?php 
$agenti = $_GET['agenti'];
$agen = array();
if (is_array($agenti)) {foreach($agenti as $val) { $agen[] = "ch.employeename LIKE '" . $val . "'"; }} else {$agen[] = 'true';}

    $raw_results = mysql_query("SELECT distinct ch.employeename, ch.customername, 
                                       ch.customerowner, ch.QuestionnaireName, ch.parentgroupname,
                                       ch.groupname, ch.visitdate from chestionare ch
                                    WHERE ch.visitdate >= '".$_GET['from']."' AND ch.visitdate <= '".$_GET['to']."' 
                                    AND (" . implode(' OR ', $agen) . ")
                                    AND ch.customerowner like '$clienti' 
                                    AND ch.class like '$task' 
                                    AND ch.parentgroupname like '$categorie' 
                                    ") 
                                or die(mysql_error());

    if(mysql_num_rows($raw_results) > 0){ 
        while($results = mysql_fetch_array($raw_results)){ ?>

What can I do if $agenti has multiple values (60 values). Values came from another file with <select multiple="multiple"> <option value="test"> test </option>< /select>

2

3 Answers 3

3

If $agenti is dynamic array. Simple example :

$agen = array();

foreach ($agenti as $val) {
    $agen[] = "ch.employeename like '" . $val . "'"; 
}

$raw_results = mysql_query(
           "SELECT distinct ch.employeename, ch.customername, 
               ch.customerowner, ch.QuestionnaireName, ch.parentgroupname,
               ch.groupname, ch.visitdate from chestionare ch
            WHERE ch.visitdate >= '".$_GET['from']."' AND ch.visitdate <= '".$_GET['to']."' 
            AND (" . implode(' OR ', $agen) . ")
            AND ch.customerowner like '$clienti' 
            AND ch.class like '$task' 
            AND ch.parentgroupname like '$categorie' 
         ");
or die(mysql_error());
Sign up to request clarification or add additional context in comments.

4 Comments

hi paul. can you help me with your script, it dosen't return results. i have modified the foreach like this: if(is_array($agenti)) { .... } but still nothing.
@rozatrra if(is_array($agenti)) { .... } else {$agen[] = 'true';} Should work fine
if i put the else condition, it returns all agents, i have update the script.
For the record, this is NOT a secure or stable querying script. Do not use this in any real application (or even if you are just practicing).
1

I think you can use OR like

if you meant $agenti is an array so it may be like this :

$agenti=array(John,Tommy,Mark);

which means this:

$agenti=array(
    0=>John,1=>Tommy,2=>Mark
);

so you should point to its elements like $agenti[0]

$raw_results = mysql_query("SELECT distinct ch.employeename, ch.customername, ch.customerowner, ch.QuestionnaireName, ch.parentgroupname,
  ch.groupname, ch.visitdate from chestionare ch
   WHERE ch.visitdate >= '{$_GET['from']}' AND ch.visitdate <= '{$_GET['to']}' 
    AND (
         ch.employeename like '{$agenti[0]}' 
         OR ch.employeename like '{$agenti[1]}' 
         OR ch.employeename like '{$agenti[2]}' 
    )
    AND ch.customerowner like '{$clienti}' 
    AND ch.class like '{$task}' 
    AND ch.parentgroupname like '{$categorie}' 
 ") or die(mysql_error());

2 Comments

thx, but i keep agenti(employees) in another file with select multiple option and there are 60 of them.
@rozatrra - if Paul's answer is what you were looking for maybe you should accept it. (-:
1
<?php
            $agenti = array('sam','robin','sugar');
            $qry = "SELECT distinct ch.employeename, ch.customername, 
                     ch.customerowner, ch.QuestionnaireName, ch.parentgroupname,
                      ch.groupname, ch.visitdate from chestionare ch
                    WHERE ch.visitdate >= '".$_GET['from']."' AND ch.visitdate <= '".$_GET['to']."' 
                    AND ch.employeename IN('".implode("','",$agenti)."')  
                    AND ch.customerowner 
                    AND ch.class like '$task' 
                    AND ch.parentgroupname like '$categorie'
                                    ";

            /* Always use this way so it becomes easier for you to echo the query*/                         
            $raw_results = mysql_query($qry);
?>

7 Comments

please complete your answer and make it reach with an example and I would put you a +1.
ok here is +1 and I have also edited your answer adding description and links.
@ Jignesh Rawal - please add some description of what the new code does and how the IN Clause works.
dev.mysql.com/doc/refman/5.6/en/…. Use the link to get it further cleared up.
you know you can edit your post to add the link and the description and also the php close (?>) tag is not seen as a code try to put some spaces before it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.