我有一个如下所示的循环
foreach( $b as $entry) {
$title2 = "<!doctype html>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type'>
<head>
<link rel='stylesheet' href='../../css/bootstrap.min.css'>
</head>
<body>";
$title2 .= "<div class='data'><div id=".$id."><span style='font-family: Web'>".$entry->pubDate." </span><a href='../../fetch.php?url=".$id."' title='$entry->title' >" .$title. "</a><br/><div class='content'>".$description."</div></div></div>";
$title3 = "<!doctype html>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type'>
<meta name='viewport' content='width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;'>
<head>
<link rel='stylesheet' href='../../../css/bootstrap.min.css'>
</head>
<body><div class='container'>
<div class='row'>
<div class='col-lg-12' style='margin-top:20px;'>";
$title3 .= "<div class='list-group'><a href='../../../fetch.php?url=".$id."' title='$entry->title' class='list-group-item' > <i class='fa fa-chevron-right rt'></i><div class='title'><div id=".$id."><span style='font-family: Malithi Web'>".$entry->pubDate." </span>'<div><h4 class='list-group-item-heading'>'" .$title. "</h4></div><br/></div></div></div></div></div></div></a></div>";
if (!file_exists('File')) {
mkdir('File', 0777, true);
}
if (!file_exists('./File/'.date("Y-m-d"))) {
mkdir('./File/'.date("Y-m-d"), 0777, true);
}
if (!file_exists('./File/titles/'.date("Y-m-d"))) {
mkdir('./File/titles/'.date("Y-m-d"), 0777, true);
}
$File = './File/'.date("Y-m-d").'/'."File.html";
file_put_contents($File, $title2, FILE_APPEND | LOCK_EX);
$FileT = './File2/titles/'.date("Y-m-d").'/'."File2.html";
file_put_contents($FileT, $title3, FILE_APPEND | LOCK_EX);
}//foreach end
当我保存文件时我想要的是用html头和样式保存它。但现在它在循环运行时每次保存..我希望它只运行一次。
谢谢!
答案 0 :(得分:1)
您是否希望每天有两个具有相同数据但格式不同的日志文件? 然后将页眉,页脚和文件命名逻辑移到循环之外。
<?php
foreach ($b as $entry) {
$body2 .= "<div class='data'><div id=" . $id . "><span style='font-family: Web'>" . $entry->pubDate . " </span><a href='../../fetch.php?url=" . $id . "' title='$entry->title' >" . $title . "</a><br/><div class='content'>" . $description . "</div></div></div>";
$body3 .= "<div class='list-group'><a href='../../../fetch.php?url=" . $id . "' title='$entry->title' class='list-group-item' > <i class='fa fa-chevron-right rt'></i><div class='title'><div id=" . $id . "><span style='font-family: Malithi Web'>" . $entry->pubDate . " </span>'<div><h4 class='list-group-item-heading'>'" . $title . "</h4></div><br/></div></div></div></div></div></div></a></div>";
}
if (!file_exists('File')) {
mkdir('File', 0777, true);
}
if (!file_exists('./File/' . date("Y-m-d"))) {
mkdir('./File/' . date("Y-m-d"), 0777, true);
}
if (!file_exists('./File/titles/' . date("Y-m-d"))) {
mkdir('./File/titles/' . date("Y-m-d"), 0777, true);
}
$File = './File/' . date("Y-m-d") . '/' . "File.html";
$title2 = "<!doctype html>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type'>
<head>
<link rel='stylesheet' href='../../css/bootstrap.min.css'>
</head>
<body>";
$footer2 = "</body></html>";
file_put_contents($File, $title2 . $body2 . $footer2, FILE_APPEND | LOCK_EX);
$title3 = "<!doctype html>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type'>
<meta name='viewport' content='width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;'>
<head>
<link rel='stylesheet' href='../../../css/bootstrap.min.css'>
</head>
<body><div class='container'>
<div class='row'>
<div class='col-lg-12' style='margin-top:20px;'>";
$footer3 = "</div></div></div></body></html>";
$FileT = './File2/titles/' . date("Y-m-d") . '/' . "File2.html";
file_put_contents($FileT, $title3 . $body3 . $footer3, FILE_APPEND | LOCK_EX);