php - Preg_match only working in order -
i having problems following code. getting users subjects using $_get sending server see if row contains subjects , if returns subject matches row each row matches. works fine if user enters lets "english,maths" , in database listed "maths, english" fail , not echo row although correct in different order. there doing wrong??
thanks,
curtisb
$subjects = $_get["subjects"]; while ($row = mysql_fetch_array($result, mysql_assoc) , ($counter < $max)) { if(preg_match('/'.$subjects.'/',$row['subjects'])){ echo "subject matches row"; } }
update: tried code below, echoing every row if not correct subject.
function is_matched($subjectsfromus, $subjectsfromdb){ $subjectsfromus = explode($subjectsfromus, ','); $subjectsfromdb = explode($subjectsfromdb, ','); foreach($subjects $s){ if(!in_array($s, subjectsfromdb)) return false; } return true; } while ($row = mysql_fetch_array($result, mysql_assoc) , ($counter < $max)) { if(is_matched($subjects,$row['subjects']) == true ){ echo "subjects matched"; } }
i think shouldn't use regexp in such case. must explode $subjects parameter ',' , traverse array trough $row['subjects']:
function is_matched($subjects, $subjectsfromdb){ $subjects = explode(',', $subjects); $subjectsfromdb = explode(',', $subjectsfromdb); foreach($subjects $s){ if(!in_array($s, $subjectsfromdb)) return false; } return true; }
Comments
Post a Comment