Hello, I'm a newb seeking help, I first posted this in the newbie section, but nobody seemed to help(so, sorry in advanced for double posting.). Anyways, I'm working on a scheduling system that uses a visual calendar. I've already got the calendar working, but I need to make a dynamic array in some sort of loop. The purpose of the array is to tell the calendar which days of the month are a clickable link. THIS array will have nothing to do with a SQL query., I will later use another array for some of the days of the month, that have events on them. I can establish the array statically, but it needs to be dynamic of each day.

Static Code:


include_once('calendar.php');
//the days I want to have a href link. if today was the 2nd, every day from 2nd - 31st of the month should be a link.
$days = array(
        2=>array('schedule.php?item=day&value=2','linked-day'),
        3=>array('schedule.php?item=day&value=3','linked-day'),
        4=>array('schedule.php?item=day&value=4','linked-day'),
        5=>array('schedule.php?item=day&value=5','linked-day'),
// etc.
)

$time = time();

 setlocale(LC_TIME, 'en_US');
//generate the calendar, and pass the $days array through as an argument.
    echo generate_calendar(date('Y', $time), date('n', $time), $days, 3, NULL, 0, $pn);

Dynamic Code in need of help:


include_once('calendar.php');

//how many days are in the month? in this case, 31
$lastday = mktime(0,0,0,date("m"),date("t"),date("Y"));
$lastday = strftime("%d",$lastday);

//current day the code is executed.
$day = date('j',mktime(0,0,0,date("m"),date("d"),date("Y")));

//declaring the array
$days[] = array();

//crazy mess of a loop that tries to get the job done, and I know it won't. This is where I need help.
for (;$day<=$lastday;$day++){
array_push($days[$day], array_push($day=array("schedule.php?item=day&value=[$day]", 'linked-day')));
}


$time = time();

 setlocale(LC_TIME, 'en_US');
//generate the calendar, and pass the $days array through as an argument.
    echo generate_calendar(date('Y', $time), date('n', $time), $days, 3, NULL, 0, $pn);

Bottom-Line Question:
How can I generate an array using a loop? or something similar?

I'm new to php and programming in general, I literally picked up a book last week, and I have the general theory of programming. I have a CMS based site (Joomla), and I plan to implement a Scheduling System and & Calendar into it, but for now, I just need some help getting those linked-days generated :o
Any help is greatly appreciated 😃 Ask me any questions necessary, and I will answer quickly. Thanks in advanced.
-Jason

    $lastday = mktime(0,0,0,date("m"),date("t"),date("Y"));
    $lastday = strftime("%d",$lastday);
    
    $day = date('j',mktime(0,0,0,date("m"),date("d"),date("Y")));
    
    $link_days=array();
    
    for ($i=$day ; $i<$lastday ; $i++){
    	$link_days[] = array('schedule.php?item=day&value='.$i ,'linked-day');
    }
    

      You're a genius. lol
      definitely works, it creates the array almost exactly how I need it.

       $day = date('j',mktime(0,0,0,date("m"),date("d"),date("Y")));
      			$days[] = array();
      			for (;$day<=$lastday ; $day++){
      			    $days[$day] = array('schedule.php?item=day&value='.$day ,'linked-day');
      			}  

      absolutely perfect. I appreciate you answering my post very much... I was pulling my hair out. 🙂

        surly not a genius,
        but glad to help

          No, I'm the surly one.

          You've got a few redundancies.

          $lastday = mktime(0,0,0,date("m"),date("t"),date("Y"));
          $lastday = strftime("%d",$lastday); 

          This doesn't get you anything different from what date('t') got you in the first place.

          $day = date('j',mktime(0,0,0,date("m"),date("d"),date("Y"))); 

          This wouldn't get you anything different from date('j') itself (midnight today and right now are still the same day).

          The whole array of days could have been built with

          range(date('j'), date('t'));

          And then the links could then be built

          $days = array();
          foreach(range(date('j'), date('t')) as $day)
              $days[] = array("schedule.php?item=day&value={$day}linked-day");
          

          And is there any particular reason why each link gets its very own array?

            Write a Reply...