Forums

PHP

This topic is locked

Conditional Region by date?

Posted 13 Dec 2001 20:35:11
1
has voted
13 Dec 2001 20:35:11 Peter R posted:
I'm trying to use this extension to display an image depending on the current date and a field in my db called "end_date".

Basically I have ads in a database with a field named "end_date" which holds the date at which the ad expires and no longer is visible to the public. I want to have an image that reads "Only 3 Days Left!" display when the "end_date" is 3 days away from the current date. I would then apply a similar condional region to three other images - "2 days left" - "1 day left" - "last day".

I haven't figured out how to do this yet using the Condional Region extension. I think I should be using the "DB Field and Script" option. Then choose my Recordset - then select my Field "end_date" - then choose the Condition "Greater than or Equals" - but then I'm not sure what Script I
need in the bottom box to accomplish what I want. I've tried all kinds of code snippets I've found around but none work properly.

Does anyone know what I can enter into the Script box to make the conditional region display an image when the "end_date" is specifically a certain number of days away like I mentioned earlier?

I'm using PHP and MySQL.

Thank you all.

Peter

Edited by - joelmartinez on 14 Dec 2001 00:14:54

Replies

Replied 14 Dec 2001 00:14:15
14 Dec 2001 00:14:15 Joel Martinez replied:
This would be a better question to ask in the PHP forum...

******Moved it*********

Joel Martinez [ ]
----------
E-Commerce Concepts with Ultradev...pre-order yours at
www.basic-ultradev.com/ecomm_concepts/

Edited by - joelmartinez on 14 Dec 2001 00:16:28
Replied 14 Dec 2001 11:50:35
14 Dec 2001 11:50:35 Tim Green replied:
Dates are always tricky to work with in this way. The only way I have been able to successfully work with them while doing something like this is to store month, day and year in separate columns and then do comparisons based on each column.

In your kind of setup this would mean in fact 6 columns:-

ad_start_day - the day the ad started
ad_start_month - the month the ad started
ad_start_year - the year the ad started

ad_end_day - the day the ad ends
ad_end_month - the month the ad ends
ad_end_year - the year the ad ends

You would then need to hand code the condition like this:-

<?php
if (($RecordsetName->Fields("ad_start_month" == $RecordsetName->Fields("ad_end_month") && ($RecordsetName->Fields("ad_start_year" == $RecordsetName->Fields("ad_end_month") && (($RecordsetName->Fields("ad_start_day" + 3) == $RecordsetName->Fields("ad_end_day")) {
echo "3 days left to go!";
}
?>

Obviously change the number 3 to 2 or 1 for the other conditions. This might seem to be a longwinded way of doing it, but it really is the only way to be absolutely sure that it is all going to work.

Hope this helps.

Tim Green

Extension & PHP TalkZone Manager
<font size=1>-------------------------------------------
<i>Please read the Forum FAQ before posting
a question to this TalkZone.</i>
-------------------------------------------
www.UDzone.com : A dynamic Dreamweaver,
Ultradev and Fireworks site for developers
by developers.
-------------------------------------------</font id=size1>
Replied 14 Dec 2001 17:42:04
14 Dec 2001 17:42:04 Peter R replied:
What type of columns should they be? Just varchar will do fine? or should they be date fields? I changed your code to suit my database:

&lt;?php
if (($rssaletracker-&gt;Fields("start_month" == $rssaletracker-&gt;Fields("end_month") && ($rssaletracker-&gt;Fields("start_year" == $rssaletracker-&gt;Fields("end_year") && (($rssaletracker-&gt;Fields("start_day" + 3) == $rssaletracker-&gt;Fields("end_day")) {
echo "3 days left to go!";
}
?&gt;

But this produces no output. Right now the above fields are just varchar and char fields holding start_year "YYYY" start_month "MM" etc. But this must be wrong, because I don't see how these are being compared to the current date which is needed to tell how many days until the ad expires. I'm just not clear on this... can you tell what I'm doing wrong?

Thank you for your help so far!

Peter
Replied 14 Dec 2001 23:49:26
14 Dec 2001 23:49:26 Tim Green replied:
Sure.

These fields are probably best as being INT as they are purely numeric integers (anything else can leave you with whitespace characters etc which can cause you problems when doing the comparisons).

Unfortunately I was in a bit of a rush earlier, and I hadn't completely read your initial question (sorry about that, was busy in the office).

Anyway, here is a modified version of the code, you shouldn't now (unless you'll use the values elsewhere) keep the start_year,start_month,start_day columns in your table:-

&lt;?php
$currYear = date("Y",time());
$currMonth = date("m",time());
$currDay = date("d",time());
if (($rssaletracker-&gt;Fields("end_year" == $currYear) && ($rssaletracker-&gt;Fields("end_month" == $currMonth)) {
$period = $rssaletracker-&gt;Fields("end_day" - $currDay;
if ($period == 1) {
echo "1 day to go!";
}
}
?&gt;

Hope this helps <img src=../images/dmxzone/forum/icon_smile.gif border=0 align=middle>

Tim Green

Extension & PHP TalkZone Manager
<font size=1>-------------------------------------------
<i>Please read the Forum FAQ before posting
a question to this TalkZone.</i>
-------------------------------------------
www.UDzone.com : A dynamic Dreamweaver,
Ultradev and Fireworks site for developers
by developers.
-------------------------------------------</font id=size1>
Replied 15 Dec 2001 21:17:41
15 Dec 2001 21:17:41 Peter R replied:
Thank you very very very much! It works great!

Just a couple quick follow up questions if you have the time. Where would I put a url to display an image, instead of the text "1 day to go!"?

Is it possible to accomplish what you did, but using a DATE field instead of three seperate INT fields? Not that I don't appreciate what you've already done! Just that I use a DATE feild named "end_date" to display when the ads will be over, and to make the database drop them from the display page when they expire. As it's set up now, I'll have to enter the ads expiry date into both the end_date field, and then into the INT fields end_year, end_month, end_day. Thought maybe I could save a little data-entry time if everything ran off the end_date DATE field.

And because you've helped me out so much I'm not gonna hassle ya with my next info quest! <img src=../images/dmxzone/forum/icon_smile.gif border=0 align=middle> But could you point me to a PHP/UltraDev tutorial that explains how to make a Search - search "multiple" words? I've only been able to find ASP tutorials on this so far.

Thank you again for everything. Your little bit of code has made my day!

Peter
Replied 16 Dec 2001 01:46:39
16 Dec 2001 01:46:39 Tim Green replied:
You're very welcome Pete!

First of all let me answer your tutorial questions. Unfortunately there aren't that many tutorials out there for PHP and UltraDev at the moment but as more and more people find their way here, I'm sure that will resolve itself.

The trick with searches is really down to the SQL you use for generating your recordset. I would suggest that you use the basic recordset as a starting block and examine the actual SQL that is created for the recordset. The good thing is that if you find a tutorial that isn't PHP related you can apply the exact same principles to the PHP server model. I would suggest that you try this, and if you get stuck, then drop us a line here (though a different thread for each problem please...)

Here is an amended version of the code that will work with your end_date DATE field, and I've also incorporated the addition for an image. Hope this answers your question <img src=../images/dmxzone/forum/icon_smile_wink.gif border=0 align=middle>


&lt;?php
ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $rssaletracker-&gt;Fields("end_date", $regs);
$currYear = date("Y",time());
$currMonth = date("m",time());
$currDay = date("d",time());
if (($regs[1] == $currYear) && ($regs[2] == $currMonth)) {
$period = $regs[3] - $currDay;
if ($period == 1) {
?&gt;
<pre id=code><font face=courier size=2 id=code>
&lt;img src="oneDay.gif"&gt;
</font id=code></pre id=code>
&lt;?php
}
}
?&gt;

Tim Green

Extension & PHP TalkZone Manager
<font size=1>-------------------------------------------
<i>Please read the Forum FAQ before posting
a question to this TalkZone.</i>
-------------------------------------------
www.UDzone.com : A dynamic Dreamweaver,
Ultradev and Fireworks site for developers
by developers.
-------------------------------------------</font id=size1>

Reply to this topic