Solved

Loop question

Posted on 2007-12-03
7
145 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

680 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