duanleiliu7373 2014-11-17 21:07
浏览 20
已采纳

显示.php文件的内容而不是数组中的变量名称

I'm trying to make a simple news feed on my site that will show the latest 3 posts from a directory.

I have a directory full of .php files, inside each is some html and a short story with a link to the full page.

On my home page I've got

<?php
$news = scandir('news_posts', SCANDIR_SORT_DESCENDING);
include($news[0]);
include($news[1]);
include($news[2]);
?>

Which isn't working. I've tried print_r, var_export, echo, the best I've managed to do is have it print the names of the variables (so 14.11.14.php, 15.11,14.php, etc.) rather than the html content within the .php files.

This is probably really simple, but being new to .php I'm a little lost and can't seem to find an appropriate answer via search engines.

  • 写回答

1条回答 默认 最新

  • douju9272 2014-11-17 21:09
    关注

    scandir only returns file names not the path, which seems to be news_posts/. Try glob:

    $news = glob('news_posts/*.*');
    

    Or to keep using scandir:

    include('news_posts/' . $news[0]);
    

    I would use glob since you can return only files and only files with a specific extension.

    sort($news, SORT_DESC);
    
    foreach($news as $file) {
        include($file);
    }
    

    To glob and sort by time in one go:

    array_multisort(array_map('filemtime', $news = glob('news_posts/*.*')), 
                    SORT_DESC, $news);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路