Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
自动博客-多个PHP函数声明?_Php_Html - Fatal编程技术网

自动博客-多个PHP函数声明?

自动博客-多个PHP函数声明?,php,html,Php,Html,我想基于特定文件夹中的文件使用PHP生成HTML,第一个文件的所有内容都有效,但如果添加第二个文件,我只看到: 致命错误:无法在第4行的[path].php中重新声明getTitle()(以前在[path]:3中声明) 我理解这个问题,但不知道如何解决。这是主文件的PHP代码: foreach(glob(“content/blog/*.php”)作为$file){ 需要_once$文件; $index=str_replace(“content/blog/”,“”,str_replace(“.ph

我想基于特定文件夹中的文件使用PHP生成HTML,第一个文件的所有内容都有效,但如果添加第二个文件,我只看到:

致命错误:无法在第4行的[path].php中重新声明getTitle()(以前在[path]:3中声明)

我理解这个问题,但不知道如何解决。这是主文件的PHP代码:
foreach(glob(“content/blog/*.php”)作为$file){
需要_once$文件;
$index=str_replace(“content/blog/”,“”,str_replace(“.php/”,“”,str_replace(“img/”,“”,$file)));
?>

这是每个博客文件中的PHP代码:

 <?php
 function getTitle() {
      echo "Lorem Ipsum 2!";
 }

 function getPublishedDate() {
      echo "When Time has finally come";
 }

 function getContent() {
      echo "<p>Text</p>";
 }
 ?>

我搜索了include、include\u once、require或require\u once的对立面。我还尝试在再次调用foreach之前将变量设置回null


感谢您提前提供的帮助。

问题是您有多个文件,它们定义了相同的函数
getTitle
,您需要它们

需要或包含
*.php
文件就像将所有代码合并到一个文件中一样,这就解释了错误,因为您需要n个文件,并且在每个文件中定义了相同的函数

可以这样做,而不是使用关联数组的函数

<?php
// blog_file_1.php
$prop = array(
    'title' => "This is file 1!",
    'published_date' => "When Time has finally come",
    'content' => "<p>Text</p>"
);

<?php
// blog_file_2.php
$prop = array(
    'title' => "This is file 2!",
    'published_date' => "When Time has finally come",
    'content' => "<p>Text</p>"
);

<?php
// blog.php
foreach (glob("content/blog/*.php") as $file) {
    require_once $file;
    $index = str_replace("content/blog/", "", str_replace(".php", "", str_replace("img/", "", $file)));
                ?>
                <div class="content">
                    <h3><?php echo $prop['title'] ?><span><?php echo $prop['published_date'] ?></span></h3>
                    <p><?php echo $prop['content'] ?></p>
                </div>
                <?php
                    $file = null;
                    }
                ?>


问题是您有多个文件,它们定义了相同的函数
getTitle
,您需要它们

需要或包含
*.php
文件就像将所有代码合并到一个文件中一样,这就解释了错误,因为您需要n个文件,并且在每个文件中定义了相同的函数

可以这样做,而不是使用关联数组的函数

<?php
// blog_file_1.php
$prop = array(
    'title' => "This is file 1!",
    'published_date' => "When Time has finally come",
    'content' => "<p>Text</p>"
);

<?php
// blog_file_2.php
$prop = array(
    'title' => "This is file 2!",
    'published_date' => "When Time has finally come",
    'content' => "<p>Text</p>"
);

<?php
// blog.php
foreach (glob("content/blog/*.php") as $file) {
    require_once $file;
    $index = str_replace("content/blog/", "", str_replace(".php", "", str_replace("img/", "", $file)));
                ?>
                <div class="content">
                    <h3><?php echo $prop['title'] ?><span><?php echo $prop['published_date'] ?></span></h3>
                    <p><?php echo $prop['content'] ?></p>
                </div>
                <?php
                    $file = null;
                    }
                ?>


定义一个类来存储关于一篇博客文章的信息

// name this file class.blogEntry.php

class blogEntry {
    var $title;
    var $publishedDate;
    var $content;

    public function getTitle() {
        return $this->title;
    }

    public function setTitle($title) {
        $this->title = $title;
    }

    public function getPublishedDate() {
        return $this->publishedDate;
    }

    public function setPublishedDate($date) {
        $this->publishedDate = $date;
    }

    public function getContent() {
        return $this->content;
    }

    public function setContent($content) {
        $this->content = $content;
    }   
}
为每一篇博客文章创建一个简单的php文件来设置它的不同属性

$entry->setTitle("Lorem Ipsum 2!");
$entry->setPublishedDate("When Time has finally come");
$entry->setContent("<p>Text</p>");
$entry->setTitle(“Lorem Ipsum 2!”);
$entry->setPublishedDate(“时间终于到了”);
$entry->setContent(Text

);
修改输出以使用类和基于类的博客条目

require_once 'class.blogEntry.php';
foreach (glob("content/blog/*.php") as $file) {
    $entry = new blogEntry();
    require_once $file;
    $index = str_replace("content/blog/", "", str_replace(".php", "", str_replace("img/", "", $file)));
?>
    <div class="content">
        <h3><?php $entry->getTitle() ?><span><?php $entry->getPublishedDate() ?></span></h3>
    <p><?php $entry->getContent() ?></p>
    </div>
<?php
    $file = null;
    }
?>
require_once'class.blogEntry.php';
foreach(glob(“content/blog/*.php”)作为$file){
$entry=新博客条目();
需要_once$文件;
$index=str_replace(“content/blog/”,“”,str_replace(“.php/”,“”,str_replace(“img/”,“”,$file)));
?>


定义一个类来存储关于一篇博客文章的信息

// name this file class.blogEntry.php

class blogEntry {
    var $title;
    var $publishedDate;
    var $content;

    public function getTitle() {
        return $this->title;
    }

    public function setTitle($title) {
        $this->title = $title;
    }

    public function getPublishedDate() {
        return $this->publishedDate;
    }

    public function setPublishedDate($date) {
        $this->publishedDate = $date;
    }

    public function getContent() {
        return $this->content;
    }

    public function setContent($content) {
        $this->content = $content;
    }   
}
为每一篇博客文章创建一个简单的php文件来设置它的不同属性

$entry->setTitle("Lorem Ipsum 2!");
$entry->setPublishedDate("When Time has finally come");
$entry->setContent("<p>Text</p>");
$entry->setTitle(“Lorem Ipsum 2!”);
$entry->setPublishedDate(“时间终于到了”);
$entry->setContent(Text

);
修改输出以使用类和基于类的博客条目

require_once 'class.blogEntry.php';
foreach (glob("content/blog/*.php") as $file) {
    $entry = new blogEntry();
    require_once $file;
    $index = str_replace("content/blog/", "", str_replace(".php", "", str_replace("img/", "", $file)));
?>
    <div class="content">
        <h3><?php $entry->getTitle() ?><span><?php $entry->getPublishedDate() ?></span></h3>
    <p><?php $entry->getContent() ?></p>
    </div>
<?php
    $file = null;
    }
?>
require_once'class.blogEntry.php';
foreach(glob(“content/blog/*.php”)作为$file){
$entry=新博客条目();
需要_once$文件;
$index=str_replace(“content/blog/”,“”,str_replace(“.php/”,“”,str_replace(“img/”,“”,$file)));
?>


我不确定这是否会对您有所帮助,但可能会帮助其他人找到这个问题,因为它符合我的要求。几天前我正是在寻找这个问题,并没有找到多少简单的PHP脚本来生成博客。我在搜索了不同函数的堆栈后将其放在一起(从文件夹、PHP分页、从另一个PHP文件传递变量、对文件进行排序)生成带有链接的图库,并将其放在一起

主博客页面的代码

    \\Blog.php
    <?php
    $directory = "blog/folder/";
    $blogfiles = glob($directory . "*.php");
    usort($blogfiles, function($file_1, $file_2)
    {
        $file_1 = filectime($file_1);
        $file_2 = filectime($file_2);
        if($file_1 == $file_2)
        {
            return 0;
        }
        return $file_2 < $file_1 ? 1 : -1;
    });

    $post_count  = 5;
    $total_pages   = ceil(count($blogfiles)/$post_count);
    $page          = $_REQUEST['page']; ///make it dyanamic :: page num
    $offset        = ($page-1)*$post_count;
    $files_filter  = array_slice($blogfiles, $offset,$post_count);

    foreach ($files_filter as $blogfile) {
    ob_start();
    include "$blogfile";
    ob_end_clean();
    echo '<div class="blog-post"><a href="'.$blogfile.'"><img src="'.$BLOG_IMG.'"><h1 
    class="blog-title">' . $BLOG_TITLE . '</h1></a><br><p class="blog-summary">' . 
    $SUMMARY . '</p></div><hr>', "\n";
    }

    if($total_pages > 1){
    if($page > 1){
    echo '<div class="blog-pagination prev-page"><a href="blog.php?page='.($page- 
    1).'">Prev Page</a></div>';
    }

    if($page != $total_pages){
    echo '<div class="blog-pagination next-page"><a href="blog.php?page='. 
    ($page+1).'">Next Page</a><div class="blog-pagination">';
    }
    }
    ?>
\\Blog.php
这是一个博客帖子模板

    \\post1.php
    <?php
    $DOC_TITLE = "Document Title for browser tab";
    $BLOG_TITLE = "Blog Post Title";
    $SUMMARY = "Page summary";
    $BLOG_IMG = "/Path/to/blog/image.jpg";
    require $_SERVER['DOCUMENT_ROOT'] . '/header.php'; //Pre-made HTML header in the root folder - or just replace with HTML header
    ?>
    \\content
    <?php
    require $_SERVER['DOCUMENT_ROOT'] . '/footer.php'; //Pre-made HTML footer in the root folder - or just replace with HTML footer
    ?>
\\post1.php
\\内容

我不确定这是否会对您有所帮助,但可能会帮助其他人找到这个问题,因为它符合我的要求。几天前我正是在寻找这个问题,并没有找到多少简单的PHP脚本来生成博客。我在搜索了不同函数的堆栈后将其放在一起(从文件夹、PHP分页、从另一个PHP文件传递变量、对文件进行排序)生成带有链接的图库,并将其放在一起

主博客页面的代码

    \\Blog.php
    <?php
    $directory = "blog/folder/";
    $blogfiles = glob($directory . "*.php");
    usort($blogfiles, function($file_1, $file_2)
    {
        $file_1 = filectime($file_1);
        $file_2 = filectime($file_2);
        if($file_1 == $file_2)
        {
            return 0;
        }
        return $file_2 < $file_1 ? 1 : -1;
    });

    $post_count  = 5;
    $total_pages   = ceil(count($blogfiles)/$post_count);
    $page          = $_REQUEST['page']; ///make it dyanamic :: page num
    $offset        = ($page-1)*$post_count;
    $files_filter  = array_slice($blogfiles, $offset,$post_count);

    foreach ($files_filter as $blogfile) {
    ob_start();
    include "$blogfile";
    ob_end_clean();
    echo '<div class="blog-post"><a href="'.$blogfile.'"><img src="'.$BLOG_IMG.'"><h1 
    class="blog-title">' . $BLOG_TITLE . '</h1></a><br><p class="blog-summary">' . 
    $SUMMARY . '</p></div><hr>', "\n";
    }

    if($total_pages > 1){
    if($page > 1){
    echo '<div class="blog-pagination prev-page"><a href="blog.php?page='.($page- 
    1).'">Prev Page</a></div>';
    }

    if($page != $total_pages){
    echo '<div class="blog-pagination next-page"><a href="blog.php?page='. 
    ($page+1).'">Next Page</a><div class="blog-pagination">';
    }
    }
    ?>
\\Blog.php
这是一个博客帖子模板

    \\post1.php
    <?php
    $DOC_TITLE = "Document Title for browser tab";
    $BLOG_TITLE = "Blog Post Title";
    $SUMMARY = "Page summary";
    $BLOG_IMG = "/Path/to/blog/image.jpg";
    require $_SERVER['DOCUMENT_ROOT'] . '/header.php'; //Pre-made HTML header in the root folder - or just replace with HTML header
    ?>
    \\content
    <?php
    require $_SERVER['DOCUMENT_ROOT'] . '/footer.php'; //Pre-made HTML footer in the root folder - or just replace with HTML footer
    ?>
\\post1.php
\\内容

要解决此问题,您应该声明一个类“blogEntry”,该类具有一些方法(getTitle/setTitle、getPublishedDate/setPublishedDate、getContent/setContent),并在每个博客文件中实例化此类的一个对象。然后使用set*方法设置实际值。要解决此问题,您应该声明一个类“blogEntry”它有一些方法(getTitle/setTitle、getPublishedDate/setPublishedDate、getContent/setContent)并在每个博客文件中实例化此类的一个对象。然后使用set*方法设置实际值。非常感谢!PHP比我之前认为的更类似于JavaScript,尽管PHP有一个真正的“脚本小子”就像start一样,它已经开发并获得了真正的OOP功能;-)非常感谢!PHP比我之前想象的更类似于JavaScript,尽管PHP有一个真正的“脚本小子”,就像start一样,它已经开发并获得了真正的OOP功能;-)