douzi1117 2012-07-09 20:11
浏览 127
已采纳

使用php动态生成列表中的页面

I need to write a short function to dynamically generate a few very basic pages. At the top of the file I will have an array that lists all of the current pages. Then I need the function to create a page for each item in the array. Here are the items that the page will generate:

$appArray = array('testAppOne', 'testAppTwo', 'testAppThree');

$fileName = 'info-'.$appName.'html';
$appLogo = 'path/logo.png';
$appName = appArray[1];
$src = '<!DOCTYPE html><html><head></head><body><img src='".$appLogo."'><h1>'.$appName.'</h1></body></html>'

So would I create a foreach statement to iterate through the array and $fwrite each file to the specific directory? Or is there a better way to approach this?

I'm aware this would be much easier if we used a database, but we're trying to avoid that for now.

  • 写回答

2条回答 默认 最新

  • douzhiba6873 2012-07-09 20:31
    关注

    I can't imagine why you'd want to do this instead of using some dynamic page generation mechanism (i.e. a database), but given the constraints of what you're asking, yes, I think that would be the best way to go. Something like this:

    $appArray = array('testAppOne', 'testAppTwo', 'testAppThree');
    
    foreach ($appArray as $app) {
        $fileName = 'info-'.$app.'html';
        $appLogo = 'path/logo.png';
        $src = '<!DOCTYPE html><html><head></head><body><img src="'.$appLogo.'"><h1>'.htmlentities($app).'</h1></body></html>'
        $fh = fopen($fileName, 'w');
        fwrite($fh, $src);
        fclose($fh);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮