Data stored in database as below where afternoon,evening are column name and comma separated strings are values for them.
morning = 'Fri,Sun';
afternoon = 'Thrs,Fri,Sat,Sun';
evening = 'Thrs,Fri,Sat,Sun';
This query works:
AND ( FIND_IN_SET( 'Sat', posts.afternoon )
OR FIND_IN_SET( 'Sat', posts.evening ) )
But I want something like this but I know cannot:
AND ( FIND_IN_SET( 'Fri','Sat', posts.afternoon )
OR FIND_IN_SET( 'Thrs','Fri','Sat', posts.evening ) )
So I want at least to search separately like below which I've tested and yields results:
AND ( FIND_IN_SET ('Fri', posts.afternoon) OR FIND_IN_SET ('Sat', posts.afternoon) OR FIND_IN_SET ('Sat', posts.evening))
Problem is how to modify my existing PHP script to match this, please? Below is my full code:
<?php
JSON formatted data received via ajax
$return = '{"avail":["Wed-2,Thrs-2","Thrs-3"]}';
In the above -1 means for monrning, -2 for afternoon and -3 for evening.I'm categorising them below.'
$avail = $data['avail'];
$days = array();
$cols = array();
$size = sizeof($avail);
if(($avail != "")&&($size > 1)){
$periods = array();
foreach($avail as $v){
list($day, $column) = explode("-", $v); // make sure you validated the data to avoid errors
$periods[$column][] = "'" . mysql_escape_string($day) . "'"; //strtolower// PHP would automatically create an empty array if $periods[$column] was not defined
}
$intToCol = array(1 => "morning", 2 => "afternoon", 3 => "evening");
// $periods should now be identical to ["2" => ["'sun'", "'mon'"], "3" => ["'sun'"]]
$conditions = array();
foreach($periods as $int => $days){
$dayString = implode(",", $days);
$conditions[] = " FIND_IN_SET ($dayString, posts." . $intToCol[$int].")" ;
}
$add_here = "AND (" . implode(" OR ", $conditions) . ")";
}else if(($avail != "")&&($size == 1))
{
foreach($avail as $k=>$v)
{
$v;
$array = explode('-', $v);
$day =$array[0]; // Wed
$column = $array[1]; // 2
if($column == 1)
{
$col = "morning";
}
if($column == 2)
{
$col = "afternoon";
}
if($column == 3)
{
$col = "evening";
}
}
$add_here = " posts.".$col." = '".$day."' ";
$sql = "SELECT * , (3956 * 2 * ASIN(SQRT( POWER(SIN(('$lat' - lat) * pi()/180 / 2), 2) +COS('$lat' * pi()/180) * COS(lat * pi()/180) * POWER(SIN(('$lon' - lon) * pi()/180 / 2), 2) ))) as distance from posts WHERE posts.subname LIKE '%$subject%' AND posts.pricing <= '$rate' ".$add_here."".$IsTutionCentre." GROUP BY posts.UUID having distance <= '$distance' order by distance";
$stmt =connection::$pdo->prepare($sql);
$stmt->execute();
$place=array();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$place[] = $row;
}
$_SESSION['subject'] = $place;
//send back to ajax call made page
echo json_encode($place);
}
?>
This MySQL function find_in_set() can search only for one string in a set of strings.
The first argument is a string, so there is no way to make it parse your comma separated string into strings (you can't use commas in SET elements at all!). The second argument is a SET, which in turn is represented by a comma separated string hence your wish to find_in_set('p,q,r', 'p,q,r,s') which works fine, but it surely can't find a string 'p,q,r' in any SET by definition - it contains commas.
You can use command like :
where setcolumn like '%p,q%'
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.