[PHP] Re: SQL select

2003-07-10 Thread sven
hi,

JiøîÆèî eË wrote:
 hello,

 i have got a problem with SQL select:

 I have got a table such this:

 id_k typ name id
  1   f   bla1   1
  2   f   bla2   1
  2   i   bla3   1
  3   z   bla4   1
  3   f   bla5   1
  4   i   bla6   1
  4   z   bla7   1
  5   z   bla8   1

 and id = 1 and I need select these rows:

 id_k  typ nazev id
  1 f   bla1  1
  2 f   bla2  1
  3 f   bla5  1
  4 i   bla6  1

 so, when doesn'i exist component (id_k = component) type f so I want
 component with type i, but when doesn't exist type f noir i I
 don't want to select row with type z.

 jiri nemec, ICQ: 114651500
 www.menea.cz - www stránky a aplikace

don't know, if i correctly understood, but how about this:

SELECT * FROM `YourTable`
WHERE `typ`=f OR `typ`=i
ODER BY `typ`;



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Re: SQL Select on a DATE field

2003-06-06 Thread Lorenzo Ciani
Sven wrote:

 just some thoughts to your script:

Dear Sven,

Thank you for your comments. Sounds like I really have to study preg_match
and regexp!!!

-- 
Lorenzo.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Re: SQL Select on a DATE field

2003-06-05 Thread sven
just some thoughts to your script:

i don't know (yet) the pear db functions and the database you use. so i
assume you already got some logic to transform your dates into valid
sql-dates (eg. mysql: mmdd or -mm-dd). that is important for valid
results from your sql-statements as the dates must be compared somehow. (in
other words your DD/MM/YY as an example 04/05/03 can be interpreted in many
ways.)

i assume, that in your order_date-field should only stand one statement, so
i suggest using preg_match.

so for your code i would start this way:

if (preg_match($yourBetweenDatesPattern, $order_date, $matches))
{
// between two dates
// define your filter here, maybe with your qouted_date-function from
pear
}
elseif (preg_match($YourAfterDatePattern, $order_date, $matches))
{
// after a date filter
}
elseif (preg_match($YourExactDatePattern, $order_date, $matches))
{
// exact date filter
}
else
{
echo no valid date given.;
}

For your Patterns:

If you use something like this
preg_match (/between ($YourDatePattern) and ($YourDatePattern)/, $subject,
matches);
you can easily catch your date1 and date2 using:
$matches[1] and $matches[2]

hope this helps.

ciao SVEN


Lorenzo Ciani [EMAIL PROTECTED] schrieb im Newsbeitrag
news:[EMAIL PROTECTED]
 Hi all!

 I would like to ask your advice on a script that I wrote. As you can
surely
 see from my script below, I really need help as I am totally new to PHP.
 Regular expressions are my worst nightmare because I don't have a
 background in software programming: I'm just kidding around here! Please
 post your suggestions. Thanks.

 Problem
 ---
 HTML form to filter records from an SQL database on a DATE field.
 Search expressions are of the form:
DATE (should be transformed into = DATE)
= DATE (or other operator)
BETWEEN DATE_1 AND DATE_2

 The variable named $order_date contains the search expression submitted by
 the HTML form.

 Here's my script
 
 // Regex pattern for date, e.g. DD/MM/YY or MM-DD-
 $date_pat = '[0-9]+(\/|-)[0-9]+(\/|-)[0-9]+';
 // Regex pattern for SQL operators
 $op_pat = '|=|=|!=|||=';

 // First, see if there is at least one date in $_POST['order_date']
 $count = preg_match_all('/'.$date_pat.'/', $order_date, $matches);
 if ($count) {
for ($i = 0; $i = $count - 1; $i++) {
   // Quote dates using PEAR::DB
   $quoted_date[$i] = $db-quote($matches[0][$i]);
}
 }
 // Then see if search string is of type BETWEEN DATE_1 AND DATE_2
 $count = preg_match_all('/between\s'.$date_pat.'\sand\s'.$date_pat.'/i',
 $order_date, $matches);
 if ($count) {
// Yes it's a BETWEEN...AND. We assume that we have two valid dates
$date_filter = ' BETWEEN '.$quoted_date[0].' AND '.$quoted_date[1];
 } else {
// No. Then check if we have a search string like '= DATE'
$count = preg_match_all('/'.$op_pat.'/', $order_date, $matches);
if ($count) {
   // Yes, then use operator and quoted date
   $date_filter = ' '.$matches[0][0].' '.$quoted_date[0];
} else {
   // No, then we just use something like ' = DATE'
   $date_filter = ' = '.$quoted_date[0];
}
  }



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php