Connecting Tech Pros Worldwide Forums | Help | Site Map

how to put variables out of the loop?

Ja NE
Guest
 
Posts: n/a
#1: Nov 10 '05
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
--

Juliette
Guest
 
Posts: n/a
#2: Nov 10 '05

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
Guest
 
Posts: n/a
#3: Nov 10 '05

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
Guest
 
Posts: n/a
#4: Nov 10 '05

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
Guest
 
Posts: n/a
#5: Nov 10 '05

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
Guest
 
Posts: n/a
#6: Nov 11 '05

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
Guest
 
Posts: n/a
#7: Nov 11 '05

re: how to put variables out of the loop?


The key here is the .=
:-)

Closed Thread


Similar PHP bytes