0

私は段階的にカレンダーを作成しようとしています。基本的には、次のような非常に基本的なカレンダーでイベントデータを出力する必要があります。

ここに画像の説明を入力してください カレンダーは問題なく作成できましたが、問題は、日付の横にイベントを表示するようにカレンダーを取得できないことです。これは私のデータベース接続のphpファイルです。mysqlデータを表示できないため、このファイルが原因で問題が発生している可能性があります。申し訳ありませんが、pdoをまったく知りません。

<?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() );
            }
        }
    }
}
?>

これは、カレンダーの日付とイベントに必要なクラスを作成する私のcalender.phpコードです。問題は_loadEventData関数内にある可能性があります

<?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 = "\n\t<h2>$cal_month</h2>";
        for ( $d=0, $labels=NULL; $d<7; ++$d )
        {
            $labels .= "\n\t\t<li>" . $weekdays[$d] . "</li>";
        }
        $html .= "\n\t<ul class=\"weekdays\">"
            . $labels . "\n\t</ul>";
        /*
        * Load events data
        */
        $events = $this->_createEventObj();
        $html .= "\n\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("\n\t\t<li class=\"%s\">", $class);
            $le = "\n\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 .= "\n\t\t\t$link";
                    }
                }
                $date = sprintf("\n\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 ? "\n\t</ul>\n\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 .= "\n\t\t<li class=\"fill\">&nbsp;</li>";
            ++$i;
        }
        /*Close the final unordered list*/
        $html .= "\n\t</ul>\n\n";
        /* Return the markup for output*/
        return $html;
    }
}
?>

これは、配列を作成するphpファイルです

<?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.");
        }
    }
}
?>

そして最後にこれはカレンダーを出力するインデックスファイルです

      <?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';
?>

これは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;
}

私はこれが長いことを知っていますが、私の問題はカレンダーまたはデータベース接続ファイルにあると思います。PDOをよく知らないので、助けていただければ幸いです。前もって感謝します

これは私のdbテーブル構造です ここに画像の説明を入力してください

すべてのコードは本から取られています。

4

1 に答える 1

1

カレンダークラスのファイル:

$date = sprintf("\n\t\t\t<strong>%02d</strong>",$c++);追加できるという行の後に$date .= $event_info;

于 2013-03-10T22:48:19.173 に答える