473,511 Members | 9,983 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to put variables out of the loop?

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
--
Nov 10 '05 #1
6 1639
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.
Nov 10 '05 #2
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.
Nov 10 '05 #3
Juliette <jr*********@jokeaday.net> wrote:

And concerning the query... you need to use the WHERE clause.

sure, just forgot to add it here

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:
no, this one wasn't.

return $sqlblock;

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


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
--
Nov 10 '05 #4
Ja NE wrote:
Juliette <jr*********@jokeaday.net> wrote:

<snip>
return $sqlblock;

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

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


You must have overlooked part of my answer...
Go back to my first mail and read again.
Nov 10 '05 #5
Juliette <jr*********@jokeaday.net> wrote:

You must have overlooked part of my answer...
Go back to my first mail and read again.


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

--
Ja NE
http://fotozine.org/?omen=janimir
--
Nov 11 '05 #6
The key here is the .=
:-)

Nov 11 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2315
by: z0ink | last post by:
I am working on a small graphing application. In the process of graphing I use 3 seperate scripts for getting the job done. The first is the page that the use sees and selects all the data from. ...
8
2510
by: lawrence | last post by:
I'm learning Javascript. I downloaded a script for study. Please tell me how the variable "loop" can have scope in the first function when it is altered in the second function? It is not defined...
5
2408
by: masood.iqbal | last post by:
My simplistic mind tells me that having local variables within looping constructs is a bad idea. The reason is that these variables are created during the beginning of an iteration and deleted at...
9
2468
by: Javaman59 | last post by:
Using local declarations within a block often makes code more readable, but is it less efficient? eg... void P() { while (...) { int i = ...; bool b = ...; .... } }
6
3027
by: tcurdts | last post by:
Greetings, I'm using WindowsXP Pro v5.1and am writing a BAT file to loop through a list of files (contained in a .txt file) and pass them (actually variables derived from them) to another program...
0
7251
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7367
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7430
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7089
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7517
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5673
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3217
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1581
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.