sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
Ja NE's Avatar

how to put variables out of the loop?


Question posted by: Ja NE (Guest) on November 10th, 2005 12:55 PM
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

--
Ja NE
http://fotozine.org/?omen=janimir
--
6 Answers Posted
Juliette's Avatar
Guest - n/a Posts
#2: Re: how to put variables out of the loop?

Ja NE wrote:[color=blue]
> 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
>[/color]


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.
Juliette's Avatar
Guest - n/a Posts
#3: Re: how to put variables out of the loop?

Juliette wrote:[color=blue]
> Ja NE wrote:
>[color=green]
>> 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
>>[/color]
>
>
> 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.[/color]


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.
Ja NE's Avatar
Guest - n/a Posts
#4: Re: how to put variables out of the loop?

Juliette <jrf_no_spam@jokeaday.net> wrote:

[color=blue][color=green]
> > And concerning the query... you need to use the WHERE clause.
> >[/color][/color]

sure, just forgot to add it here
[color=blue]
>
> 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:[/color]

no, this one wasn't.
[color=blue]
>
> return $sqlblock;
>
> If the code is not within a function, you can just use $sqlblock wherever.[/color]

in any case, it returns to me only the last $sqlblock, not all of them.
that is problem.

I need a way to put them all together after the loop.
in the loop script will define 1 or more $sqlblock, if is only one,
that's good (and, btw, all that mess is unnecessary) but if there are
more than one (and what was reason I started looking for solution) how
can I get them in one line?
obiously I need to create one variable from that array containing
$sqlblock[0], $sqlblock[1], $sqlblock[2]... and that is where I don't
know what to do...

$blockarray[$justnum]=$sqlblock;
array($blockarray);

will create array containing all my $sqlblock, but I can't find way to
put them latter in search query which, I think, must end like:

SELECT id,uname,name,famname FROM my_table WHERE $sqlblock[0]
$sqlblock[1] $sqlblock[2] ORDER BY id ASC

any more ideas?
tnx

--
Ja NE
http://fotozine.org/?omen=janimir
--
Juliette's Avatar
Guest - n/a Posts
#5: Re: how to put variables out of the loop?

Ja NE wrote:[color=blue]
> Juliette <jrf_no_spam@jokeaday.net> wrote:
>
>[/color]
<snip>[color=blue]
>[color=green]
>>return $sqlblock;
>>
>>If the code is not within a function, you can just use $sqlblock wherever.[/color]
>
>
> in any case, it returns to me only the last $sqlblock, not all of them.
> that is problem.
>
> I need a way to put them all together after the loop.
> in the loop script will define 1 or more $sqlblock, if is only one,
> that's good (and, btw, all that mess is unnecessary) but if there are
> more than one (and what was reason I started looking for solution) how
> can I get them in one line?
> obiously I need to create one variable from that array containing
> $sqlblock[0], $sqlblock[1], $sqlblock[2]... and that is where I don't
> know what to do...
>
> $blockarray[$justnum]=$sqlblock;
> array($blockarray);
>
> will create array containing all my $sqlblock, but I can't find way to
> put them latter in search query which, I think, must end like:
>
> SELECT id,uname,name,famname FROM my_table WHERE $sqlblock[0]
> $sqlblock[1] $sqlblock[2] ORDER BY id ASC
>
> any more ideas?
> tnx
>[/color]

You must have overlooked part of my answer...
Go back to my first mail and read again.
Ja NE's Avatar
Guest - n/a Posts
#6: Re: how to put variables out of the loop?

Juliette <jrf_no_spam@jokeaday.net> wrote:
[color=blue]
>
> You must have overlooked part of my answer...
> Go back to my first mail and read again.[/color]

yes, sorry... have placed dot on the wrong place (=. instead of .=)...
but yoy helped. thank you!

--
Ja NE
http://fotozine.org/?omen=janimir
--
pwiegers@gmail.com's Avatar
pwiegers@gmail.com November 11th, 2005 09:25 AM
Guest - n/a Posts
#7: Re: how to put variables out of the loop?

The key here is the .=
:-)

 
Not the answer you were looking for? Post your question . . .
196,788 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 196,788 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top Community Contributors