dongzhang7961 2013-03-10 17:20 采纳率: 0%
浏览 44
已采纳

基本日历构建 - 使用pdo输出mysql事件数据

I'm trying to build a calender step by step, basically I just need to output my events data in a really basic calender which looks like this.

enter image description here I've built the calendar okay, but my problem is that I cannot get it to display the events next to the dates. This is my database connection php file, the issue may be caused by this file because I cannot display my mysql data - sorry I dont know pdo at all.

<?php

class DB_Connect {
    /**
     * Stores a database object
     *
     * @var object A database object
     */
    protected $db;
    /**
     * Checks for a DB object or creates one if one isn't found
     *
     * @param object $dbo A database object
     */
    protected function __construct($dbo=NULL)
    {
        if ( is_object($db) )
        {
            $this->db = $db;
        }
        else
        {
            // Constants are defined in /sys/config/db-cred.inc.php
            $dsn = "mysql:host=" . DB_HOST . ";dbname=" . DB_NAME;
            try
            {
                $this->db = new PDO($dsn, DB_USER, DB_PASS);
            }
            catch ( Exception $e )
            {
                // If the DB connection fails, output the error
                die ( $e->getMessage() );
            }
        }
    }
}
?>

this is my calender.php code which creates the necessary classes for the calender dates and events. The issue could be within the _loadEventData function

<?php

include_once '../sys/class/class.db_connect.inc.php';
include_once '../sys/config/db-cred.inc.php';
include_once '../sys/class/class.event.inc.php';

class Calendar extends DB_Connect
{
    private $_useDate;
    private $_m;
    private $_y;
    private $_daysInMonth;
    private $_startDay;

    public function __construct($dbo=NULL, $useDate=NULL)
    {
        //Call the parent constructor to check for a db obj

        parent::__construct($dbo);
        //Gather and store data relevant to the month

        if ( isset($useDate) )
        {
            $this->_useDate = $useDate;
        }
        else
        {
            $this->_useDate = date('Y-m-d H:i:s');
        }
        // Convert to a timestamp, then determine the month&year to use when building the calendar

        $ts = strtotime($this->_useDate);
        $this->_m = date('m', $ts);
        $this->_y = date('Y', $ts);
        //Determine how many days are in the month

        $this->_daysInMonth = cal_days_in_month(
            CAL_GREGORIAN,
            $this->_m,
            $this->_y
        );
        // Determine what weekday the month starts on

        $ts = mktime(0, 0, 0, $this->_m, 1, $this->_y);
        $this->_startDay = date('w', $ts);
    }
    //generate calendar


    private function _loadEventData($id=NULL)
    {
        $sql = "SELECT
    `event_id`, `event_title`, `event_desc`,
    `event_start`, `event_end`
    FROM `events`";
        //If an event ID is supplied, add a WHERE clause so only that event is returned


        if ( !empty($id) )
        {
            $sql .= "WHERE `event_id`=:id LIMIT 1";
        }
        //Otherwise, load all events for the month in use

        else
        {
            //Find the first and last days of the month

            $start_ts = mktime(0, 0, 0, $this->_m, 1, $this->_y);
            $end_ts = mktime(23, 59, 59, $this->_m+1, 0, $this->_y);
            $start_date = date('Y-m-d H:i:s', $start_ts);
            $end_date = date('Y-m-d H:i:s', $end_ts);
            //Filter events to only those happening in the currently selected month

            $sql .= "WHERE `event_start`
    BETWEEN '$start_date'
    AND '$end_date'
    ORDER BY `event_start`";
        }
        try
        {
            $stmt = $this->db->prepare($sql);
            //Bind the parameter if an ID was passed

            if ( !empty($id) )
            {
                $stmt->bindParam(":id", $id, PDO::PARAM_INT);
            }
            $stmt->execute();
            $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
            $stmt->closeCursor();
            return $results;
        }
        catch ( Exception $e )
        {
            die ( $e->getMessage() );
        }
    }
    //Loads all events for the month into an array
    private function _createEventObj()
    {
        /*Load the events array*/
        $arr = $this->_loadEventData();
        /* Create a new array, then organize the events by the day of the month on which they occur*/
        $events = array();
        foreach ( $arr as $event )
        {
            $day = date('j', strtotime($event['event_start']));
            try
            {
                $events[$day][] = new Event($event);
            }
            catch ( Exception $e )
            {
                die ( $e->getMessage() );
            }
        }
        return $events;
    }
    //Returns HTML markup to display the calendar and events Using the information stored in class properties
    public function buildCalendar()
    {
        /*Determine the calendar month and create an array of
        weekday abbreviations to label the calendar columns
        */
        $cal_month = date('F Y', strtotime($this->_useDate));
        $weekdays = array('Sun', 'Mon', 'Tue',
            'Wed', 'Thu', 'Fri', 'Sat');
        /*Add a header to the calendar markup*/
        $html = "
\t<h2>$cal_month</h2>";
        for ( $d=0, $labels=NULL; $d<7; ++$d )
        {
            $labels .= "
\t\t<li>" . $weekdays[$d] . "</li>";
        }
        $html .= "
\t<ul class=\"weekdays\">"
            . $labels . "
\t</ul>";
        /*
        * Load events data
        */
        $events = $this->_createEventObj();
        $html .= "
\t<ul>"; // Start a new unordered list
        for ( $i=1, $c=1, $t=date('j'), $m=date('m'), $y=date('Y');
              $c<=$this->_daysInMonth; ++$i )
        {
            /*Apply a "fill" class to the boxes occurring before
            the first of the month */
            $class = $i<=$this->_startDay ? "fill" : NULL;
            /* Add a "today" class if the current date matches
            the current date*/
            if ( $c==$t && $m==$this->_m && $y==$this->_y )
            {
                $class = "today";
            }
            /*Build the opening and closing list item tags*/
            $ls = sprintf("
\t\t<li class=\"%s\">", $class);
            $le = "
\t\t</li>";
            /*Add the day of the month to identify the calendar box*/
            if ( $this->_startDay<$i && $this->_daysInMonth>=$c)
            {
                /*
                * Format events data
                */
                $event_info = NULL; // clear the variable
                if ( isset($events[$c]) )
                {
                    foreach ( $events[$c] as $event )
                    {
                        $link = '<a href="view.php?event_id='
                            . $event->id . '">' . $event->title
                            . '</a>';
                        $event_info .= "
\t\t\t$link";
                    }
                }
                $date = sprintf("
\t\t\t<strong>%02d</strong>",$c++);
            }
            else { $date="&nbsp;"; }
            /*If the current day is a Saturday, wrap to the next row*/
            $wrap = $i!=0 && $i%7==0 ? "
\t</ul>
\t<ul>" : NULL;
            /*Assemble the pieces into a finished item*/
            $html .= $ls . $date . $le . $wrap;
        }
        /*Add filler to finish out the last week*/
        while ( $i%7!=1 )
        {
            $html .= "
\t\t<li class=\"fill\">&nbsp;</li>";
            ++$i;
        }
        /*Close the final unordered list*/
        $html .= "
\t</ul>

";
        /* Return the markup for output*/
        return $html;
    }
}
?>

This is the php file that creates the array

<?php
/**
 * Stores event information
 */
class Event
{
    /**

     * The event ID
     *
     * @var int
     */
    public $id;
    /**
     * The event title
     *
     * @var string
     */
    public $title;
    /**
     * The event description
     *
     * @var string
     */
    public $description;
    /**
     * The event start time
     *
     * @var string
     */
    public $start;
    /**
     * The event end time
     *
     * @var string
     */
    public $end;
    /**
     * Accepts an array of event data and stores it
     *
     * @param array $event Associative array of event data
     * @return void
     */
    public function __construct($event)
    {
        if ( is_array($event) )
        {
            $this->id = $event['event_id'];
            $this->title = $event['event_title'];
            $this->description = $event['event_desc'];
            $this->start = $event['event_start'];
            $this->end = $event['event_end'];
        }
        else

        {
            throw new Exception("No event data was supplied.");
        }
    }
}
?>

AND finally this is the index file that outputs the calender

      <?php
/*
 * Include necessary files
 */
include_once '../sys/core/init.inc.php';
/*
 * Load the calendar for January
 */
$cal = new Calendar($dbo, "2013-02-01 12:00:00");


/*
 * Set up the page title and CSS files
 */
$page_title = "Events Calendar";
$css_files = array('style.css');
/*
 * Include the header
 */
include_once 'assets/common/header.inc.php';
?>
    <div id="content">
        <?php

        echo $cal->buildCalendar();

        ?>
    </div><!-- end #content -->
<?php
/*
 * Include the footer
 */
include_once 'assets/common/footer.inc.php';
?>

and this is the css

body {
background-color: #789;
font-family: georgia, serif;
font-size: 13px;
}
#content {
display: block;
width: 812px;
margin: 40px auto 10px;
padding: 10px;
background-color: #FFF;
-moz-border-radius: 6px;
-webkit-border-radius: 6px;
border-radius: 6px;
border:2px solid black;
-moz-box-shadow: 0 0 14px #123;
-webkit-box-shadow: 0 0 14px #123;
box-shadow: 0 0 14px #123;
}
h2,p {
margin: 0 auto 14px;
www.it-ebooks.info
CHAPTER 4 ■ BUILD AN EVENTS CALENDAR
156
text-align: center;
}
ul {
display: block;
clear: left;
height: 82px;
width: 812px;
margin: 0 auto;
padding: 0;
list-style: none;
background-color: #FFF;
text-align: center;
border: 1px solid black;
border-top: 0;
border-bottom: 2px solid black;
}
li {
position: relative;
float: left;
margin: 0;
padding: 20px 2px 2px;
border-left: 1px solid black;
border-right: 1px solid black;
width: 110px;
height: 60px;
overflow: hidden;
background-color: white;
}
li:hover {
background-color: #FCB;
z-index: 1;
-moz-box-shadow: 0 0 10px #789;
-webkit-box-shadow: 0 0 10px #789;
box-shadow: 0 0 10px #789;
}
.weekdays {
height: 20px;
border-top: 2px solid black;
}
.weekdays li {
height: 16px;
padding: 2px 2px;
background-color: #BCF;
}
.fill {
www.it-ebooks.info
CHAPTER 4 ■ BUILD AN EVENTS CALENDAR
157
background-color: #BCD;
}
.weekdays li:hover,li.fill:hover {
background-color: #BCD;
-moz-box-shadow: none;
-webkit-box-shadow: none;
box-shadow: none;
}
.weekdays li:hover,.today {
background-color: #BCF;
}
li strong {
position: absolute;
top: 2px;
right: 2px;
}
li a {
position: relative;
display: block;
border: 1px dotted black;
margin: 2px;
padding: 2px;
font-size: 11px;
background-color: #DEF;
text-align: left;
-moz-border-radius: 6px;
-webkit-border-radius: 6px;
border-radius: 6px;
z-index: 1;
text-decoration: none;
color: black;
font-weight: bold;
font-style: italic;
}
li a:hover {
background-color: #BCF;
z-index: 2;
-moz-box-shadow: 0 0 6px #789;
-webkit-box-shadow: 0 0 6px #789;
box-shadow: 0 0 6px #789;
}

I know this is long but I think my issue lies within the calender or db connection file, Id really appreciate some help, as I do not know PDO that well. Thanks in advance

This is my db table structure enter image description here

All of the code is taken from a book.

  • 写回答

1条回答 默认 最新

  • dsxxqndv41105 2013-03-10 22:48
    关注

    In the file with the calendar class:

    After the line that says $date = sprintf(" \t\t\t<strong>%02d</strong>",$c++); you could add $date .= $event_info;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答