I’m creating 192 html pages using this function in the web.php file, but when it creates the fifth page gives me the error.
function()
{
$homepage = Page::find(1);
$articles_show = Article::where(function ($query) {
$query->where(function ($query2) {
$query2->where('draft', 0)->whereNull('publication_date');
})->orWhere(function ($query2) {
$query2->where('draft', 0)->where('publication_date', '<=', DateHelper::currentDateTime());
});
})->orderBy('publicated_at', 'DESC')->get();
$last_page = ceil($articles_show->count() / 8);
$articles = [];
$count = 0;
for ($i = 5; $i <= $last_page; $i++) {
$filename = 'page'.$i.'.html';
$max_desc_id = $i * 8;
$min_desc_id = $max_desc_id - 7;
foreach ($articles_show as $article) {
$count++;
if ($article->desc_id >= $min_desc_id && $article->desc_id <= $max_desc_id) {
$articles[] = $article;
}
if (($count % 8) == 0) {
$count = 0;
File::put(
resource_path('views/allArticlesHtml/'.$filename),
view('allArticlesTemplate')->with([
"articles" => $articles,
"homepage" => $homepage,
"this_page" => $i,
"last_page" => $last_page
])->render()
);
continue;
}
}
$articles = [];
// if(($count % 8) == 0){
// }
}
}
The pages are created correctly but it’s way too slow.
I don’t really know if i am doing this in the right way or not, i’m still very new at programming and i don’t know how to improve or recreate this code.
2
Answers
I read your code. I’ll try to suggest you best solution.
why you are trying to create html files with same content?
you can easily use blade engine and extend a template in different file.
to solve the error
Maximum execution time of 60 seconds exceeded
you need to increase
max_execution_time
value inphp.ini
file.or put
ini_set('max_execution_time', 180)
at the top of php file .but if you want an alternative way of creating files describe your initial problem.
I had this issue the other day. You can do this workaround:
Every time you create 1 file under that command put this:
It will reset the time limit to 30 seconds and every time reset it.
Reference: https://www.php.net/manual/en/function.set-time-limit.php