Link to home
Start Free TrialLog in
Avatar of J2J
J2J

asked on

Loop with time

hai, I need a loop function that use "time" as a counter.
eg. it will show as follow:

start from 10:00
10:00
10:30
11:00
11:30
...
until 20:00

how to make this function??

i have an example, but cannot work:

<cfset timeform = CreateTime(10,00,00)>
<cfset time= #TimeFormat(timeform, 'hh:mm')#>
<CFLOOP CONDITION="time less than or equal to '20:00'">
<cfoutput>
<tr>
<th>#time#</th>
</tr>
</cfoutput>
<cfset addedvar =#TimeFormat(dateadd('n',30,time), 'hh:mm')#>
<cfset time = addedvar>
</cfloop>
Avatar of CF_Spike
CF_Spike

I'm not sure why you would want to do this. You will be absolutely killing the machine that the script runs on as it will be permanently using all available processor cycles to evaluate the loop.

If you can give us some more information about why you need this we can probably suggest a better strategy.

Spike
Avatar of J2J

ASKER

i see. so that's why my program run very slow and i say very very slow...

actually, i used this is for the purpose of matching time.
since i have three query to go for. each query has time variable.

i need the loop to cover at the outer, and the three query inside it.
if the time inside the query match the time of the loop,
it will display the data.

i have an example using number as variable (time change to num), and it work fine.
but when i intend to use time variable, the system goes very slow...

any suggestion for my problem?



Avatar of J2J

ASKER

here is the source code of my example:

<html>
<head>     <title>Untitled</title></head>

<body>testing preview
<cfquery name="testing1" datasource="DB">
 select * from testing where room = 'A'
</cfquery>

<table>
<tr> <th>TIME</th> <th>ROOM</th></tr>

<CFLOOP INDEX="Count"  FROM="1"  TO="20"  STEP="+1">
<tr>
<cfoutput>
   <th>#Count#</th>
</cfoutput>
     
<cfoutput query="testing1">
 <cfif (testing1.time eq Count)>
       <th>#testing1.name#</th>
 </cfif>
</cfoutput>
</tr>
</cfloop>
</table>

</body>
</html>
I sort of understand what you are doing, but can you post the code for the page with the queries so I can get a clearer idea of exactly what you are trying to do.

Spike
Avatar of J2J

ASKER

another two query is the same as testing1, just another testing2, testing3
and they are also put inside the loop.
um...

So where do you need to use the loop you originally asked about?

Also, there is only 1 query in this code, you mentioned that you have 3. Where are the others?

<CFLOOP CONDITION="time less than or equal to '20:00'">

Are you trying to only display data up until the current time?

Can you post the code that is causing the server to slow down, as the example doesn't really show me why you want to use the time variable.

Spike
Avatar of J2J

ASKER

let's make it simple.
the source code for my example is already posted, the only different is the other two
query testing2 and testing3 is quite the same, only different in condition.

as u can see, the following loop has been used for looping in the example:

   <CFLOOP INDEX="Count"  FROM="1"  TO="20"  STEP="+1">

but it can't work for the "time", so i decided to change it into:

   <CFLOOP CONDITION="time less than or equal to '20:00'">

but it cause the system run slow...

for testing purpose, i wish the output could be like this:

10:00  abc
10:30  
11:00  cde
11:30  
12:00
...until 20:00
the only query that match the time will be display at specific row.
  sample of query: abc's time is 10:00, cde's time is 11:00
ok, So if I understand you correctly you are saying that you only want the display to show times, not that it should be created incrementally as time gets to each half hour segment.

One way of doing that is as follows:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
     <title>Untitled</title>
</head>

<body>

<CFOUTPUT>

     <CFLOOP FROM="10" TO="20" INDEX="i">
     
          #TimeFormat(CreateTime(i,0,0),"HH:mm")#
         
          <BR>
         
          <CFIF i LT 20>
               
               #TimeFormat(CreateTime(i,30,0),"HH:mm")#<BR>
         
          </CFIF>
         
     </CFLOOP>

</CFOUTPUT>

</body>
</html>

This only displays the times. It doesn't do anything with the query results.

Spike
Avatar of J2J

ASKER

well, that's only a little part of what i meant.
true that i need a column at leftside to display the time,
but not forget that i also need a counter, eg "time".
i think in ur coding that "i" can be function like "time"

another problem is the query timeformat,
inside the Access db is "10:00"
but after the query, it display "1899-12-30 10:00:00"
how to solve this? if not, it won't match with the "i"


thank you.
The data will display as 10:00 in Access because Access is being _helpful_. The field is actually a datetime field. you can use DateCompare() to compare dates. If you want to construct a date from the i in the loop you can do the following:

<CFSET thisTime = CreateDateTime(0,0,0,i,0,0)>

And for the half hours:

<CFSET thisTime = CreateDateTime(0,0,0,i,30,0)>

You could then use DateCompare as follows:

<CFOUTPUT Query="testing1">
<CFIF DateCompare(thisTime,testing1.time,'h') EQ 0>
#testing1.somecolumn#
</CFIF>
</CFOUTPUT>

Spike
Avatar of J2J

ASKER

Almost completed, but still one problem:

source code:
<CFLOOP from="10" to="20" index="i">
  <CFSET thisTime1 = CreateDateTime(1899,12,30,i,0,0)>
  <CFSET thisTime2 = CreateDateTime(1899,12,30,i,30,0)>

  <cfoutput>
    <tr><th>#TimeFormat(thisTime1,"HH:mm")#<BR></th>
    <CFIF i LT 20>
      <tr><th>#TimeFormat(thisTime2,"HH:mm")#</th><BR>
    </CFIF>
  </cfoutput>
               
  <CFOUTPUT Query="VIEWApp1">
    <CFIF DateCompare(thisTime1,VIEWApp1.time_start,'h') EQ 0>
      <th>#VIEWApp1.cust_id#</th>
         
    <cfelseif DateCompare(thisTime2,VIEWApp1.time_start,'h') EQ 0>
      <th>#VIEWApp1.cust_id#</th>
    </CFIF>
  </CFOUTPUT>

  </tr>
</cfloop>

The result is : column correct, but row is wrong

Expected result:

10:00 abc
10:30
11:00

Actual result:
10:00
10:30 abc
11:00

which means that everything "drop a row"
     if 10:00 then show at 10:30 row(wrong)
     if 11:00 then show at 11:30 row(wrong)

     but if 10:30, it show at the correct row
     why this happen?
ASKER CERTIFIED SOLUTION
Avatar of JYnet
JYnet

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of J2J

ASKER

i think it does work. thanks!