473,785 Members | 2,411 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($searc h)) {
// spliting search terms
$oneword = explode(" ", $search);
// counting numbers of terms to search
$all=str_word_c ount($search);
foreach($onewor d 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 1651
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($searc h)) {
// spliting search terms
$oneword = explode(" ", $search);
// counting numbers of terms to search
$all=str_word_c ount($search);
foreach($onewor d 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($searc h)) {
// spliting search terms
$oneword = explode(" ", $search);
// counting numbers of terms to search
$all=str_word_c ount($search);
foreach($onewor d 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*********@jo keaday.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($blockarr ay);

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,f amname 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*********@jo keaday.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($blockarr ay);

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,f amname 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*********@jo keaday.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
2331
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. This selected data gets passed internally using session variables to the next page. This page takes the passed data, runs it for error checking and determines where to send it next. I have gotten most of the desired results through this method...
8
2559
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 in global space, therefore it is not a global variable, yes? Even if it was global, how would it get from one function to another? In PHP variables are copied by value. Are they copied by reference in Javascript? <SCRIPT LANGUAGE="JavaScript">
5
2419
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 the end of the iteration. Kindly note that I am not talking about dynamically allocating memory within a loop, which is a perfectly valid operation (albeit with a chance of resulting in a memory leak, unless we are careful). The most common...
9
2490
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
3037
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 (ERDAS Imagine). What appears to be happening is that the FOR loop isn't utilizing the variables until the 2nd iteration after the variable is set. Since one variable is dependent on another, I have to run the program 3 times before the variables...
0
9646
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9483
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10346
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10096
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7504
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6742
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5514
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2887
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.