Jump to content

Query question for search function


michaelkirby

Recommended Posts

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!!

Link to comment
https://forums.phpfreaks.com/topic/200157-query-question-for-search-function/
Share on other sites

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??

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.