473,396 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

What's wrong the the logic in this code?

Greetings:

I am attempting to get conditional output based on POSTed form data.
If the posted value is either the key or value of an array, $x=key and
$q=foo. elseif it is neither key nor value(I tried || too) , $q=bar.
Even though $x is properly assigned the new value, $q always ends up
being bar. Abstractions aside, here's the code: (note ## comments
added to this post)

<snip>
$search = $_POST[search];
$sort = $_POST[sortby];
$states = array(
"AL" ="Alabama",
"AK" ="Alaska",
"AZ" ="Arizona",
...
);
##echo $search here outputs the POSTed input
foreach ($states as $key =$value)

if ($search == $value || $search == $key) {
$search = $key;
##echo $search here outputs the new value, when applicable
$q = "
SELECT
organization.*, contacts.*
FROM organization
LEFT JOIN contacts
ON organization.org_id = contacts.org_id
OR organization.org_id = 0
WHERE organization.state
= '$search'
OR contacts.state
= '$search'
ORDER BY '$sort' ASC";

} elseif ($search != $value && $search != $key) { ##I tried || here too.
##echo $search here outputs the new value too, when applicable
$q = "
SELECT
CONCAT(contacts.firstname,' ',contacts.lastname) AS fullname,
organization.*, contacts.*
FROM organization
LEFT JOIN contacts
ON organization.org_id = contacts.org_id
OR organization.org_id = 0
WHERE organization.state
LIKE '%$search%'
OR organization.orgname
LIKE '%$search%'
OR contacts.state
LIKE '%$search%'
OR contacts.lastname
LIKE '%$search%'
OR contacts.firstname
LIKE '%$search%'
ORDER BY '$sort' ASC";
}

$result = mysql_query($q)
or die('Query Failed: ' . mysql_error());
</snip>

Where is this evaluation breaking down and why? Thanks in advance.
--

Regards,

Jeff Gardner
___________________________

"Contrary to popular belief, Unix is user friendly. It just happens
to be very selective about who its friends are." --Kyle Hearn
Sep 15 '06 #1
6 1299
"Jeff Gardner" <us**@example.tldwrote in message
news:FN***************@newssvr11.news.prodigy.com. ..
Greetings:

I am attempting to get conditional output based on POSTed form data.
If the posted value is either the key or value of an array, $x=key and
$q=foo. elseif it is neither key nor value(I tried || too) , $q=bar.
Even though $x is properly assigned the new value, $q always ends up
being bar. Abstractions aside, here's the code: (note ## comments
added to this post)

<snip>
$search = $_POST[search];
$sort = $_POST[sortby];
$states = array(
"AL" ="Alabama",
"AK" ="Alaska",
"AZ" ="Arizona",
...
);
##echo $search here outputs the POSTed input
foreach ($states as $key =$value)

if ($search == $value || $search == $key) {
$search = $key;
##echo $search here outputs the new value, when applicable
$q = "
SELECT
organization.*, contacts.*
FROM organization
LEFT JOIN contacts
ON organization.org_id = contacts.org_id
OR organization.org_id = 0
WHERE organization.state
= '$search'
OR contacts.state
= '$search'
ORDER BY '$sort' ASC";

} elseif ($search != $value && $search != $key) { ##I tried || here too.
##echo $search here outputs the new value too, when applicable
$q = "
SELECT
CONCAT(contacts.firstname,' ',contacts.lastname) AS fullname,
organization.*, contacts.*
FROM organization
LEFT JOIN contacts
ON organization.org_id = contacts.org_id
OR organization.org_id = 0
WHERE organization.state
LIKE '%$search%'
OR organization.orgname
LIKE '%$search%'
OR contacts.state
LIKE '%$search%'
OR contacts.lastname
LIKE '%$search%'
OR contacts.firstname
LIKE '%$search%'
ORDER BY '$sort' ASC";
}

$result = mysql_query($q)
or die('Query Failed: ' . mysql_error());
</snip>

Where is this evaluation breaking down and why? Thanks in advance.
--

Regards,

Jeff Gardner
Instead of the elseif you could just use else. Why check again, it's either
a state value, or something else.

Hans
Sep 15 '06 #2
Hans 'pritaeas' Pollaerts wrote:
"Jeff Gardner" <us**@example.tldwrote in message
news:FN***************@newssvr11.news.prodigy.com. ..
>Greetings:

I am attempting to get conditional output based on POSTed form data.
If the posted value is either the key or value of an array, $x=key and
$q=foo. elseif it is neither key nor value(I tried || too) , $q=bar.
Even though $x is properly assigned the new value, $q always ends up
being bar. Abstractions aside, here's the code: (note ## comments
added to this post)

<snip>
$search = $_POST[search];
$sort = $_POST[sortby];
$states = array(
"AL" ="Alabama",
"AK" ="Alaska",
"AZ" ="Arizona",
...
);
##echo $search here outputs the POSTed input
foreach ($states as $key =$value)

if ($search == $value || $search == $key) {
$search = $key;
##echo $search here outputs the new value, when applicable
$q = "
SELECT
organization.*, contacts.*
FROM organization
LEFT JOIN contacts
ON organization.org_id = contacts.org_id
OR organization.org_id = 0
WHERE organization.state
= '$search'
OR contacts.state
= '$search'
ORDER BY '$sort' ASC";

} elseif ($search != $value && $search != $key) { ##I tried || here too.
##echo $search here outputs the new value too, when applicable
$q = "
SELECT
CONCAT(contacts.firstname,' ',contacts.lastname) AS fullname,
organization.*, contacts.*
FROM organization
LEFT JOIN contacts
ON organization.org_id = contacts.org_id
OR organization.org_id = 0
WHERE organization.state
LIKE '%$search%'
OR organization.orgname
LIKE '%$search%'
OR contacts.state
LIKE '%$search%'
OR contacts.lastname
LIKE '%$search%'
OR contacts.firstname
LIKE '%$search%'
ORDER BY '$sort' ASC";
}

$result = mysql_query($q)
or die('Query Failed: ' . mysql_error());
</snip>

Where is this evaluation breaking down and why? Thanks in advance.
--

Regards,

Jeff Gardner

Instead of the elseif you could just use else. Why check again, it's either
a state value, or something else.

Hans

I added the elsif because I get the same output with an else. That's
why I suspect that there is a logic problem. For some reason, the second
$q variable is populated regardless of the previous conditional
statement being satisfied or not. If a state is not input, $search
remains untouched and the 2nd $q is populated as expected. If a state
is input, $search is modified but the 2nd $q is still populated.

--

Regards,

Jeff Gardner
___________________________

"Contrary to popular belief, Unix is user friendly. It just happens
to be very selective about who its friends are." --Kyle Hearn
Sep 15 '06 #3
Jeff Gardner wrote:
Greetings:

I am attempting to get conditional output based on POSTed form data.
If the posted value is either the key or value of an array, $x=key and
$q=foo. elseif it is neither key nor value(I tried || too) , $q=bar.
Even though $x is properly assigned the new value, $q always ends up
being bar. Abstractions aside, here's the code: (note ## comments
added to this post)

<snip>
$search = $_POST[search];
$sort = $_POST[sortby];
$states = array(
"AL" ="Alabama",
"AK" ="Alaska",
"AZ" ="Arizona",
...
);
##echo $search here outputs the POSTed input
foreach ($states as $key =$value)

if ($search == $value || $search == $key) {
$search = $key;
##echo $search here outputs the new value, when applicable
$q = "
SELECT
organization.*, contacts.*
FROM organization
LEFT JOIN contacts
ON organization.org_id = contacts.org_id
OR organization.org_id = 0
WHERE organization.state
= '$search'
OR contacts.state
= '$search'
ORDER BY '$sort' ASC";

} elseif ($search != $value && $search != $key) { ##I tried || here too.
##echo $search here outputs the new value too, when applicable
$q = "
SELECT
CONCAT(contacts.firstname,' ',contacts.lastname) AS fullname,
organization.*, contacts.*
FROM organization
LEFT JOIN contacts
ON organization.org_id = contacts.org_id
OR organization.org_id = 0
WHERE organization.state
LIKE '%$search%'
OR organization.orgname
LIKE '%$search%'
OR contacts.state
LIKE '%$search%'
OR contacts.lastname
LIKE '%$search%'
OR contacts.firstname
LIKE '%$search%'
ORDER BY '$sort' ASC";
}

$result = mysql_query($q)
or die('Query Failed: ' . mysql_error());
</snip>

Where is this evaluation breaking down and why? Thanks in advance.
You're problem is you are setting $q each time through the loop, then
using it outside of the loop. $q will always have the last value set in
the loop.

If $search matched the last state in your list, you would get the value
from your "then" clause. Any other time you'll get the value from your
"then" clause.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Sep 15 '06 #4
Jeff Gardner wrote:
Hans 'pritaeas' Pollaerts wrote:
>"Jeff Gardner" <us**@example.tldwrote in message
news:FN***************@newssvr11.news.prodigy.com ...
>>Greetings:

I am attempting to get conditional output based on POSTed form data.
If the posted value is either the key or value of an array, $x=key and
$q=foo. elseif it is neither key nor value(I tried || too) , $q=bar.
Even though $x is properly assigned the new value, $q always ends up
being bar. Abstractions aside, here's the code: (note ## comments
added to this post)

<snip>
$search = $_POST[search];
$sort = $_POST[sortby];
$states = array(
"AL" ="Alabama",
"AK" ="Alaska",
"AZ" ="Arizona",
...
);
##echo $search here outputs the POSTed input
foreach ($states as $key =$value)

if ($search == $value || $search == $key) {
$search = $key;
##echo $search here outputs the new value, when applicable
$q = "
SELECT
organization.*, contacts.*
FROM organization
LEFT JOIN contacts
ON organization.org_id = contacts.org_id
OR organization.org_id = 0
WHERE organization.state
= '$search'
OR contacts.state
= '$search'
ORDER BY '$sort' ASC";

} elseif ($search != $value && $search != $key) { ##I tried || here too.
##echo $search here outputs the new value too, when applicable
$q = "
SELECT
CONCAT(contacts.firstname,' ',contacts.lastname) AS fullname,
organization.*, contacts.*
FROM organization
LEFT JOIN contacts
ON organization.org_id = contacts.org_id
OR organization.org_id = 0
WHERE organization.state
LIKE '%$search%'
OR organization.orgname
LIKE '%$search%'
OR contacts.state
LIKE '%$search%'
OR contacts.lastname
LIKE '%$search%'
OR contacts.firstname
LIKE '%$search%'
ORDER BY '$sort' ASC";
}

$result = mysql_query($q)
or die('Query Failed: ' . mysql_error());
</snip>

Where is this evaluation breaking down and why? Thanks in advance.
--

Regards,

Jeff Gardner

Instead of the elseif you could just use else. Why check again, it's
either
a state value, or something else.

Hans

I added the elsif because I get the same output with an else. That's
why I suspect that there is a logic problem. For some reason, the second
$q variable is populated regardless of the previous conditional
statement being satisfied or not. If a state is not input, $search
remains untouched and the 2nd $q is populated as expected. If a state
is input, $search is modified but the 2nd $q is still populated.
The problem is how I am comparing $search with the array:

foreach ($states as $key =$value)

if ($search == $value || $search == $key)...

This ends up populating $search with multiple strings from the array.
The issue isn't with the logic but with my method of comparison to the
array. What's the proper way to compare strings with values within an
array? Would probably be a better question

--

Regards,

Jeff Gardner
___________________________

"Contrary to popular belief, Unix is user friendly. It just happens
to be very selective about who its friends are." --Kyle Hearn
Sep 15 '06 #5
Jerry Stuckle wrote:
Jeff Gardner wrote:
>Greetings:

I am attempting to get conditional output based on POSTed form
data. If the posted value is either the key or value of an array,
$x=key and $q=foo. elseif it is neither key nor value(I tried || too)
, $q=bar. Even though $x is properly assigned the new value, $q always
ends up being bar. Abstractions aside, here's the code: (note ##
comments added to this post)

<snip>
$search = $_POST[search];
$sort = $_POST[sortby];
$states = array(
"AL" ="Alabama",
"AK" ="Alaska",
"AZ" ="Arizona",
...
);
##echo $search here outputs the POSTed input
foreach ($states as $key =$value)

if ($search == $value || $search == $key) {
$search = $key;
##echo $search here outputs the new value, when applicable
$q = "
SELECT
organization.*, contacts.*
FROM organization
LEFT JOIN contacts
ON organization.org_id = contacts.org_id
OR organization.org_id = 0
WHERE organization.state
= '$search'
OR contacts.state
= '$search'
ORDER BY '$sort' ASC";
} elseif ($search != $value && $search != $key) { ##I tried ||
here too.
##echo $search here outputs the new value too, when applicable
$q = "
SELECT
CONCAT(contacts.firstname,' ',contacts.lastname) AS fullname,
organization.*, contacts.*
FROM organization
LEFT JOIN contacts
ON organization.org_id = contacts.org_id
OR organization.org_id = 0
WHERE organization.state
LIKE '%$search%'
OR organization.orgname
LIKE '%$search%'
OR contacts.state
LIKE '%$search%'
OR contacts.lastname
LIKE '%$search%'
OR contacts.firstname
LIKE '%$search%'
ORDER BY '$sort' ASC";
}

$result = mysql_query($q)
or die('Query Failed: ' . mysql_error());
</snip>

Where is this evaluation breaking down and why? Thanks in advance.

You're problem is you are setting $q each time through the loop, then
using it outside of the loop. $q will always have the last value set in
the loop.

If $search matched the last state in your list, you would get the value
from your "then" clause. Any other time you'll get the value from your
"then" clause.
Here's what ended up working: (note the if(){foreach(){if()}} at the
beginning) Unfortunately, in_array() and array_key_exists() are case
sensitive. I'm trying to figure out a way to get strtoupper and
ucfirst() to fix any input.

<snip>
$search = $_POST[search];
$sort = $_POST[sortby];
$states = array(
"AL" ="Alabama",
"AK" ="Alaska",
"AZ" ="Arizona",
...
);

if (in_array($search ,$states) || array_key_exists($search, $states)) {
foreach ($states as $key =$value){ if ($search == $value || $search
== $key){
$search = $key;
}}
$q = "
SELECT
organization.*, contacts.*
FROM organization
LEFT JOIN contacts
ON organization.org_id = contacts.org_id
OR contacts.org_id = 0
WHERE organization.state
LIKE '$search%'
OR contacts.state
LIKE '%$search%'
ORDER BY '$sort' ASC";

} else {
$q = "
SELECT
CONCAT(contacts.firstname,' ',contacts.lastname) AS fullname,
organization.*, contacts.*
FROM organization
LEFT JOIN contacts
ON organization.org_id = contacts.org_id
OR contacts.org_id = 0
WHERE organization.state
LIKE '%$search%'
OR organization.orgname
LIKE '%$search%'
OR contacts.state
LIKE '%$search%'
OR contacts.lastname
LIKE '%$search%'
OR contacts.firstname
LIKE '%$search%'
ORDER BY '$sort' ASC";
}
</snip>

--

Regards,

Jeff Gardner
___________________________

"Contrary to popular belief, Unix is user friendly. It just happens
to be very selective about who its friends are." --Kyle Hearn
Sep 15 '06 #6
Jeff Gardner wrote:
Jerry Stuckle wrote:
>Jeff Gardner wrote:
>>Greetings:

I am attempting to get conditional output based on POSTed form
data. If the posted value is either the key or value of an array,
$x=key and $q=foo. elseif it is neither key nor value(I tried ||
too) , $q=bar. Even though $x is properly assigned the new value, $q
always ends up being bar. Abstractions aside, here's the code:
(note ## comments added to this post)

<snip>
$search = $_POST[search];
$sort = $_POST[sortby];
$states = array(
"AL" ="Alabama",
"AK" ="Alaska",
"AZ" ="Arizona",
...
);
##echo $search here outputs the POSTed input
foreach ($states as $key =$value)

if ($search == $value || $search == $key) {
$search = $key;
##echo $search here outputs the new value, when applicable
$q = "
SELECT
organization.*, contacts.*
FROM organization
LEFT JOIN contacts
ON organization.org_id = contacts.org_id
OR organization.org_id = 0
WHERE organization.state
= '$search'
OR contacts.state
= '$search'
ORDER BY '$sort' ASC";
} elseif ($search != $value && $search != $key) { ##I tried ||
here too.
##echo $search here outputs the new value too, when applicable
$q = "
SELECT
CONCAT(contacts.firstname,' ',contacts.lastname) AS fullname,
organization.*, contacts.*
FROM organization
LEFT JOIN contacts
ON organization.org_id = contacts.org_id
OR organization.org_id = 0
WHERE organization.state
LIKE '%$search%'
OR organization.orgname
LIKE '%$search%'
OR contacts.state
LIKE '%$search%'
OR contacts.lastname
LIKE '%$search%'
OR contacts.firstname
LIKE '%$search%'
ORDER BY '$sort' ASC";
}

$result = mysql_query($q)
or die('Query Failed: ' . mysql_error());
</snip>

Where is this evaluation breaking down and why? Thanks in advance.


You're problem is you are setting $q each time through the loop, then
using it outside of the loop. $q will always have the last value set
in the loop.

If $search matched the last state in your list, you would get the
value from your "then" clause. Any other time you'll get the value
from your "then" clause.
Here's what ended up working: (note the if(){foreach(){if()}} at the
beginning) Unfortunately, in_array() and array_key_exists() are case
sensitive. I'm trying to figure out a way to get strtoupper and
ucfirst() to fix any input.

<snip>
$search = $_POST[search];
$sort = $_POST[sortby];
$states = array(
"AL" ="Alabama",
"AK" ="Alaska",
"AZ" ="Arizona",
...
);

if (in_array($search ,$states) || array_key_exists($search, $states)) {
foreach ($states as $key =$value){ if ($search == $value ||
$search == $key){
$search = $key;
}}
$q = "
SELECT
organization.*, contacts.*
FROM organization
LEFT JOIN contacts
ON organization.org_id = contacts.org_id
OR contacts.org_id = 0
WHERE organization.state
LIKE '$search%'
OR contacts.state
LIKE '%$search%'
ORDER BY '$sort' ASC";

} else {
$q = "
SELECT
CONCAT(contacts.firstname,' ',contacts.lastname) AS fullname,
organization.*, contacts.*
FROM organization
LEFT JOIN contacts
ON organization.org_id = contacts.org_id
OR contacts.org_id = 0
WHERE organization.state
LIKE '%$search%'
OR organization.orgname
LIKE '%$search%'
OR contacts.state
LIKE '%$search%'
OR contacts.lastname
LIKE '%$search%'
OR contacts.firstname
LIKE '%$search%'
ORDER BY '$sort' ASC";
}
</snip>
Hmmm, not easy to make a case insensitive search. The keys are already
upper case; maybe change the values to all lower case then if you find
it use ucwords() to uppercase the first char of each word?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Sep 15 '06 #7

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

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
29
by: RAY | last post by:
Hi , my boss has asked I sit in on an interview this afternoon and that I create some interview questions on the person's experience. What is C++ used for and why would a company benefit from...
51
by: WindAndWaves | last post by:
Can anyone tell me what is wrong with the goto command. I noticed it is one of those NEVER USE. I can understand that it may lead to confusing code, but I often use it like this: is this...
56
by: Cherrish Vaidiyan | last post by:
Frinds, Hope everyone is doing fine.i feel pointers to be the most toughest part in C. i have just completed learning pointers & arrays related portions. I need to attend technical interview on...
17
by: vishal | last post by:
I am new to sql and require some help on cursors? what are they and how and why are they used for??? it will be kind enough if anyone helps me in this regards.. regards vishal jain.
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
0
by: Steve | last post by:
I have a gridview which uses an objectdatasource for its select and delete. The delete command uses the function below. The delete itself works but the extra logic which requires parameters...
23
by: marora | last post by:
I keep hearing about not to use 'go_to' in your code. However, I don't know what's the reasoning behind it? Can any one here shed some light? Thanks in advance, Manish
6
by: dr | last post by:
does anyone see what is wrong with my .gif download logic? no matter what image url i try it downloads a junk file that can't be opened in any paint program. System.Net.WebRequest myRequest =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...
0
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
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 projectplanning, coding, testing,...

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.