问题描述:

I have to print those news on my homepage.

On the index.php file I setted include this file below, but nothing is returned... Where is the error?

The script get the news from files.

Then it set each news into a string.

Then the function creates a "standard" echo for each news

Then the news are printed.

But nothing is printed...

Thank you everybody!

<?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();

}

?>

网友答案:

Lots wrong here.

1 - You're not creating a $news array. Create a $news array at the top and populate it:

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

2 - $news isn't global in any case, so the function can't see it. Either modify the function to allow you to pass in a variable:

function post_news($news){...

and then call it with

post_news($news);

Or make the news array global. You can do this by either creating $news as a global array ($GLOBALS['news']=array()) and then accessing it in the function from the global array, or by calling global $news in the beginning of your function.

3 - Learn about isset(). You try to just print out each news item $news[0], etc without checking that an element exists for that key. Try using a foreach() loop.

4 - Assuming that you property set the news items as an array, get variable scope correct, and use a foreach loop, you then try to echo an array... which you cannot do. Either change your file() calls to file_get_contents() or loop through each element of each news item and print the string.

Overall, you need to learn more about scope, arrays, and loops.

Have a look at this code.. I've tried to explain what I'm doing at each step and why it's a better approach:

<?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>';
}
?>
网友答案:

The problem is your function: You are using $news there but you have not declared it a global variable, nor have you passed it to the function.

So $news is undefined in your function.

You should replace your function calls with post_news($news); and your function definition with function post_news($news) for it to work (global variables are bad...).

相关阅读:
Top