i m developing one real estate site using php/mysql.when i search i get one page with type, state ,locality.i wanted another column as click here to view that details.
I want every detail page stored as a html document.
Basically i need a script that will read the entire database
and create one html doc for every listing.

If there are 100 entries in the DB i must have 100 html docs.i can name every enteries as listing id 1 or listing id 2 ext.

Every row in the search table will have a link to this details page
HTML doc.
how i must proceed to store details .
pls guide me. i m new to php

Dani AI

Generated

This thread is about generating one static HTML file per listing from a PHP/MySQL site. Earlier replies from (URL-rewrite / SEO comments) and (capture/cache approach) are useful context. Below is a simple, reliable pattern that avoids re-requesting your own pages over HTTP and reduces race conditions when producing many files.

Workflow in plain steps: query the listings you want to publish; create a filesystem-safe filename (use a sanitized title plus the numeric id so names stay unique); render the listing into your template using output buffering (so you use the data directly, not an HTTP fetch); write the rendered HTML to a temporary file and then rename it into place (atomic replace); set secure permissions; and regenerate only records whose last_modified timestamp changed. Run the generator from the command line or a cron job to avoid web request timeouts, and always escape user content before inserting it into HTML.

Example pattern (short):

<?php
// assume $pdo is a PDO instance and template-listing.php uses $row
$stmt = $pdo->query("SELECT id, title, body, last_modified FROM listings");
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $slug = preg_replace('/[^a-z0-9\-]+/', '-', strtolower(trim($row['title'])));
    if ($slug === '') $slug = 'listing';
    $filename = "pages/{$slug}-{$row['id']}.html";
    ob_start();
    include 'template-listing.php';
    $html = ob_get_clean();
    $tmp = $filename . '.tmp';
    file_put_contents($tmp, $html, LOCK_EX);
    rename($tmp, $filename); // atomic on same filesystem
    chmod($filename, 0644);
}
?>

Troubleshooting and cautions: keep images and asset paths correct (or copy assets), track and delete files for removed listings, avoid 0777 (use file ownership/group instead), and monitor disk usage. If listings update very frequently, consider server-side caching rather than full static regeneration. Since reports success with a file-write approach, this buffered render + atomic write method is generally simpler, faster and safer for bulk generation.

Recommended Answers

All 8 Replies

You can use .htaccess and mod_rewrite funtion to rewrite your dynamic page into a 'static' html page. For example:

Rewrite this:

to this:

Even though this looks like a static page it is still as dynamic as it was before.
BUT, you have to make sure your server/hosting provider allow .htaccess to be used.

Good luck

By the way, why you want html pages? Many search engine like google and yahoo is able to crawl dynamic site like daniweb. Plus, almost all browser support php page and php page will not reduce your page rank.

if i convert dyanmic pages to static
html, becomes faster.

There is no evident about static page will load faster than dynamic one. The loading speed is depend on the network capability of your server as well as the user's. Many dynamic site may load faster as the page using server-side include to call content from a number of small pages or from database. The page will load almost immediately when called although some content from SSI pages may not completey loaded yet. In contrast, static page need to hold all information you currently store in database and guess what. Your page load slower.

i agree with ur point .but not my client :D . so any body help me of good tutorial. or any body tell me how i should proceed

Hi aarya,

I posted a reply for you here http://www.daniweb.com/techtalkforums/thread32816.html on your last post.

However, it seems you want to get the data from your db.
What you would need to do is to gather each listing from the db. And use a function like the following to create the html pages.

// creates a local version of any url contents as html files
function cache_url($url, $cache_name, $cache_path = 'cache/') {

        // this is simple the path to store the html file
        $cache_abs_path = $cache_path.$cache_name;
        
        // this opens an http connection to the url of your webpage
        // search fopen at php.net
        // you must supply a http path to this, so that we use the http wrapper and return only html
        if (!$file = @fopen($url, 'r')) {
            echo ('could not open the url: '.$url);
            return false;
        }
       
        // this opens a file pointer to the html file you are saving too
        // if the file doesnt exist, then it will attempt to create a new one
        // you need the directory to have write permissions for the script
        // chmod 777  (this can be done via ftp, or a web control panel, or php)
        if (!$cache_file = @fopen($cache_abs_path, 'w+')) {
            echo ('could not open the cache: '.$cache_abs_path.' for writing.');
            return false;
        }

        // here we read the contents of the webpage
        while($read = fread($file, '2082')) {

            $content .= $read;
            
           // we write the content of our webpage to the html file
            if (!@fwrite($cache_file, $read)) {
            echo ('error writing to cache: '.$cache_file.' for writing.');
            return false;
            }
            
        }

Heres an example:

// if the script is in your webroot (http://mysite.com/) then the new page will be )

cache_url('', 'page_43.html', 'pages');

what you will do is:

connect to db:
get page listing id=1
Then supply it to he function.

cache_url('', 'page_1.html', 'pages');

I would however recommend using the title of the listing instead of id (as long as the titles are unique), since you are already going to the difficulty of creating the static pages, make it SEF.

good luck.
;)

ps: I didnt test the function, so there may be typos. It should be easy to figure out though if you use php.net as a reference to search each function.

hi thanks for ur reply. thsi would definetly help me
but i tried my self and i got what i wanted .i used fputs function to do this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.