Jump to content

Calendar hack - 3 week event rotation


Hooch

Recommended Posts

Hello all.

I am trying to change this old script up a bit and need some help to get it 100% correct.

I'm trying to create a weekly list of 3 people and their job lineup for each of those 3 weeks.Example:Week1:Job1:LesJob2:MikeJob3:DanWeek2:Job1:DanJob2:LesJob3:MikeWeek3:Job1:MikeJob2:DanJob3:LesThen repeat the job cycle from week 1 for eternity.If you check out my results so far at LINKY you can see it is simply repeating week#3 instead of starting over as week 1.Here is my code so far:

            <?PHP            $var1 = 'Les';            $var2 = 'Dan';            $var3 = 'Mike';            $dayName  = array(1 => "Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");            $monthName = array(1 => "January","February","March","April","May","June","July","August","September","October","November","December");            $monthDays = array(1 => 31,28,31,30,31,30,31,31,30,31,30,31);            $row1 = array($var1,$var1,$var1,$var1,$var1,$var1,$var1,$var2,$var2,$var2,$var2,$var2,$var2,$var2,$var3,$var3,$var3,$var3,$var3,$var3,$var3);            $row2 = array($var2,$var2,$var2,$var2,$var2,$var2,$var2,$var3,$var3,$var3,$var3,$var3,$var3,$var3,$var1,$var1,$var1,$var1,$var1,$var1,$var1);            $row3 = array($var3,$var3,$var3,$var3,$var3,$var3,$var3,$var1,$var1,$var1,$var1,$var1,$var1,$var1,$var2,$var2,$var2,$var2,$var2,$var2,$var2);            $now = time();            $nowd = date('j', $now);            $nowm = date('F', $now);            $nowy = date('Y', $now);            $startdate = date(mktime(0,0,1,10,6,2013)); //Jan 1 2001 00:00:01            $getMonth = $_POST['month'];            if($getMonth == '')                $getMonth = date('n', $now);            $getYear = $_POST['year'];            if($getYear == '')                $getYear = date('Y', $now);                        //$getMonth = $_POST['month'];            if($getMonth == 0)            {                $getMonth = 12;                $getYear = ($getYear - 1);            }                elseif($getMonth == 13)            {                $getMonth = 1;                $getYear = ($getYear + 1);            }                else            {                $getYear = $getYear;            }                                        // if this year is divisable by 4 or 100, and is divisible by 400... then change last day of feb to 29. //            if($getMonth == 2)            {                if (($getYear%4==0)||($getYear%100==0)) {$monthDays[1]=29;}                if ($getYear%400==0) {$monthDays[1]=29;}            }                        $firstDay = date(mktime(0,0,0,$getMonth,1,$getYear)); //Jan 1 2001 00:00:01            $firstDay = date('w', $firstDay);//0 through 6 (Sun - Sat)                        if($getMonth == 1)             $buttonMonth = 'January';            if($getMonth == 2)             $buttonMonth = 'February';            if($getMonth == 3)             $buttonMonth = 'March';            if($getMonth == 4)             $buttonMonth = 'April';            if($getMonth == 5)             $buttonMonth = 'May';            if($getMonth == 6)             $buttonMonth = 'June';            if($getMonth == 7)             $buttonMonth = 'July';            if($getMonth == 8)             $buttonMonth = 'August';            if($getMonth == 9)             $buttonMonth = 'September';            if($getMonth == 10)             $buttonMonth = 'October';            if($getMonth == 11)             $buttonMonth = 'November';            if($getMonth == 12)             $buttonMonth = 'December';                        ?>                        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">            <html xmlns="http://www.w3.org/1999/xhtml">            <head>            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />            <title>Shift Schedule</title>            <link href="styleCalendar.css" rel="stylesheet" type="text/css" />            </head>            <body>                <?php include_once("analyticstracking.php") ?>                <table width="400px" align="center" border="0" cellpadding="1" cellspacing="0">                  <tr>                    <td align="right">                        <form method="POST">                            <input type="hidden" value="<?PHP echo ($getMonth);?>" name="month">                            <input type="hidden" value="<?PHP echo ($getYear-1);?>" name="year">                            <input type="submit" value="<<Year" name="submit" class="button-alt" />                        </form>                    </td>                    <td align="left">                        <form method="POST">                            <input type="hidden" value="<?PHP echo ($getMonth-1);?>" name="month">                            <input type="hidden" value="<?PHP echo ($getYear);?>" name="year">                            <input type="submit" value="<<Month" name="submit" class="button-alt" />                        </form>                    </td>                    <td align="center"><form method="POST" action="puller.php"><input type="submit" value=" Reset " name="submit" class="button-alt" /></form></td>                    <td align="right">                        <form method="POST">                            <input type="hidden" value="<?PHP echo ($getMonth+1);?>" name="month">                            <input type="hidden" value="<?PHP echo ($getYear);?>" name="year">                            <input type="submit" value="Month>>" name="submit" class="button-alt" />                        </form>                    </td>                    <td align="left">                        <form method="POST">                            <input type="hidden" value="<?PHP echo ($getMonth);?>" name="month">                            <input type="hidden" value="<?PHP echo ($getYear+1);?>" name="year">                            <input type="submit" value="Year>>" name="submit" class="button-alt" />                        </form>                    </td>                  </tr>                  <tr>                    <td colspan="5" align="center"><H1><?PHP echo $buttonMonth;?> <?PHP echo $getYear;?></H1></td>                  </tr>                </table>            <?PHP                echo '<table align="center" border="0" cellpadding="0" cellspacing="0" class="tableOutterBorder">';                echo '<tr>';                            for($i = 1; $i < 8; $i++)                    echo '<td width="120" align="center" class="weekDayHeaderCells">' . substr($dayName[$i], 0, 3) . '</td>';                              echo '</tr>';                echo '<tr>';                            $dayCount = 1;                for ($ii = 0; $ii < $firstDay; $ii++)                    $dayCount = $dayCount + 1;                if($ii >=1)                    echo '<td colspan="'.$ii.'" width="'.($ii*120).'" height="90" align="left" class="emptyCells"></td>';//Empty                            if($monthDays[1] == 29)                    $daysInMonth = 29;                else                    $daysInMonth = $monthDays[$getMonth];                                    for ($i = 1; $i <= $daysInMonth; $i++)                {                        $fDate = mktime(0,0,0,$getMonth,$i,$getYear);//Hr Mn Sec Mn Dy Yr                        $diffdate = round(($fDate - $startdate)/86400);                                            $remainder = $diffdate%28;                    $showRow1 = $row1[$remainder];                    $showRow2 = $row2[$remainder];                    $showRow3 = $row3[$remainder];                                        //Highlight today if this month is selected                    if($getYear == $nowy && $i == $nowd && $buttonMonth == $nowm)$live = "Live";                    if($showRow1 == $var1){                        $class = "dayCellsE1" . $live;                        $weekSet = 1;                        $showJobs = '<br />Puller:'.$var1.'<br>Ground:'.$var2.'<br>Loco:'.$var3;                    }elseif($showRow1 == $var2){                        $class = "dayCellsE2" . $live;                        $weekSet = 2;                        $showJobs = '<br />Puller:'.$var2.'<br>Ground:'.$var3.'<br>Loco:'.$var1;                    }else{                        $class = "dayCellsE3" . $live;                        $weekSet = 3;                        $showJobs = '<br />Puller:'.$var3.'<br>Ground:'.$var1.'<br>Loco:'.$var2;                    }                                                echo '<td width="120" height="90" valign="top" align="center" class="'.$class.'">';                    echo '<span class="text_16_white">'.$i.'</span>'.$showJobs;                    echo '</td>';                    $live = '';                    if (($i+$firstDay)%7==0&&($dayCount<$daysInMonth+1))                        echo '</tr><tr>';                }                $totCells = $firstDay + $daysInMonth;                for ($i = 0; $i < ($totCells > 28 ? ($totCells > 35 ? 42 : 35) : 28) - $totCells; $i++)                    echo '';                if($i >=1)                    echo '<td colspan="'.$i.'" width="'.($i*120).'" height="90" align="left" class="emptyCells"></td>';//Empty                echo '</tr>';                echo '</table>';            ?>            <br />            <center><a href="../" class="button-alt">ZENTYX</a></center>            </body>            </html>

Any help is greatly appreciated.

Edited by Hooch
Link to comment
Share on other sites

if you want to follow that example you posted at the top, I think we can manage to pull that off with far less code. first take a look at your example and look for the patterns. every week these three people (I'll call $workers) rotate their jobs (I'll call $shifts) using modulus and arrays, its not hard to change each worker's shift every week. also PHP has it's own Date class that handles ALLLL that code you have in figuring out the date, just for you. While you are using Date here and there, you're not really harnessing its potential and just "re-inventing the wheel".

<?php$workers = array("Dan","Mike","Les");$shifts = array("Puller","Ground","Loco");$date = new DateTime();//set the date to what the webpage requests hereif(isset($__POST['year'])&& isset($__POST['month']))    $date->setDate($__POST['year'],$__POST['month'],1);    //sets date to whats been requested, defaults to today's date$shownMonth = $date->format('m');//if sunday isn't the 1st day in the the month, prepends empty table cells // so if the first day in the month is wednesday then 3 blank cells are made// for sunday, monday, and tuesdayif($date->format('w')!=0)echo '<tr>';for($i=0;$i<$date->format('w');$i++){    ?><td></td><?php}while($date->format('m')==$shownMonth){  if($date->format('w')==0)echo '<tr>';?>  <td>    <span class="text_16_white">    <?=$date->format('j');?>    </span>  <?php for($i=0;$i<count($shifts);$i++)    echo '<br />'.$shifts[i].":".$workers[(i+intval($date->format('W'))%count($workers));  ?>    </td>  <?php if($date->format('w')==6)echo '</tr>'; $date->modify('+1 day');//iterates to the next day}//this fills out the last week if the last day in the month isn't saturdayfor($i=6;$i>$date->format('w');$i++){?><td></td><?php }if($date->format('w')!=6)echo '</tr>';?>

now... place that inside the table html like so..

<html>  <head></head>  <body>    <table>    PHP STUFF IN PREVIOUS CODE BLOCK GOES HERE    </table>  </body></html>

now the code provided isn't everything you have on your page, for example I haven't added the "if day in calender is today, use this style" you have nor the "print this month's name" but really all you need to do for each is:

$date->format('F'); //returns a string of that date's month name... Or it'll print the current month's name ("December") by default//if $date is today$today = new DateTime();if($date->format("o m d")==$today->format("o m d"))

I will be honest... its been quite a while since I actually programmed in PHP so I can't say the code is bug-proof. but the spirit should be there in full.

Edited by Hadien
Link to comment
Share on other sites

That's kind of a weird way of determining which week it is, I'm not sure what all of that code is for. If you want to determine if it is the first, second, or third week in the series, use modulus. PHP can tell you the week number of the current year, you can add additional years if you need to in order to figure out how many weeks have passed since whatever you want your starting week to be, and then just use the modulus operator to get the remainder of division by 3. The result will be 0 , 1, or 2, and that will tell you which week in the series it is.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...