假设我有一个文件foo_version1.php及其实时和发布。我需要在将来的某个特定日期和时间添加新内容。一旦发布日期等于今天的日期,有没有办法让新内容显示?
这是我到目前为止所做的......
<?php
//set the time zone to LA
date_default_timezone_set('America/Los_Angeles');
class Timelock{
var $fileName;
var $contentName;
var $publishDate;
function __construct($fileName, $contentName, $publishDate){
$this->contentName = $contentName;
$this->publishDate = $publishDate;
$this->fileName = $fileName;
}
function contentName(){
return $this->contentName;
}
function publishDate(){
return $this->publishDate;
}
function fileName(){
return $this->fileName;
}
}
//create objects that has certain publish date
$chargers = new Timelock("content1.php" ,"San Diego Chargers", "2013-10-18 16:41:00");
$colts = new Timelock("content2.php" ,"Indianapolis Colts", "2013-10-18 16:41:03");
$vikings = new Timelock("content3.php" ,"Minnesota Vikings", "2013-10-18 16:41:06");
$eagles = new Timelock("content4.php" ,"Philadelphia Eagles", "2013-10-18 16:41:09");
$cowboys = new Timelock("content5.php" ,"Dallas Cowboyws", "2013-10-18 16:41:12");
//add the contents with timelocks into the array
$contentArray = array();
$contentArray[] = $chargers;
$contentArray[] = $colts;
$contentArray[] = $vikings;
$contentArray[] = $eagles;
$contentArray[] = $cowboys;
//include the file if the publish date is today
foreach($contentArray as $obj){
if( strtotime($currentDate) >= strtotime($obj->publishDate())){
include ($obj->fileName());
}
}
?>
这个问题是我每次需要添加不理想的新内容时都需要创建新的php文件。 回到问题 还有另外一个优雅地做到这一点吗?
答案 0 :(得分:0)
您可以将所有内容文件放入单个“content.php”文件中。接下来我不会将文件传递给对象,因为我们现在可以假设所有内容都在content.php中。
现在我要做的是将名称或日期传递给content.php文件,并在include中包含URL中的参数。
在你的content.php中,我会使用带有传递参数的switch语句并回显出正确的内容。
代码如下:
的index.php:
<?php
//set the time zone to LA
date_default_timezone_set('America/Los_Angeles');
class Timelock{
private $contentName;
private $publishDate;
public function __construct($contentName, $publishDate){
$this->contentName = $contentName;
$this->publishDate = $publishDate;
}
public function contentName(){
return $this->contentName;
}
public function publishDate(){
return $this->publishDate;
}
public function fileName(){
return "content.php" . "?name=" . $this->contentName;
}
}
//create objects that has certain publish date
$chargers = new Timelock("San Diego Chargers", "2013-10-18 16:41:00");
$colts = new Timelock("Indianapolis Colts", "2013-10-18 16:41:03");
$vikings = new Timelock("Minnesota Vikings", "2013-10-18 16:41:06");
$eagles = new Timelock("Philadelphia Eagles", "2013-10-18 16:41:09");
$cowboys = new Timelock("Dallas Cowboyws", "2013-10-18 16:41:12");
//add the contents with timelocks into the array
$contentArray = array();
$contentArray[] = $chargers;
$contentArray[] = $colts;
$contentArray[] = $vikings;
$contentArray[] = $eagles;
$contentArray[] = $cowboys;
//include the file if the publish date is today
foreach($contentArray as $obj){
if( strtotime($currentDate) >= strtotime($obj->publishDate())){
include ($obj->fileName());
}
}
?>
content.php:
<?php
switch($_GET["name"]) {
case "San Diego Chargers":
echo "We are in San Diego Chargers content.";
break;
default:
echo "No content found for " . $_GET["name"] . ".";
}
?>
更高效: 根据每个“contentX.php”包含的确切内容,您可以创建一个MySQL数据库,用于存储团队名称,日期和内容。然后您的页面可以检查日期,并从数据库中获取内容以发布到页面。
希望我能提供帮助, 路加