Reputation: 401
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>
Upvotes: 1
Views: 1131
Reputation: 549
<?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);
?>
Upvotes: 1
Reputation: 785
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());
Upvotes: 3
Reputation: 1788
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());
Upvotes: 1