【问题标题】:Function to get the news from files and post them on the homepage从文件中获取新闻并将其发布在主页上的功能
【发布时间】:2013-01-05 04:53:31
【问题描述】:

我必须在我的主页上打印这些新闻。 在我设置的 index.php 文件中,在下面包含此文件,但没有返回任何内容...错误在哪里? 该脚本从文件中获取消息。 然后它将每个新闻设置为一个字符串。 然后该函数为每个新闻创建一个“标准”回声 然后将新闻打印出来。

但是什么都没有打印出来……

谢谢大家!

<?php
if (file_exists("./public/ita/news/news.txt")) {
$getnews1 = "./public/ita/news/news.txt";
$news1 = file($getnews1); //file in to an array
}

if (file_exists("./public/ita/news/news2.txt")) {
$getnews2 = "./public/ita/news/news2.txt";
$news2 = file($getnews2); //file in to an array
}

if (file_exists("./public/ita/news/news3.txt")) {
$getnews3 = "./public/ita/news/news3.txt";
$news3 = file($getnews3); //file in to an array
}

if (file_exists("./public/ita/news/news4.txt")) {
$getnews4 = "./public/ita/news/news4.txt";
$news4 = file($getnews4); //file in to an array
}

if (file_exists("./public/ita/news/news5.txt")) {
$getnews5 = "./public/ita/news/news5.txt";
$news5 = file($getnews5); //file in to an array
}

function post_news()
    {
        echo '<div align="left" class="newstitle">';
        echo $news[0];
        echo '</div>';

        echo '<div align="left" class="news">';
        echo '<p></p>';
        echo $news[1];
        echo $news[2];
        echo $news[3];
        echo $news[4];
        echo $news[5];
        echo $news[6];
        echo $news[7];
        echo $news[8];
        echo $news[9];
        echo $news[10];
        echo $news[11];
        echo '</div>';
    }

if($news1 != NULL) {
    $news = $news1;
    post_news();
}

if($news2 != NULL) {
    $news = $news2;
    post_news();
}

if($news3 != NULL) {
    $news = $news3;
    post_news();
}

?>

【问题讨论】:

  • $news1 与 $news[1] 不同;)
  • $news1 是文件新闻。 $news[1] 是该文件的第一行...不幸的是,这不是我的问题
  • 您应该了解 PHP 中的变量范围.. 以及变量名.. 和类型!你还有一段路要走..
  • 我在暗示一个解决方案,确实名称不同。 $news 在你的函数外部和内部是不同的。
  • @Perocat - 如果$news1 是文件行的数组,那么您使用$news1[0] 访问第一行...您从未创建过名为$news 的数组

标签: php file function include indexing


【解决方案1】:

这里有很多错误。

1 - 您没有创建$news 数组。在顶部创建一个$news 数组并填充它:

$news = array();
$news[0]=file(...)

2 - $news 在任何情况下都不是全局的,所以函数看不到它。修改函数以允许您传入变量:

function post_news($news){...

然后用

调用它
post_news($news);

或者使新闻数组全局化。您可以通过将 $news 创建为全局数组 ($GLOBALS['news']=array()) 然后在函数中从全局数组中访问它,或者在函数的开头调用 global $news 来做到这一点。

3 - 了解isset()。您尝试只打印每个新闻项目$news[0], etc,而不检查该键是否存在元素。尝试使用foreach() 循环。

4 - 假设您将新闻项目设置为数组,获取正确的变量范围,并使用 foreach 循环,然后您尝试 echo 数组...这是您无法做到的做。将您的 file() 调用更改为 file_get_contents() 或循环遍历每个新闻项目的每个元素并打印字符串。

总体而言,您需要了解有关范围、数组和循环的更多信息。

看看这段代码。我试图解释我在每一步都在做什么以及为什么这是一个更好的方法:

<?php
//Create an array with all the possible news items
$possible_files = array(
    "./public/ita/news/news.txt",
    "./public/ita/news/news2.txt",
    "./public/ita/news/news3.txt",
    "./public/ita/news/news4.txt",
    "./public/ita/news/news5.txt"
);

//Now loop through these files, check if they exist, and then pass the lines into your function
foreach($possible_files as $possible_file){
    if (file_exists($possible_file)){
         /*why create an array and then loop through it?
           Do everything here... grab the lines and then
           have it echeod out via post_news() in one step */
        $lines_of_news_items = file($possible_file);    
        //PASS this array into the function!
        post_news($lines_of_news_items); 
    }
}

function post_news($news_item_lines){
    //watch out for align='left'... it's an antiquated attribute
    echo '<div align="left" class="newstitle">';
    //print the first line here
    echo $news_item_lines[0]; 
    echo '</div>';
    echo '<div align="left" class="news">';
    //btw, this <p></p> is bad practice... if you're trying to create space use css
    echo '<p></p>'; 
    //now loop though each line, starting with the second line ([1])
    for($i=1;$i<count($news_item_lines);$i++){ 
        echo $news_item_lines[$i];
    }
    echo '</div>';
}
?>

【讨论】:

  • 感谢您的完整解释......我自 1 个月以来一直在自己“学习”php,所以我对范围、数组和循环完全不实用。现在有了你的向导,我明白了你在做什么。我正在使用 CSS,但

    是创建空间的最快方式。 div 左对齐,因为它位于居中对齐的表格上。
  • @Perocat,通常我建议单独提出一个问题,但在这种情况下,只需修改您的样式表:.newstitle {text-align:left; margin-bottom:12px;} .news {text-align:left;}
  • 再次感谢您,我能够做到这一点...事实上,将所有内容都放在 CSS 上要好得多... HTML 会更干净!您说我要了解有关范围、数组等的更多信息。php 手册是最好的方法吗?再次为“很多错误”道歉,但我真的在开始!
  • @Perocat - 不用担心......有些概念有点棘手,但你会掌握它的。至于教程,我不确定在哪里指向您。你可以试试this,但老实说,一本书可能是最好的起点。
  • 我现在做了一个表格来创建一个新的新闻。当我“提交”表单时,在 php 创建新的“news.txt”文件之前,我必须重命名旧新闻,以便 news2 -> news3,news3 -> news4 等。我在这里做错了什么? for($i=2;$i=5;$i++) { 重命名('news{$i}.txt','news{$i++}.txt'); }
【解决方案2】:

问题在于你的函数:你在那里使用$news,但你没有将它声明为全局变量,也没有将它传递给函数。

所以$news 在你的函数中是未定义的。

你应该用post_news($news);替换你的函数调用,用function post_news($news)替换你的函数定义,这样它才能工作(全局变量不好......)。

【讨论】:

  • 您能更好地解释一下如何解决这个问题吗?谢谢!
  • 谢谢它的工作!那么这个函数是带全局变量的,不好吗?出于安全原因,在 php.ini 中默认不会禁用全局变量?
  • @Perocat 不,我不依赖全局变量,而是将变量传递给函数。
  • 如果我想从第 2 行打印到文件的最后一行,我该怎么做?我现在用一个类打印标题(第 0 行),然后用另一个类打印所有其他内容(第 1 行 - 第 11 行),但如果可能的话,我想从第 1 行打印到最后一行而不是第 11 行。 ..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-15
  • 2023-01-22
  • 1970-01-01
相关资源
最近更新 更多