Fetching an optional array from an optional column

2002-03-15 Thread phplist

I'm working on a form that queries by selection in PHP, where a user
selects a subject and an optional selection of geographic region to get
information. Each database entry will have 2 subject fields, Subject 1
being the main subject and Subject 2 being the cross-subject, and a
geographic field, which is optional. A table is set up like this:

+--+--+--+--+--+-+
| ID   | Organization | URL  | SUBJECT1 | SUBJECT2 | Geographic  |
+--+--+--+--+--+-+
|  1   | Acme | www  |  Math|  English | Canada  |
|  2   | Loony Toons  | www  |  Comedy  |  Math| Brazil  |

...


The idea is that the query will check the database to see if $Subject
has a match in either Subject1 or Subject2. The geographic is an
optional selection. If I select a country, then that's fairly simple.
However, if I select Math as a subject, and left the Geographic option
unselected, I want it to select all records that has a match in either
Subject1 and Subject2, irregardless of the Geographic option.

I tried the following query:

SELECT * FROM links WHERE (SUBJECT1='Legislation' OR
SUBJECT2='Legislation') AND GEOGRAPHIC = ''

But it won't find the above records because the geographic is not a
blank field.

I could drop the .. AND GEOGRAPHIC part, but then if someone does
select a country, it won't take country into consideration. How do I get
it to select all records based on the subject, irregardless of the
country?

Thanks in advance,

Lmlweb



-
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/   (the list archive)

To request this thread, e-mail [EMAIL PROTECTED]
To unsubscribe, e-mail [EMAIL PROTECTED]
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php




FW: Fetching an optional array from an optional column

2002-03-15 Thread phplist


Thank you for the hint.

I've tried further, and still came up short. This time, if I use the % -
it returns nothing.

Here's the sample code I'm using:

?php

if (!subject) {
header(Location: nowhere.html);
exit;
}

// Connects to database
include(cnx_db.inc);

// First addslashes to text:
$formatted_subject = addslashes($subject);
// Formats Query
$sql = SELECT * FROM links WHERE (SUBJECT1='$formatted_subject' OR
SUBJECT2='$formatted_subject') AND GEOGRAPHIC='$geographic%'
 ORDER BY ORGANIZATION ASC;

print($sql);

$sql_result = mysql_query($sql);
if (!$sql_result) {
   echo Can't execute $sql  . mysql_error();
   exit;
}

// organizes data in an orderly manner (ie bulleted area)
while ($row = mysql_fetch_array($sql_result)) {

$esc_organization = $row[ORGANIZATION];
$esc_desc = $row[DESCRIPTION];
$esc_url = $row[URL];
$esc_subject1 = $row[SUBJECT1];
$esc_subject2 = $row[SUBJECT2];
$esc_geographic = $row[GEOGRAPHIC];

$organization = stripslashes($esc_organization);
$description = stripslashes($esc_desc);
$url = stripslashes($esc_url);
$subject1 = stripslashes($esc_subject1);
$subject2 = stripslashes($esc_subject2);
$geographic = stripslashes($esc_geographic);

$option_block .= libOrganization:/b a
href=\http://$url\;$organization/abrbGeographic Region:/b
$geographicbrbDescription:/b $descriptionbrbURL:/b a
href=\http://$url\;$url/a/librbSubject1:/b
$subject1brbSubject2:/b $subject2\n; } include(byebye.inc);

?
!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN


html
head
titleTest Result/title
/head

body
h1?php echo $subject; ?/h1
You have chosen ?php echo $subject; ?. Here are the results: Ul
?php echo $option_block; ? /UL

/body
/html

The printed SQL comes out like this: 
SELECT * FROM links WHERE (SUBJECT1='Legislation' OR
SUBJECT2='Legislation') AND GEOGRAPHIC='%' ORDER BY ORGANIZATION ASC 

Any ideas? Again, thanks for your help

 -Original Message-
 From: Gurhan Ozen [mailto:[EMAIL PROTECTED]]
 Sent: Friday, March 15, 2002 4:18 PM
 To: [EMAIL PROTECTED]; [EMAIL PROTECTED]
 Subject: RE: Fetching an optional array from an optional column
 
 
 Hi,
 
 Since you are using a form to query the table i assume the
 subject1, subject and geographic will be variables. So, you 
 could do something like:
 
  SELECT * FROM links WHERE (subject1='$sub1' OR
 subject2='$sub2') AND GEOGRAPHIC like '$geo%'
 
  In this way, if the user enters no value for the variable
 $geo then the query statement will query with (WHERE 
 geographic like '%' ) clause which will mean any country.. On 
 the other hand, if the user enters  a country name for the 
 $geo , say Canada, then the query statement will query with 
 (WHERE geopgrahic like 'Canada%') clause which will hit the 
 rows with Canada.
 
 Gurhan
 
 
 -Original Message-
 From: phplist [mailto:[EMAIL PROTECTED]]
 Sent: Friday, March 15, 2002 7:01 PM
 To: [EMAIL PROTECTED]
 Subject: Fetching an optional array from an optional column
 
 
 I'm working on a form that queries by selection in PHP, where
 a user selects a subject and an optional selection of 
 geographic region to get information. Each database entry 
 will have 2 subject fields, Subject 1 being the main subject 
 and Subject 2 being the cross-subject, and a geographic 
 field, which is optional. A table is set up like this:
 
 +--+--+--+--+--+-+
 | ID   | Organization | URL  | SUBJECT1 | SUBJECT2 | Geographic  |
 +--+--+--+--+--+-+
 |  1   | Acme | www  |  Math|  English | Canada  |
 |  2   | Loony Toons  | www  |  Comedy  |  Math| Brazil  |
 
 ...
 
 
 The idea is that the query will check the database to see if
 $Subject has a match in either Subject1 or Subject2. The 
 geographic is an optional selection. If I select a country, 
 then that's fairly simple. However, if I select Math as a 
 subject, and left the Geographic option unselected, I want it 
 to select all records that has a match in either Subject1 and 
 Subject2, irregardless of the Geographic option.
 
 I tried the following query:
 
 SELECT * FROM links WHERE (SUBJECT1='Legislation' OR
 SUBJECT2='Legislation') AND GEOGRAPHIC = ''
 
 But it won't find the above records because the geographic is
 not a blank field.
 
 I could drop the .. AND GEOGRAPHIC part, but then if
 someone does select a country, it won't take country into 
 consideration. How do I get it to select all records based on 
 the subject, irregardless of the country?
 
 Thanks in advance,
 
 Lmlweb
 
 
 
 -
 Before posting, please check:
http://www.mysql.com/manual.php   (the manual)
http://lists.mysql.com/   (the list archive)
 
 To request this thread, e-mail [EMAIL PROTECTED