michaelkirby Posted April 29, 2010 Share Posted April 29, 2010 Hi all, I have implemented some search code that I have sourced off the net and have adjusted to fit my needs. What I want to be able to do is search by either fname or organisation name according to the search text which it does, but to also only select the person if the profilestatus = public. I tired adding this to the query but with no luck. Can anyone advise what I'm doing wrong? Below is the code: $todo=$_POST['todo']; if(isset($todo) and $todo=="search"){ $search_text=$_POST['search_text']; $value=$_POST['type']; $county = Berkshire; //echo $type; $search_text=ltrim($search_text); $search_text=rtrim($search_text); if($value <> "designer"){ $query="select * from users where fname or organisationname ='$search_text' and profilestatus='public'"; } else{ $kt=split(" ",$search_text);//Breaking the string to array of words // Now let us generate the sql while(list($key,$val)=each($kt)){ if($val<>" " and strlen($val) > 0){$q .= " fname like '%$val%' or organisationname like '%$val%' or ";} }// end o $kt=splif while $q=substr($q,0,(strlen($q)-3)); // this will remove the last or from the string. $query="select * from users where $q "; } } echo "<br><br>"; $nt=mysql_query($query); //echo mysql_error(); ?> Thanks in advance!! Quote Link to comment https://forums.phpfreaks.com/topic/200157-query-question-for-search-function/ Share on other sites More sharing options...
Ken2k7 Posted April 29, 2010 Share Posted April 29, 2010 You have to break that up. ... WHERE fname = '$search_text' OR organisationname = '$search_text' ... BTW, please read the rules, especially #4 of Forum DOs. The link is located in the red text that displays before you post. Quote Link to comment https://forums.phpfreaks.com/topic/200157-query-question-for-search-function/#findComment-1050502 Share on other sites More sharing options...
michaelkirby Posted April 29, 2010 Author Share Posted April 29, 2010 Hi Ken2k7 I just tried breaking it up and the query still produces people that i search for but it does not take into account the condtion I have put in place for profilestatus='public' This is what the code looks like now: $todo=$_POST['todo']; if(isset($todo) and $todo=="search"){ $search_text=$_POST['search_text']; $value=$_POST['type']; $county = Berkshire; //echo $type; $search_text=ltrim($search_text); $search_text=rtrim($search_text); if($value <> "designer"){ $query="select * from users where profilestatus='public' and fname ='$search_text' or organisationname ='$search_text'"; } else{ $kt=split(" ",$search_text);//Breaking the string to array of words // Now let us generate the sql while(list($key,$val)=each($kt)){ if($val<>" " and strlen($val) > 0){$q .= " fname like '%$val%' or organisationname like '%$val%' or ";} }// end o $kt=splif while $q=substr($q,0,(strlen($q)-3)); // this will remove the last or from the string. $query="select * from users where $q "; } } echo "<br><br>"; $nt=mysql_query($query); //echo mysql_error(); ?> Any Ideas?? Quote Link to comment https://forums.phpfreaks.com/topic/200157-query-question-for-search-function/#findComment-1050516 Share on other sites More sharing options...
Ken2k7 Posted April 29, 2010 Share Posted April 29, 2010 AND has precedence over OR. If you're going to do that, put parentheses around the fname ='$search_text' or organisationname ='$search_text'. Quote Link to comment https://forums.phpfreaks.com/topic/200157-query-question-for-search-function/#findComment-1050523 Share on other sites More sharing options...
michaelkirby Posted April 29, 2010 Author Share Posted April 29, 2010 Hi, Sorry I don't quite understand what you mean? Can you explain a little further please? Your help is much appreciated!! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/200157-query-question-for-search-function/#findComment-1050610 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.