Solved

Loop question

Posted on 2007-12-03
7
141 Views
Last Modified: 2013-12-13
I have the following code:

 <?php
                  for ($i =0, $t = time(); $i< 7; ++$i) {
                      echo strftime('<li class = "inline">%a <br /><a href="http://calendar.site.com/app.php?tab=events&query=&site=name&tpl=EventSearchAllESD&SearchKeyword=&SearchDate=%Y%m%d&SearchDateEnd=%Y%m%d&SearchLocation=&SearchPrice=&SearchCategory"> %d</a>', $t+$i*3600*24) ."</li>";
                  }
       ?>

the code just produces 7 different list items and the appropriate date string in the URL

I need to modify the code to display 7 different div's and I am having trouble

<!-- THIS IS THE FIRST DIV - ID = 0 -->
            <div class="square" id="day0">
                  <a href="href="http://calendar.site.com/app.php?tab=events&query=&site=name&tpl=EventSearchAllESD&SearchKeyword=&SearchDate=%Y%m%d&SearchDateEnd=%Y%m%d&SearchLocation=&SearchPrice=&SearchCategory">
            </div>

<!-- THIS IS THE SECOND DIV - ID = 1 -->
            <div class="square" id="day1">
                  <a href="href="http://calendar.site.com/app.php?tab=events&query=&site=name&tpl=EventSearchAllESD&SearchKeyword=&SearchDate=%Y%m%d&SearchDateEnd=%Y%m%d&SearchLocation=&SearchPrice=&SearchCategory">
            </div>

<!-- THIS IS THE THIRD DAY DIV - ID = 2 -->
            <div class="square" id="day2">
                  <a href="href="http://calendar.site.com/app.php?tab=events&query=&site=name&tpl=EventSearchAllESD&SearchKeyword=&SearchDate=%Y%m%d&SearchDateEnd=%Y%m%d&SearchLocation=&SearchPrice=&SearchCategory">
            </div>


this would go 7 days all the way to div id = 6

the SearchDate=%Y%m%d&SearchDateEnd=%Y%m%d& in the URL would also have to reflect the proper date.

Can anyone help me out with this?
0
Comment
Question by:lvollmer
  • 4
  • 3
7 Comments
 
LVL 50

Expert Comment

by:Steve Bink
ID: 20399866
Just some small changes to your existing code:


<?php

  $searchdate=date('ymd',$_GET['SearchDate']);

  $searchenddate=date('ymd',$_GET['SearchEndDate']);

  for ($i =0, $t = time(); $i< 7; ++$i) {

    printf("<div class=\"square\" id=\"day%u\"><a href=\"http://calendar.site.com/app.php?tab=events&query=&site=name&tpl=EventSearchAllESD&SearchKeyword=&SearchDate=%s&SearchDateEnd=%s&SearchLocation=&SearchPrice=&SearchCategory\"></div>",$i,$searchdate,$searchenddate);

  }

?>

Open in new window

0
 
LVL 50

Expert Comment

by:Steve Bink
ID: 20399872
I forgot the link display text and the </a> to close the tag, but you should get the idea from that.
0
 

Author Comment

by:lvollmer
ID: 20399902
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 50

Expert Comment

by:Steve Bink
ID: 20399978
>>> [ ... ] SearchDate=691231 [ ... ]

That date is coming up as December 31, 1969.  Bad date parsing, is all.  Alter the code so that it is expecting a 4-digit year, and the fix below should work.  Note that the strtotime() call is redundant, but it does provide some basic conformity to the input data when it prints out the links.


<?php

  $searchdate=date('Ymd',strtotime($_GET['SearchDate']));

  $searchenddate=date('Ymd',strtotime($_GET['SearchEndDate']));

  for ($i =0, $t = time(); $i< 7; ++$i) {

    printf("<div class=\"square\" id=\"day%u\"><a href=\"http://calendar.site.com/app.php?tab=events&query=&site=name&tpl=EventSearchAllESD&SearchKeyword=&SearchDate=%s&SearchDateEnd=%s&SearchLocation=&SearchPrice=&SearchCategory\">link $i</a></div>",$i,$searchdate,$searchenddate);

  }

?>

Open in new window

0
 

Author Comment

by:lvollmer
ID: 20404398
I'm still getting 1969 with that code

&SearchDate=19691231&SearchDateEnd=19691231

also, the links are supposed to bring up the appropriate date

ie: if today is 12/04/2007

link0= &SearchDate=2007/12/04&SearchDateEnd=2007/12/04
link1= &SearchDate=2007/12/05&SearchDateEnd=2007/12/05
link2= &SearchDate=2007/12/06&SearchDateEnd=2007/12/06


etc..
0
 

Author Comment

by:lvollmer
ID: 20404431
sorry I was reffering to the other snippet of code. the last code you gave me doesnt provide any results at all
0
 
LVL 50

Accepted Solution

by:
Steve Bink earned 500 total points
ID: 20406833
I misunderstood what the loop was for.  Here's the revised code using the loop counter to construct the date, with the formatting you wanted from your last post:


<?

  $z = time();

  for ($i =0; $i< 7; ++$i) {

    $thedate = date("Y/m/d", $z + ($i*86400));

    printf("<div class=\"square\" id=\"day%u\"><a href=\"http://calendar.site.com/app.php?tab=events&query=&site=name&tpl=EventSearchAllESD&SearchKeyword=&SearchDate=%s&SearchDateEnd=%s&SearchLocation=&SearchPrice=&SearchCategory\">link $i</a></div>",$i,$thedate,$thedate);

  }

?>

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

930 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now