Jump to content

Got great help here, but having trouble implementing?


Izzy-B

Recommended Posts

I wanted to stop back by today and share something that finally dawned on me around 2:00 this morning, when I was still mulling why the calendar didn't look quite right.  In my fervor to have the calendar function, I thoughtlessly omitted including an echo to close the last row,  the table and 2 div tags.  I  corrected this in the code below and also added a bit to get the empty table rows (falling after the last day of the month) to continue to flesh out the table row so that the overall appearance is a bit neater.  So you'd compare this page to this page to see what I mean. 

 

Edited to include my omissions:

     <?php

    $conn = ($GLOBALS["___mysqli_ston"] = mysqli_connect("localhost", "user", "password", "database_")) or die('Cannot connect to the database because: ' . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));
    ?>
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Calendar</title>
    <meta charset="utf-8">
   <link rel="stylesheet" type="text/css" media="screen" href="css/master.css"> 

</head>
<body>
      <?php
     
    // calender entries. Use the date as the key (in YYYY/MM/DD format)
     
    $entries = array(
    '2014/8/16' => array(
    'EVENT',
    ) ,
    );
    $currMonth = isset($_GET['month']) ? $_GET['month'] : date('n');
    $currYear = isset($_GET['year']) ? $_GET['year'] : date('Y');
    $today = (($currYear == date('Y')) && ($currMonth == date('n'))) ? date('j') : 0;
    $currMonth = sprintf('%02d', $currMonth); // pad currMonth with zero's
     
    // when using user input, always use prepared statements otherwise could lead to SQL injection attacks
     
    $stmt = $conn->prepare('SELECT startdt, description FROM events WHERE YEAR(startdt) = ? AND MONTH(startdt) = ?');
    $stmt->bind_param('ii', $currYear, $currMonth);
    $stmt->execute();
    $stmt->bind_result($date, $description);
    $entries = array();
     
    while ($stmt->fetch())
    {
    $entries[$date][] = $description;
    }
     
    $prevMonth = $currMonth == 1 ? 12 : $currMonth - 1;
    $nextMonth = $currMonth == 12 ? 1 : $currMonth + 1;
    $prevYear = $currMonth == 1 ? $currYear - 1 : $currYear;
    $nextYear = $currMonth == 12 ? $currYear + 1 : $currYear;
    $day1 = mktime(0, 0, 0, $currMonth, 1, $currYear);
    $dim = date('t', $day1);
    $dayN = mktime(0, 0, 0, $currMonth, $dim, $currYear);
    $dow1 = (date('w', $day1) + 0) % 7;
    $dowN = (date('w', $dayN) + 0) % 7;
    $calHead = date('F Y', $day1);
    echo <<<EOT
    <div class="calwrapper">
    <div class="container">
    <div class="fnl first"></div>
    <div class="adjust"></div>
    <div class="fnl last"></div>
    </div>
    <div class="caldisplay">
    <table cellspacing="0">
    <tr>
    <td class="hd"><a class="cal_button" href="$_SERVER[PHP_SELF]?year=$prevYear&month=$prevMonth"> Prev </a></td>
    <td colspan="5" class="adjust">$calHead</td>
    <td class="hd"><a class="cal_button" href="$_SERVER[PHP_SELF]?year=$nextYear&month=$nextMonth"> Next </a></td>
    </tr>
    <tr>
    <th class="we">Sun</th>
    <th class="wd">Mon</th>
    <th class="wd">Tue</th>
    <th class="wd">Wed</th>
    <th class="wd">Thu</th>
    <th class="wd">Fri</th>
    <th class="we">Sat</th>
    </tr>
    <tr>
EOT;
    for ($d=0;$d<$dow1;$d++) echo "<td class=\"hd\"> </td>";
    $c = $dow1;
    for ($d=1; $d<=$dim; $d++, $c++) {
    if ($c%7==0) echo "</tr><tr>";
    $cl = ($c%7==5) || ($c%7==6) ? 'we' : 'wd';
    $st = ($d == $today) ? "style='padding: 0px;'" : '';
    echo "<td class=\"$cl\" $st>\n";
    echo " $d ";
    $dateKey = sprintf('%04d-%02d-%02d',$currYear,$currMonth,$d);
    if(array_key_exists($dateKey, $entries))
    {   
    foreach($entries[$dateKey] as $entry)
    {
    echo '<div class="has-tooltip">
    Event
    <span class="tooltip">'.$entry.'</span>
    </div>';
    }
    }
    }
    while ($c++ % 7 != 0) echo '<td class=\"hd\"> </td>';
    echo "</tr></table>\n";
    echo '</div></div>';
    ?>
                         
</body>
</html>

(My table is "events".  Yours may be something else.)

 

And, yes, again, I cannot say enough about how appreciative I am of the patient time and effort taken to help me and to also educate me.  I'm so grateful.  :)

Link to comment
Share on other sites

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.