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