0

假设我有一个文件 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 文件。 那么回到这个问题 ,是否有另一个优雅地做到这一点?

4

1 回答 1

0

您可以将所有内容文件放入一个“content.php”文件中。接下来我不会将文件传递给对象,因为我们现在可以假设所有内容都在 content.php 中。

现在我要做的是将名称或日期传递给 content.php 文件,并在包含期间使用 URL 中的参数。

在您的 content.php 中,我将使用带有传递参数的 switch 语句并回显正确的内容。

代码如下:

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

内容.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 数据库,用于存储团队名称、日期和内容。然后您的页面可以检查日期,并从数据库中获取内容以发布到页面。

希望我能帮上忙,卢克

于 2013-10-19T03:56:22.527 回答