Solved

Loop question

Posted on 2007-12-03
7
142 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

773 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