Solved

Loop question

Posted on 2007-12-03
7
140 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

757 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

17 Experts available now in Live!

Get 1:1 Help Now