Solved

Loop with time

Posted on 2001-06-11
13
261 Views
Last Modified: 2013-12-24
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>
0
Comment
Question by:J2J
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
13 Comments
 
LVL 4

Expert Comment

by:CF_Spike
ID: 6177620
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
0
 

Author Comment

by:J2J
ID: 6178022
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?



0
 

Author Comment

by:J2J
ID: 6178051
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>
0
Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

 
LVL 4

Expert Comment

by:CF_Spike
ID: 6178054
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
0
 

Author Comment

by:J2J
ID: 6178071
another two query is the same as testing1, just another testing2, testing3
and they are also put inside the loop.
0
 
LVL 4

Expert Comment

by:CF_Spike
ID: 6178107
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
0
 

Author Comment

by:J2J
ID: 6179485
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
0
 
LVL 4

Expert Comment

by:CF_Spike
ID: 6179596
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
0
 

Author Comment

by:J2J
ID: 6179797
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.
0
 
LVL 4

Expert Comment

by:CF_Spike
ID: 6180894
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
0
 

Author Comment

by:J2J
ID: 6197492
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?
0
 

Accepted Solution

by:
JYnet earned 35 total points
ID: 6205935
in your case, using two variable is inefficient,
use one variable is more likely to be useful.
the following code might be suitable:

<CFSET thisTime1 = CreateDateTime(0,0,0,10,0,0)>
<CFLOOP CONDITION="thisTime1 LT CreateDateTime(0,0,0,20,30,0)">


  here is the loop body...


<CFSET thisTime1=dateadd('n',30,thisTime1)>
</cfloop>

also, not to forget to change <CFIF DateCompare(thisTime1,VIEWApp1.time_start,'h') EQ 0>
to <CFIF DateCompare(thisTime1,VIEWApp1.time_start,'n') EQ 0>
because the time need to match until minute.

hope this will help.
0
 

Author Comment

by:J2J
ID: 6209083
i think it does work. thanks!
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This is a guide to setting up a new WHM/cPanel Server to be used for web hosting accounts. It is intended for web hosting company administrators and dedicated server owners. For under $99 per month (considering normal rate of Big Data Cetnters like …
When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question