让我说我有一个文件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文件,这并不理想。,所以回到问题,,还有其他人能优雅地做这件事吗?
发布于 2013-10-19 03:56:22
您可以将所有内容文件放入一个"content.php“文件中。接下来,我将不将文件传递给对象,因为我们现在可以假设所有内容都在content.php中。
现在,我要做的是在包含过程中将名称或日期传递给content.php文件,其中包含一个参数。
在您的content.php中,我将使用带传递参数的开关语句,并回显正确的内容。
守则如下:
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数据库来存储团队名称、日期和内容。然后,您的页面可以检查日期,并从数据库获取要发布到页面的内容。
希望我能帮上忙,卢克
https://stackoverflow.com/questions/19461601
复制相似问题