我有一个 Excel 文件,其内容如图所示:
如您所见,基本上有三列(可能是四列或更多列)代表类别和子类别。我需要阅读这个 Excel 文件,然后编写一些 SQL 语句,例如:
INSERT INTO `category` (name, parent, status, modified) VALUES('Electronica', NULL, 1, NOW());
INSERT INTO `category` (name, parent, status, modified) VALUES('Celulares y Telefonos', 1, 1, NOW());
INSERT INTO `category` (name, parent, status, modified) VALUES('Accesorios para celulares', 2, 1, NOW());
我知道如何使用 PHPExcel_IO 读取文件,但不知道如何获取父>子关系以构建正确的 SQL 语句,有什么帮助或建议吗?
更新
这是我到目前为止所做的。首先这里是完整的源,如果您有任何问题,请随时与我联系以发送文件(我这样做是因为数组很大,并且像 paste.org 这样的服务不允许发布它,这里会很丑帖子)。
我创建了这个函数来读取数组数据:
function print_r_reverse($in) {
$lines = explode("\n", trim($in));
if (trim($lines[0]) != 'Array') {
return $in;
} else {
if (preg_match("/(\s{5,})\(/", $lines[1], $match)) {
$spaces = $match[1];
$spaces_length = strlen($spaces);
$lines_total = count($lines);
for ($i = 0; $i < $lines_total; $i++) {
if (substr($lines[$i], 0, $spaces_length) == $spaces) {
$lines[$i] = substr($lines[$i], $spaces_length);
}
}
}
array_shift($lines); // Array
array_shift($lines); // (
array_pop($lines); // )
$in = implode("\n", $lines);
preg_match_all("/^\s{4}\[(.+?)\] \=\> /m", $in, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
$pos = array();
$previous_key = '';
$in_length = strlen($in);
foreach ($matches as $match) {
$key = $match[1][0];
$start = $match[0][1] + strlen($match[0][0]);
$pos[$key] = array($start, $in_length);
if ($previous_key != '')
$pos[$previous_key][1] = $match[0][1] - 1;
$previous_key = $key;
}
$ret = array();
foreach ($pos as $key => $where) {
$ret[$key] = print_r_reverse(substr($in, $where[0], $where[1] - $where[0]));
}
return $ret;
}
}
这是我创建的用于读取数组值并编写 SQL 语句的代码:
$result = print_r_reverse($arrayData);
class RPMQGen {
var $array_to = "";
var $count = 1;
var $last_count_parent = -1;
var $t_name = "";
function RPMQGen($tableName, $array) {
$this->t_name = $tableName;
$this->array_to = $array;
}
function walk_children($value) {
$query = "";
$key_result = 0;
$index_count = 0;
foreach ($value as $key => $val)
if (strlen($val) > 1) {
$index_count++;
$key_result = $key;
};
if ($index_count > 1) {
foreach ($value as $key => $val) {
if (strlen($val) > 1) {
$query = $query . "INSERT INTO `" . $this->t_name . "` (`id`, `name`, `parent`, `deletedAt`) VALUES (" . $this->count . ",`" . $val . "`, " . ($key + 1) . ", 1, NULL); <br>";
$this->count++;
}
}
$this->last_count_parent = $this->count;
} else
if ($index_count == 1) {
$query = $query . "INSERT INTO `" . $this->t_name . "` (`id`, `name`, `parent`, `deletedAt`) VALUES (" . $this->count . ",`" . ($value[$key_result]) . "`, " . ($this->last_count_parent - 1) . ", 1, NULL); <br>";
$this->count++;
}
return $query;
}
function get_queries() {
$this->count = 2;
$query = "INSERT INTO `" . $this->t_name . "` (`id`, `name`, `parent`, `deletedAt`) VALUES (1,`root`, NULL, NULL); <br>";
foreach ($this->array_to as $key => $value) {
$query = $query . "INSERT INTO `" . $this->t_name . "` (`id`, `name`, `parent`, `deletedAt`) VALUES (" . $this->count . ",`" . $key . "`, 0, NULL); <br>";
$this->count++;
foreach ($value as $key => $value2) {
$query = $query . $this->walk_children($value2);
}
}
print($query);
}
}
$qgen = new RPMQGen('category', $result);
$qgen->get_queries();
但是父母>孩子的关系不对,我找不到原因