簡體   English   中英

如何在PHP中根據CSV數據生成一系列HTML頁面?

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

一個問題:需要在 PHP 腳本中生成一系列 HTML 頁面,其中包含從 CSV 中的每一行數據輸出的數據(TITLE、DESCRIPTION、H1、IMG、Text、A HREF = "URL")。

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

也就是說,實際上CSV中的每一行,在PHP>中處理時,根據給定的單個模板,即每個單獨的物理頁面(服務器上指定地址處的$頁面名稱)將頁面交給gallery> HTML或PHP目錄) 用這個字符串的 CSV 文件中的數據每個變量的值: $title, $ description, $h1, $image_source, $keywords, $image(number), $url = ...

這對於在 Google 圖片中建立索引是必要的,但與 MySQL 通信來處理查詢還沒有意義。

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

用於處理 CSV 和卸載模板頁面的示例 PHP 代碼:(* 據我所知 - 並寫道,可能存在錯誤)。

<?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."
    }
}
?>

CSV 中的每一行,在 PHP 中處理時,根據給定的單個模板將頁面輸出到 HTML 目錄中的圖形,即每個單獨的物理頁面(服務器上指定地址的名稱 $ page_name)以及來自 CSV 文件的數據給定字符串,每個變量的值:$title, $description, $h1, $image_source, $keywords, $image (number), $url = ...

這對於在 Google 圖片中建立索引是必要的,但與 MySQL 通信來處理查詢還沒有意義。

你最好制作一些動態的東西而不是創建 .html 文件而是回答。

第一行是標題,將其存儲在一個變量中,然后將它與array_combine與行一起使用以創建關聯數組,盡管您只能通過索引訪問。

<?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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM