Jump to content

Output calendar as planner year


bouton

Recommended Posts

I almost have it but can't get those last few inches...
I have a database of events
[code]
table instrument_planning;
id
instrument
start_date (eg 2006-01-01)
start_Month = Month(start_date)
end_date (eg 2006-11-1)
status
[/code]

Data would look like this
[code]
"instrument 1","2006-01-01","2006-01-31","Proposed"
"instrument 1","2006-02-01","2006-04-30","Funded"
"instrument 2","2006-02-01","2006-02-28","Funded"
"instrument 3","2006-01-01","2006-01-31","Proposed"
"instrument 3","2006-03-01","2006-03-31","Proposed"
[/code]

What I want it to look like is
[table]
[tr][td]Instrument[/td][td]Jan[/td][td]Feb[/td][td]Mar[/td][td]Apr[/td][td]May[/td][td]Jun[/td][td]Jul[/td][td]Aug[/td][td]Sep[/td][td]Oct[/td][td]Nov[/td][td]Dec[/td][/tr]
[tr][td]instrument 1[/td][td]Proposed[/td][td]Funded[/td][td]Funded[/td][td]Funded[/td][td]-[/td][td]-[/td][td]-[/td][td]-[/td][td]-[/td][td]-[/td][td]-[/td][td]-[/td][/tr]
[tr][td]instrument 2[/td][td]-[/td][td]Funded[/td][td]-[/td][td]-[/td][td]-[/td][td]-[/td][td]-[/td][td]-[/td][td]-[/td][td]-[/td][td]-[/td][td]-[/td][/tr]
[tr][td]instrument 3[/td][td]Proposed[/td][td]-[/td][td]Proposed[/td][td]-[/td][td]-[/td][td]-[/td][td]-[/td][td]-[/td][td]-[/td][td]-[/td][td]-[/td][td]-[/td][/tr]
[/table]

where each of the boxes "Proposed" or "Funded" will be a different color, set by CSS (e.g. class="proposed" which I couldn't do with this editor

I can only get it to put each of the status on a separate line
[table]
[tr][td]Instrument[/td][td]Jan[/td][td]Feb[/td][td]Mar[/td][td]Apr[/td][td]May[/td][td]Jun[/td][td]Jul[/td][td]Aug[/td][td]Sep[/td][td]Oct[/td][td]Nov[/td][td]Dec[/td][/tr]
[tr][td]instrument 1[/td][td]Proposed[/td][td]Funded[/td][td]-[/td][td]Funded[/td][td]-[/td][td]-[/td][td]-[/td][td]-[/td][td]-[/td][td]-[/td][td]-[/td][td]-[/td][/tr]
[tr][td]instrument 1[/td][td]Proposed[/td][td]-[/td][td]Funded[/td][td]Funded[/td][td]-[/td][td]-[/td][td]-[/td][td]-[/td][td]-[/td][td]-[/td][td]-[/td][td]-[/td][/tr]
[tr][td]instrument 2[/td][td]-[/td][td]Funded[/td][td]-[/td][td]-[/td][td]-[/td][td]-[/td][td]-[/td][td]-[/td][td]-[/td][td]-[/td][td]-[/td][td]-[/td][/tr]
[tr][td]instrument 3[/td][td]Proposed[/td][td]-[/td][td]-[/td][td]-[/td][td]-[/td][td]-[/td][td]-[/td][td]-[/td][td]-[/td][td]-[/td][td]-[/td][td]-[/td][/tr]
[tr][td]instrument 3[/td][td]-[/td][td]-[/td][td]Proposed[/td][td]-[/td][td]-[/td][td]-[/td][td]-[/td][td]-[/td][td]-[/td][td]-[/td][td]-[/td][td]-[/td][/tr]
[/table]
I can't that looping to work.
my code
[code]
$results = $dbq->queryGetResults();
echo '<table border="1">';
echo '<tr><th colspan="14" align="center">2006</th></tr>'."\n";
echo '<tr><td>Institute</td><td>Instrument</td><td>Jan</td><td>Feb</td><td>Mar</td><td>Apr</td><td>May</td><td>Jun</td><td>Jul</td><td>Aug</td><td>Sep</td><td>Oct</td><td>Nov</td><td>Dec</td></tr>'."\n";
foreach ($results as $result)  {
      if ($result['status'] == "F") {
          $CSSRow =' class="funded"';
      } elseif ($result['status'] == "P") {
          $CSSRow =' class="proposed"';
      }
if ($result['start_Year']=="2006") {
      echo '<tr>';
      echo "<td>".$result['institute']."</td>";
      echo "<td>".$result['instrument']."</td>";
      if ($result['start_Month']=="1") {
        echo '<td'.$CSSRow.'>'.$result['start_date'].' ' .$result['status'] .'</td>';     
      } else {
        echo '<td>&nbsp;</td>';
      }
      if ($result['start_Month']=="2") {
        echo '<td'.$CSSRow.'>'.$result['start_date'].' ' .$result['status'] .'</td>';     
            } else {
        echo '<td>&nbsp;</td>';
      }
      if ($result['start_Month']=="3") {
        echo '<td'.$CSSRow.'>'.$result['start_date'].' ' .$result['status'] .'</td>';     
            } else {
        echo '<td>&nbsp;</td>';
      }
      if ($result['start_Month']=="4") {
        echo '<td'.$CSSRow.'>'.$result['start_date'].' ' .$result['status'] .'</td>';     
            } else {
        echo '<td>&nbsp;</td>';
      }
      if ($result['start_Month']=="5") {
        echo '<td'.$CSSRow.'>'.$result['start_date'].' ' .$result['status'] .'</td>';     
              } else {
        echo '<td>&nbsp;</td>';
      }
      if ($result['start_Month']=="6") {
        echo '<td'.$CSSRow.'>'.$result['start_date'].' ' .$result['status'] .'</td>';     
            } else {
        echo '<td>&nbsp;</td>';
      }
      if ($result['UFAM_start_Month']=="7"){
        echo '<td'.$CSSRow.'>'.$result['start_date'].' ' .$result['status'] .'</td>';     
            } else {
        echo '<td>&nbsp;</td>';
      }
      if ($result['start_Month']=="8") {
        echo '<td'.$CSSRow.'>'.$result['start_date'].' ' .$result['status'] .'</td>';     
            } else {
        echo '<td>&nbsp;</td>';
      }
      if ($result['start_Month']=="9") {
        echo '<td'.$CSSRow.'>'.$result['start_date'].' ' .$result['status'] .'</td>';     
            } else {
        echo '<td>&nbsp;</td>';
      }
      if ($result['start_Month']=="10") {
        echo '<td'.$CSSRow.'>'.$result['start_date'].' ' .$result['status'] .'</td>';     
            } else {
        echo "<td>&nbsp;</td>";
      }
      if ($result['start_Month']=="11") {
        echo '<td'.$CSSRow.'>'.$result['start_date'].' ' .$result['status'] .'</td>';     
              } else {
        echo '<td>&nbsp;</td>';
      }
      if ($result['start_Month']=="12") {
        echo '<td'.$CSSRow.'>'.$result['start_date'].' ' .$result['status'] .'</td>';     
      } else {
        echo "<td>&nbsp;</td>";
      }
      echo '</tr>';
    } // end for 2006
}  // end for each result
echo '</table>'."\n";
[/code]

Probably an array of months or something?
Anyway, if you can help me out - appreciated.
Link to comment
https://forums.phpfreaks.com/topic/31376-output-calendar-as-planner-year/
Share on other sites

Use a 2D array

[code]
<style>
TH {
    font-size: 0.8em;
    width: 120px;
}
TD {
    text-align: center;
    width: 80px;
    font-size: 0.8em;
}
TD.Proposed {
    background-color: #FCC;
}
TD.Funded {
    background-color: #CFC;
}
</style>

<?php
$data =  array(
    array("instrument 1","2006-01-01","2006-01-31","Proposed"),
    array("instrument 1","2006-02-01","2006-04-30","Funded"),
    array("instrument 2","2006-02-01","2006-02-28","Funded"),
    array("instrument 3","2006-01-01","2006-01-31","Proposed"),
    array("instrument 3","2006-03-01","2006-03-31","Proposed")
);

$calendar = array();

foreach ($data as $record) {
    list ($inst, $d1, $d2, $status) = $record;
    $m1 = date('n', strtotime($d1));
    $m2 = date('n', strtotime($d2));
   
    if (!isset($calendar[$inst])) $calendar[$inst] = array();
   
    for ($m = $m1; $m <= $m2; $m++) $calendar[$inst][$m] = $status;
}

/**
* output the calendar array data
*/

echo "<table border='1'>\n";
echo "<tr><th>Instrument</th>\n";
for ($i=1; $i<=12; $i++) {
    $t = mktime(0,0,0, $i, 1, 2006);
    $m = date('M', $t);
    echo "<td>$m</td>";
}
echo "<tr>\n";

// data
foreach ($calendar as $inst => $item) {
    echo "<tr><th>$inst</th>\n";
    for ($m=1; $m <= 12; $m++) {
        echo (isset($item[$m])) ? "<td class='{$item[$m]}'>{$item[$m]}</td>" : '<td>-</td>';
    }
    echo "</tr>\n";
}
echo "</table>\n";
?>
[/code]
THANK YOU SO MUCH.

Worked almost perfect out of the box.
I just had to change
[code]list ($inst, $d1, $d2, $status) = $record;[/code]
to
[code]list ($inst, $d1, $d2, $status) = array_values($record);[/code]
because of the way my array was coming out of the database.

A perfect xmas present.
K

PS - you are the barry andrews of the baagrid - I have used that in the past for crosstabs - it's great.

Thanks again.
;D
As I said - works perfect.
I extended it even just a bit further so I could have a campaign label in a status colored box.
Thanks again.
[code]
<style>
TD.P {
    background-color: #FCC;
    text-align: center;
}
TD.M {
    background-color: #CCC;
    text-align: center;
}
TD.F {
    background-color: #CFC;
    text-align: center;
}
</style>
<?php
$results = $dbq->queryGetResults();
$calendar = array();

foreach ($results as $record) {
    list ($inst, $d1, $d2, $campaign, $status) = array_values($record);
    $m1 = date('n', strtotime($d1));
    $m2 = date('n', strtotime($d2));
   
    if (!isset($calendar[$inst])) $calendar[$inst] = array();
   
    for ($m = $m1; $m <= $m2; $m++) {
        $calendar[$inst][$m][0] = $campaign;
        $calendar[$inst][$m][1] = $status;
    }

}

/**
* output the calendar array data
*/
echo '<table border="1" align="left">';

echo "\n<tr><td>Instrument</td>\n";

for ($i=1; $i<=12; $i++) {              // for the twelve months of the year
    $t = mktime(0,0,0, $i, 1, 2006);  // set $t to hour minute second month day year
    $m = date('M', $t);                  // set the month column name to three letter month eg Jan Feb Mar
    echo "<td>$m</td>";                // make the first row the column header of the list of months
}
echo "</tr>\n";

// data
foreach ($calendar as $inst => $item) {
    echo "<tr><td>$inst</td>\n";          // put the instrument name in the first column
    for ($m=1; $m <= 12; $m++) {        // for every month that there is data - print it out
                                                      // with status as class, campaign as column
      echo (isset($item[$m])) ? "<td class='{$item[$m][1]}'>{$item[$m][0]}</td>" : '<td>&nbsp;</td>';
    }
    echo "</tr>\n";
}
echo "</table>\n";
?>[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.