Gossamer Forum
Home : Products : DBMan : Customization :

search against DateAdded field

Quote Reply
search against DateAdded field
I want to build a date search field just like the one used in this forum. The field user can choose is dropdown list of how new the records are, such as one day, one week, one month old. The selected number of days will be used to search records against the DateAdded field.

Thanks in advance.

Long

Quote Reply
Re: search against DateAdded field In reply to
First, I'm assuming you understand you'll have to have a separate subroutine for your search form.

Next, if you haven't already, you need to replace sub get_date with the one that's at http://www.jpdeni.com/dbman/Mods/changes.txt.

Then you would need to add the following to your search form subroutine. Make sure you close off any print statements before you enter the code.

Code:

$time1 = time();
$day1 = &get_date($time1 - (86400 *2));
$day2 = &get_date($time1 - (86400 *3));
$week1 = &get_date($time1 - (86400 * 8));
$week2 = &get_date($time1 - (86400 * 15));
$week3 = &get_date($time1 - (86400 * 22));
$month1 = &get_date($time1 - (86400 * 31));
$month2 = &get_date($time1 - (86400 * 61));
$month3 = &get_date($time1 - (86400 * 91));
$month6 = &get_date($time1 - (86400 * 181));
$year1 = &get_date($time1 - (86400 * 366));

print qq|<input type="select" name="Date-gt">
<option value="$day1">Newer than 1 day</option>
<option value="$day2">Newer than 2 days</option>
<option value="$week1">Newer than 1 week</option>
<option value="$week2">Newer than 2 weeks</option>
<option value="$week3">Newer than 3 weeks</option>
<option value="$month1">Newer than 1 month</option>
<option value="$month2">Newer than 2 months</option>
<option value="$month3">Newer than 3 months</option>
<option value="$month6">Newer than 6 months</option>
<option value="$year1">Newer than 1 year</option>
<option value="---">All dates</option>|;
If you should want to add more options, just follow the pattern. Be sure you mulitply 86400 times one more day than you want to look for.

Also, be sure you change Date to the actual name of your date field.

You may want to have one selected by default. I would select the last one, but you can select any one you want.

Be aware that, if the search fails, the previous value will not be selected within the field.

JPD
http://www.jpdeni.com/dbman/
Quote Reply
Re: search against DateAdded field In reply to
Hi, JPDeni

Thank you very much for your reply and I am happy you are back.

I have tried your code and found it didnot work. the problem is no matter what date I selected, like newer than one day, one month,..., the date part in the URLs returned were always the current date (like this: &DateAdded-gt=27-Jul-2000). What could have been wrong? I changed the field name to that used in my db. I run dbman on my win98 and I have set up a new search subroutine and change the sub get_date to this:

my ($time1) = $_[0];
($time1) or ($time1 = time());
my ($sec, $min, $hour, $day, $mon, $year, $dweek, $dyear, $daylight) = localtime($time1);
my (@months) = qw!Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec!;
($day < 10) and ($day = "0$day");
$year = $year + 1900;

return "$day-$months[$mon]-$year";
}
------------------------------
the following is the the code used in search routine:

$time = time();
$day1 = &get_date($time1 - (86400 *2));
$day2 = &get_date($time1 - (86400 *3));
$week1 = &get_date($time1 - (86400 * 8));
$week2 = &get_date($time1 - (86400 * 15));
$week3 = &get_date($time1 - (86400 * 22));
$month1 = &get_date($time1 - (86400 * 31));
$month2 = &get_date($time1 - (86400 * 61));
$month3 = &get_date($time1 - (86400 * 91));
$month6 = &get_date($time1 - (86400 * 181));
$year1 = &get_date($time1 - (86400 * 366));
print qq|<select name="DateAdded-gt">
<option value="$day1">Newer than 1 day</option>
<option value="$day2">Newer than 2 days</option>
<option value="$week1">Newer than 1 week</option>
<option value="$week2">Newer than 2 weeks</option>
<option value="$week3">Newer than 3 weeks</option>
<option value="$month1">Newer than 1 Month</option>
<option value="$month2">Newer than 2 months</option>
<option value="$month3">Newer than 3 months</option>
<option value="$month6">Newer than 6 months</option>
<option value="$year1">Newer than 1 year</option>
<option value="---" selected>All dates</option>
</select>|;
}

Quote Reply
Re: search against DateAdded field In reply to
My apology. I made an error.

I entered it in the code above, but here it is again.

$time = time();

needs to be

$time1 = time();

Now it should work.

JPD
http://www.jpdeni.com/dbman/
Quote Reply
Re: search against DateAdded field In reply to
Actually I already tried this, it made no difference. what else could be wrong?

Long

Quote Reply
Re: search against DateAdded field In reply to
I don't know. It made a difference when I ran it through Perl on my computer. It gave me just what you would expect.

The only other thing I can think to do would be to copy the code from sub get_date for each of the lines. Something like

my (@months) = qw!Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec!;
$time1 = time();
my ($sec, $min, $hour, $day, $mon, $year, $dweek, $dyear, $daylight) = localtime($time1 - (86400 *2));
($day < 10) and ($day = "0$day");
$year = $year + 1900;
$day1 = "$day-$months[$mon]-$year";
my ($sec, $min, $hour, $day, $mon, $year, $dweek, $dyear, $daylight) = localtime($time1 - (86400 *3));
($day < 10) and ($day = "0$day");
$year = $year + 1900;
$day2 = "$day-$months[$mon]-$year";
my ($sec, $min, $hour, $day, $mon, $year, $dweek, $dyear, $daylight) = localtime($time1 - (86400 *8));
($day < 10) and ($day = "0$day");
$year = $year + 1900;
$week1 = "$day-$months[$mon]-$year";

and so on.

That works, too, but it's a lot of coding.

JPD
http://www.jpdeni.com/dbman/
Quote Reply
Re: search against DateAdded field In reply to
Thanks JPDeni, It works fine. Although it mean more codes but much better than none.

Thanks a lot!

Long