Create static page PHP page with mysql query

Asked

Viewed 163 times

0

My project has a page. php that displays lists of various product categories, the page gets the category ID and lists the products, but each ID has more than 1 thousand results, and the page loading is heavy, I thought of converting the page into static so it will load quickly, try to do with the . htaccess but the page keeps making the request in the database and I was unsuccessful.

ExpiresActive On
ExpiresByType text/html "access plus 1 week"

# CSS / jScript - 5 dias e 2 horas
ExpiresByType text/css "access plus 5 days"

Someone has a tip on how to convert my pages into static without them disobeying the mod_rewrite rules ?

I read online about Xcache, but I don’t have complete freedom on the server to install and configure the domain.

1 answer

0


The solution found was simple, as I want only the contents of the DIV to be static, I created a directory 'cache' for the HTML files that will be generated and stored, and later each page will be checked in the directory if it exists, if it exists, includes it in PHP, otherwise, make the query in mysql and create a new HTML and save the contents of the DIV for later use.

<?php
    if(file_exists('cache/'.$idPagina.'.html')) {
        $content = file_get_contents('cache/'.$idPagina.'.html');
        $content = mb_convert_encoding($content, 'ISO-8859-1', 'UTF-8');
        echo $content;
    } else {
        echo "<div class='divCategorias'>";
        $sql = "SELECT ....";
        while(.......);
        echo "</div>";
        //libxml_use_internal_errors(true);
        $dom = new DOMDocument();
        $dom->loadHTMLFile(site_url_full());
        $xpath = new DOMXPath($dom);
        $element = $xpath->query('//div[@class="divCategorias"]')->item(0);
        $html = $dom->saveHTML($element);
        $data = iconv("CP1257","UTF-8", $data);
        file_put_contents('cache/'.$idPagina.'.html', $html);
        $html = "";
    }
?>

Understanding: - If the cached HTML file exists, print it in PHP. - If the HTML file does not exist, query the DB and then generate the HTML cache for later use.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.