How to generate a HTML page dynamically using PHP?

前端 未结 8 1374
我在风中等你
我在风中等你 2020-12-23 11:01

I have a page which displays info about a property, based on the unique ID from the url, searching the mysql database for that ID, getting all the info from that row etc, fa

相关标签:
8条回答
  • 2020-12-23 11:15

    I suggest you to use URL rewrite mod is enough for your problem,I have the same problem but using URL rewrite mod and getting good SEO response. I can give you a small example. Example is that you consider WordPress , here the data is stored in database but using URL rewrite mod many WordPress websites getting good responses from Google and got rank also.

    Example: wordpress url with out url rewrite mod -- domain.com/?p=123 after url rewrite mode -- domain.com/{title of article} like domain.com/seo-url-rewrite-mod

    i think you have understood what i want to say you

    0 讨论(0)
  • 2020-12-23 11:17

    It looks funny but it works.

    <?php 
    $file = 'newpage.html';
    // Open the file to get existing content
    $current = file_get_contents($file);
    // Append a new person to the file
    $current .= "<!doctype html><html>
    <head><meta charset='utf-8'>
    <title>new file</title>
    </head><body><h3>New HTML file</h3>
    </body></html>
    ";
    // Write the contents back to the file
    file_put_contents($file, $current);
    ?>
    
    0 讨论(0)
  • 2020-12-23 11:19

    I was wondering if/how I can 'create' a html page for each database row?

    You just need to create one php file that generate an html template, what changes is the text based content on that page. In that page is where you can get a parameter (eg. row id) via POST or GET and then get the info form the database.

    I'm assuming this would be better for SEO?

    Search Engine as Google interpret that example.php?id=33 and example.php?id=44 are different pages, and yes, this way is better than single listing page from the SEO point of view, so you just need two php files at least (listing.php and single.php), because is better link this pages from the listing.php.

    Extra advice:

    example.php?id=33 is really ugly and not very seo friendly, maybe you need some url rewriting code. Something like example/properties/property-name is better ;)

    0 讨论(0)
  • 2020-12-23 11:20

    Just in case someone wants to generate/create actual HTML file...

    $myFile = "filename.html"; // or .php   
    $fh = fopen($myFile, 'w'); // or die("error");  
    $stringData = "your html code php code goes here";   
    fwrite($fh, $stringData);
    fclose($fh);
    

    Enjoy!

    0 讨论(0)
  • 2020-12-23 11:23

    As per your requirement you dont have to generate a html page dynamicaly. It can be done by .htaccess file .

    Still this is sample code to generate HTML Page

    <?php
    
     $filename = 'test.html';
     header("Cache-Control: public");
     header("Content-Description: File Transfer");
     header("Content-Disposition: attachment; filename=$filename");
     header("Content-Type: application/octet-stream; ");
     header("Content-Transfer-Encoding: binary");
    ?>
    

    you can create any .html , .php file just change extention in file name

    0 讨论(0)
  • 2020-12-23 11:26

    I'll just update the code to contain the changes, and comment it to so that you can see what's going on clearly...

    <?php   
    include("templates/header.htm");   
    
    // Set the default name 
    $action = 'index'; 
    // Specify some disallowed paths 
    $disallowed_paths = array('header', 'footer'); 
    if (!empty($_GET['action'])) { 
        $tmp_action = basename($_GET['action']); 
        // If it's not a disallowed path, and if the file exists, update $action 
        if (!in_array($tmp_action, $disallowed_paths) && file_exists("templates/{$tmp_action}.htm")) 
            $action = $tmp_action; 
    } 
    // Include $action 
    include("templates/$action.htm"); 
    
    include("templates/footer.htm");
    
    0 讨论(0)
提交回复
热议问题