You are here: Re: how to put variables out of the loop? « PHP Programming Language « IT news, forums, messages
Re: how to put variables out of the loop?

Posted by Juliette on 11/10/05 19:12

Juliette wrote:
> Ja NE wrote:
>
>> I'm trying to write a search script for my site. basic serch is fine, I
>> can search for some word in one or other or third table, column... no
>> problem, but now I would like to serch for name and family name of
>> rtegistered users. so I need to search in more than one table. for one
>> or two or even more words (imagine you are looking for Jan Michael
>> Bellay) do I need query with AND... o.k., here is what I have done so
>> far:
>>
>> $search = $_POST['serach']
>> if(isset($search)) {
>> // spliting search terms
>> $oneword = explode(" ", $search);
>> // counting numbers of terms to search
>> $all=str_word_count($search);
>> foreach($oneword as $number => $searchoneword) {
>> // forming sql block if there is more than one term to search
>> if($number < ($all-1)) {
>> $sqlblock = "uname LIKE %$searchoneword% OR
>> name LIKE %$searchoneword% OR
>> famname LIKE %$searchoneword% AND ";
>> }
>> // forming sql block if there is one or is the last one to search
>> else {
>> $sqlblock = "uname LIKE %$searchoneword% OR
>> name LIKE %$searchoneword% OR
>> famname LIKE %$searchoneword% ";
>> }
>> }
>> // and now... WHAT?
>> }
>>
>> I have tried all I know. and that isn't much.
>> tried to create array in foreach loop, couldnt get out what I wanted.
>> tried to write a function of above, to get out something with return()
>> but don't know what to do later...
>> I can echo what I want, but that isn't that... I need to put those block
>> together(!) in one variable which I will later use in slq query like:
>>
>> $query = "SELECT $alltogether ORDER BY id ACS";
>>
>> anyone can suggest me what to do?
>> tnx
>>
>
>
> My two cents:
> * put the sql phrases between brackets so it will be clear which groups
> of selection criteria belong together (you will get very strange
> results otherwise)
> * string the phrases together (take note of the .= in the below example)
>
> You would get something like:
> $sqlblock .= "(uname LIKE %$searchoneword% OR
> name LIKE %$searchoneword% OR
> famname LIKE %$searchoneword%) AND ";
>
>
> And concerning the query... you need to use the WHERE clause.
>
> Good luck and have a look at the manuals.
>
> Grz, J.


Oh .. and if it is defined within a function (which it isn't by the
looks of it), you can then return it by doing:

return $sqlblock;

If the code is not within a function, you can just use $sqlblock wherever.

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация