0

i have a loop like this below

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."&nbsp;&nbsp;</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."&nbsp;&nbsp;</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

When i save the file what i want is to save it with html head and styles..but now it is saving for each time when the loop runs..i want it for just run once.

Thank you!

1
  • You could use a break or limit the number of elements in $b variable. Commented Nov 27, 2014 at 7:38

1 Answer 1

1

Do you want to have two dayly log files with the same data but differently formatted? Then move your header , footer and filenaming logic outside of your loop.

<?php
foreach ($b as $entry) {
    $body2 .= "<div class='data'><div id=" . $id . "><span style='font-family: Web'>" . $entry->pubDate . "&nbsp;&nbsp;</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 . "&nbsp;&nbsp;</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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.