简体   繁体   中英

How to generate in PHP a series of HTML pages based on CSV data?

A problem: it is necessary to generate in PHP script the series of HTML pages with data output from each row of data in CSV (TITLE, DESCRIPTION, H1, IMG, Text, A HREF = "URL").

$image_number
$image_name
$title
$description
$image_source
$keywords
$url
$page_name

That is, in fact, each line in CSV, when processed in PHP>, gives pages to the gallery> HTML or PHP directory according to a given single template, ie each individual physical page ($ Page Name at the specified address on the server) with data from the CSV file of this string values ​​of each variable: $ title, $ description, $ h1, $ image_source, $ keywords, $ image (number), $ url = ...

This is necessary for indexing in Google Images, but there is no point in communicating with MySQL to process queries yet.

CSV directory: https://drive.google.com/file/d/1F_zWVaFDtjp82NDMZdvhWHyB1GYq4BmG/view?usp=sharing

Sample PHP code * for processing CSV and unloading template pages: (* as I understood - and wrote, probably there are errors).

<?php
    function CSVtoHTML {
        $row = 1;
if (($handle = fopen ("catalog.csv", "r"))! == FALSE) {
  while (($data = fgetcsv ($handle, 30, ","))! == FALSE) {
    for each ($row[i]) {
                $column[1] = $image_number;
                $column[2] = $image_name;
                $column[3] = $title;
                $column[4] = $description;
                $column[5] = $image_source;
                $column[6] = $url;
                $column[7] = $page_name;
                $content = '<! doctype html>
                <html lang="en">
                    <head>
                        <meta charset = "UTF-8">
                        <title>'.$title.'</title>
                        <meta description = "'.$ description.'" />
                    </head>
                    <body>
                        <div class="content">
                            <h1> '.$h1.'</h1>
                            <img src = "'.$image_source.'" alt = "">
                            <h2> '.$image_name.' </h2>
                            <p> Keywords: '.$keywords.' </p>
                            <p> <a href="/register?value='.$image_number.'"> Buy now </a> </p>
                        </div>
                    </body>
                </html> ';
/ * How to write data in HTML with UTF-8 encoding to a folder on the server with a given name? * /
                $htmldata = file_get_contents ($content);
                $htmldata = mb_convert_encoding ($htmldata, 'UTF-8');
                file_put_contents ('graphics/'.$page_name.'.html', $htmldata);
           }
           fclose ($handle);
       }
       return $data;
       echo "Catalog generated now"
    }
    else {
        echo "Catalog in CSV is empty or broken."
    }
}
?>

Each line in CSV, when processed in PHP, outputs pages to the graphics in HTML directory according to a given single template, ie each individual physical page (name $ page_name at the specified address on the server) with data from the CSV file of the given string, the values ​​of each variable: $title, $description, $h1, $image_source, $keywords, $image (number), $url = ...

This is necessary for indexing in Google Images, but there is no point in communicating with MySQL to process queries yet.

You would be better off making something dynamic rather than creating .html files but to answer.

First row is the header, store it in a variable then use it with array_combine with the row to create your associative array, though you could just access by index.

<?php
function CSVtoHTML() {
    $row = 0;
    $header = [];
    if (($handle = fopen("catalog.csv", "r")) !== FALSE) {
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {

            // first line is header, grab it then continue
            if ( $row === 0) {
                $header = $data;
                $row++;
                continue;
            }

            // make associative array from header and data
            $data = array_combine($header, $data);

            // make document (you need to work on this, but your see var is accessed by header name)
            $content = '<!doctype html>
                <html lang="en">
                    <head>
                        <meta charset = "UTF-8">
                        <title>'.$data['Title'].'</title>
                        <meta description = "'.$data['Description'].'" />
                    </head>
                    <body>
                        <div class="content">
                            <h1> '.$data['Title'].'</h1>
                            <img src="'.$data['Image Source'].'" alt>
                            <h2> '.$data['Image Name'].' </h2>
                            <p> Keywords: '.$data['Keywords'].' </p>
                            <p> <a href="/register?value='.$data['Image Number'].'"> Buy now </a> </p>
                        </div>
                    </body>
                </html> ';

            // make the file
            file_put_contents('./graphics/'.$data['Page Name'], $content);

            $row++;
        }
        fclose($handle);
    }
}

// and don't forget to call it
CSVtoHTML();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM