如何将此信息转换为查询?

时间:2015-04-17 15:31:06

标签: php mysql regex

我有一些信息要转换成查询,但我不知道是否可能(使用正则表达式的崇高文本?或其他?)

我有一个包含很多汽车品牌及其模型的文件

AUSTIN;
1100
HEALEY
MINI
BMW;
X1
X3
FERRARI;
CALIFORNIA


INSERT INTO `car_model` (``,` name_model`, `id_brand`) VALUES ('', '1100', '1'), ('', 'HEALEY', '1'),('', 'MINI', '1');
INSERT INTO `car_model` (``,` name_model`, `id_brand`) VALUES ('', 'X1', '2'), ('', 'X3', '2');
INSERT INTO `car_model` (``,` name_model`, `id_brand`) VALUES ('', '1100', '3');

我需要将每个品牌转换为ID,并将每个模型添加到此ID中。 就像我手动做的那样。

示例,在第一个查询中VALUES ('', '1100', '1')表示('nothing because it's automatic id', '1100 because it's the model', '1 because it's the first brand')。 每个品牌之后都有一个;

2 个答案:

答案 0 :(得分:0)

因为你问这是一个PHP问题,我会写一个小的PHP脚本来提取模型并将它们插入数据库,例如:

<?php
// TODO: add database connection with mysqli_connect()
if (($handle = fopen("test.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
        $sql = "INSERT IGNORE INTO car_model (name_model) VALUES ({$data[0]})";
        mysqli_query($sql);
    }
    fclose($handle);
}
?>

答案 1 :(得分:0)

尝试使用readfile(),然后explode(),然后使用几个循环:

$vals   =   'AUSTIN;
1100
HEALEY
MINI
BMW;
X1
X3
FERRARI;
CALIFORNIA
';

    function FetchModels($vals)
        {
            // Explode on new lines
            $set    =   explode("\n",$vals);
            // If the array is set and not empty
            if(is_array($set) && !empty($set)) {
                    // Filter empties
                    $set    =   array_filter($set);
                    // Loop through the array
                    foreach($set as $value) {
                            // Search for the semi-colon which
                            // indicates brand name
                            if(strpos($value,';') !== false)
                                // Trim off semi-colon
                                $brand = trim($value,";");
                            else {
                                // If the brand is set, start models
                                if(isset($brand))
                                    $array[$brand][]    =   $value;
                                }
                        }
                }
            // Return the array if made
            return (isset($array))? $array:false;
        }

    // Create array from function
    $array  =   FetchModels($vals);

    if(is_array($array) && !empty($array)) {
            // Loop through the array and insert into db
            $i = 1;
            foreach($array as $brand => $models) {
                    // Set the start of the sql
                    $sql    =   "insert into `carbrand` (`name_model`,`id_brand`) VALUES";  
                    // Loop trough models
                    foreach($models as $name) {
                            $finals[$brand][]   =   "('$name',$i)";
                        }
                    // Add models to the sql
                    echo
                    $sql    .=  implode(", ",$finals[$brand]);

                    // Insert your mysql insert here
                    $i++;
                }
        }

给你:

// Original Models array 
// FetchModels()
Array
(
    [AUSTIN] => Array
        (
            [0] => 1100
            [1] => HEALEY
            [2] => MINI
        )

    [BMW] => Array
        (
            [0] => X1
            [1] => X3
        )

    [FERRARI] => Array
        (
            [0] => CALIFORNIA
        )

)

// SQL inserts
insert into `carbrand` (`name_model`,`id_brand`) VALUES('1100',1), ('HEALEY',1), ('MINI',1)
insert into `carbrand` (`name_model`,`id_brand`) VALUES('X1',2), ('X3',2)
insert into `carbrand` (`name_model`,`id_brand`) VALUES('CALIFORNIA',3)