2013-03-10 37 views
0

我想要一步一步地构建一个日历,基本上我只需要在一个非常基本的日历中输出我的事件数据,它看起来像这样。基本日历构建 - 使用pdo输出mysql事件数据

enter image description here 我已经建立了日历好,但我的问题是,我不能让它显示日期旁边的事件。这是我的数据库连接php文件,这个问题可能是由这个fil e引起的,因为我无法显示我的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 
/** 
* 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文件中

 <?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。 在此先感谢

这是我的数据库表结构 enter image description here

所有的代码是从一本书拍摄。

+0

你可以给我们您正在使用的数据库的模式? – Kris 2013-03-10 17:24:46

+0

@Kris,我添加了我的表结构的屏幕截图 – elmify 2013-03-10 17:27:54

+1

实际上,来仔细看看你的截图,看起来问题是在css的某个地方。你忘了链接样式表或其他东西? – Kris 2013-03-10 17:28:15

回答

1

在与日历类文件:

,说$date = sprintf("\n\t\t\t<strong>%02d</strong>",$c++);线后,您可以添加$date .= $event_info;

+0

谢谢,现在我可以看到所有的数据在我的日历上 – elmify 2013-03-10 23:19:49