gpt4 book ai didi

php - 在创建类似功能以导入和映射 CSV 方面寻求帮助,类似于 Mailchimp

转载 作者:行者123 更新时间:2023-11-29 06:37:05 25 4
gpt4 key购买 nike

我正在尝试做的是上传产品的 CSV 文件并获取 header 并创建一个映射工具,该工具允许用户将 CSV header 与需要导入的字段相匹配。我在想我需要获取列值而不是行值。

Mailchimp 目前拥有这项非常酷的功能,我相信电子邮件营销中的所有其他大公司都这样做。导入后,系统会列出每个列名称,并允许用户选择要将其映射到其内置列的内容。

我已经研究了几种不同的预先存在的解决方案,但它们似乎都有问题,我将不得不创建自己的自定义解决方案。

我目前正在使用 PHP/MySQL。任何帮助或建议将不胜感激!

最佳答案

根据您为我提供的内容,我将以下 PHP 类放在一起,希望它能为您提供所需的功能。代码底部演示了小类的用法。

<?php
class CSVtoSQL {
private $filePath, $csvData;

public function __construct($filePath) {
$this->filePath = $filePath;
}

public function readDataIn() {
$file = fopen($this->filePath, 'r');
$data = fread($file, filesize($this->filePath));

// Split CSV line by line.
$lines = explode("\n", $data);

// Split each line by commas to extract each value.
for ($i = 0; $i < sizeof($lines); $i++) {
$this->csvData[$i] = explode(',', $lines[$i]);
}

fclose($file);

// Returns true if we managed to read data, false if we didn't.
return (sizeof($this->csvData) > 0);
}

public function getHeadings() {
if (sizeof($this->csvData) > 0) {
// The first index of csvData has all the headings.
return $this->csvData[0];
}
else {
return null;
}
}

public function pushToDatabase($con, $tableName, $columns) {
$createColumnQuery = '';
$insertQuery = 'INSERT INTO ' . $tableName . ' (';

// Build column section of query.
foreach ($columns as $column) {
$createColumnQuery .= $column['name'] . ' ' . $column['type'] . ',';
}

// Remove trailing comma.
$createColumnQuery = rtrim($createColumnQuery, ',');

// Create table.
$con->query('CREATE TABLE ' . $tableName . ' (' . $createColumnQuery . ')');

// Create insert query template.
foreach ($columns as $column) {
$insertQuery .= $column['name'] . ',';
}

// Remove trailing comma.
$insertQuery = rtrim($insertQuery, ',');

// Insert values using our insertQuery string which looks like: "INSERT INTO <<table name>> (<<column 1>>, <<column 2>>"
// Also skip the first line as that has the headings.
for ($i = 1; $i < sizeof($this->csvData); $i++) {
$values = '';

foreach($this->csvData[$i] as $value) {
$values .= '\'' . $value . '\',';
}

// Remove trailing comma.
$values = rtrim($values, ',');
$con->query($insertQuery . ') VALUES (' . $values . ')');
}
}
}

$csv = new CSVtoSQL('test.csv');
$csv->readDataIn();
var_dump($csv->getHeadings());
$con = new mysqli('127.0.0.1', 'root', '', 'testdb');
$columns = array(array('name'=>'col1', 'type'=>'VARCHAR(256)'),
array('name'=>'col2', 'type'=>'VARCHAR(256)'),
array('name'=>'col3', 'type'=>'VARCHAR(256)'));
$csv->pushToDatabase($con, 'test_table', $columns);
?>

创建 CSVtoSQL 类的新对象时,传递 CSV 文件的路径(这可以是相对的或绝对的)。然后调用 readDataIn() 过程打开 CSV,提取行并将它们存储在对象中。然后,您可以选择通过调用 getHeadings() 来获取在 CSV 中找到的标题数组。当您希望提示用户输入他们希望将每个 CSV 标题映射到哪些数据库列时,这对您很有用。最后,调用 pushToDatabase() 并提供一个 MySQLi 连接对象、表名和列数据,如所示。

为您编写上传表单对我来说是微不足道的,但是这个类(class)应该可以帮助您解决问题。

祝你好运!

关于php - 在创建类似功能以导入和映射 CSV 方面寻求帮助,类似于 Mailchimp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23833440/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com