How to generate a HTML page dynamically using PHP?

前端 未结 8 1375
我在风中等你
我在风中等你 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:31

    I've been working kind of similar to this and I have some code that might help you. The live example is here and below, is the code I'm using for you to have it as reference.

    create-page.php

    <?php
    
    // Session is started.
    session_start();
    
    // Name of the template file.
    $template_file = 'couples-template.php';
    
    // Root folder if working in subdirectory. Name is up to you ut must match with server's folder.
    $base_path = '/couple/';
    
    // Path to the directory where you store the "couples-template.php" file.
    $template_path = '../template/';
    
    // Path to the directory where php will store the auto-generated couple's pages.
    $couples_path = '../couples/';
    
    // Posted data.
    $data['groom-name'] = str_replace(' ', '', $_POST['groom-name']);
    $data['bride-name'] = str_replace(' ', '', $_POST['bride-name']);
    // $data['groom-surname'] = $_POST['groom-surname'];
    // $data['bride-surname'] = $_POST['bride-surname'];
    $data['wedding-date'] = $_POST['wedding-date'];
    $data['email'] = $_POST['email'];
    $data['code'] = str_replace(array('/', '-', ' '), '', $_POST['wedding-date']).strtoupper(substr($data['groom-name'], 0, 1)).urlencode('&').strtoupper(substr($data['bride-name'], 0, 1));
    
    // Data array (Should match with data above's order).
    $placeholders = array('{groom-name}', '{bride-name}', '{wedding-date}', '{email}', '{code}');
    
    // Get the couples-template.php as a string.
    $template = file_get_contents($template_path.$template_file);
    
    // Fills the template.
    $new_file = str_replace($placeholders, $data, $template);
    
    // Generates couple's URL and makes it frendly and lowercase.
    $couples_url = str_replace(' ', '', strtolower($data['groom-name'].'-'.$data['bride-name'].'.php'));
    
    // Save file into couples directory.
    $fp = fopen($couples_path.$couples_url, 'w');
    fwrite($fp, $new_file);
    fclose($fp);
    
    // Set the variables to pass them to success page.
    $_SESSION['couples_url'] = $couples_url;
    // If working in root directory.
    $_SESSION['couples_path'] = str_replace('.', '', $couples_path);
    // If working in a sub directory.
    //$_SESSION['couples_path'] = substr_replace($base_path, '', -1).str_replace('.', '',$couples_path);
    
    header('Location: success.php');
    
    ?>
    

    Hope this file can help and work as reference to start and boost your project.

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

    You dont need to generate any dynamic html page, just use .htaccess file and rewrite the URL.

    0 讨论(0)
提交回复
热议问题