将每个td的数组数据插入mysql数据库

时间:2015-08-29 03:35:38

标签: mysql arrays

请帮助我如何从表中插入每个t​​d的数组数据:

我想要插入样本:

TRXID>表mysql TRXID VALUE 1045629等。

这是我的代码



$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($output);
libxml_clear_errors();
$xpath = new DOMXpath($dom);

$data = array();

// get all table rows and rows which are not headers
//$table_rows = $xpath->query('//table[@class="report_table"]//tr[2]//td[8]');//tabel sukses
$table_rows = $xpath->query('//table[@class="report_table"]');

foreach($table_rows as $row => $tr) {
    foreach($tr->childNodes as $td) {
        $data[$row][] = preg_replace('~[\r\n]+~', '', trim($td->nodeValue));
    }
    $data[$row] = $data[$row];
    //$data[$row] = $data[$row][1];
   // $data[$row] = $data[$row][2];
    //$data[$row] = $data[$row][3];
    
}

echo '<pre>';
print_r($data);
&#13;
&#13;
&#13;

代码显示这样的数组

&#13;
&#13;
Array
(
    [0] => Array
        (
            [0] => Trx ID                  Member ID                  NoHP Pengirim                  Waktu Mulai                  Waktu Selesai                  NoHP Di isi                  Kode Voucher                  Keterangan
            [1] => 1045629                          SY3345                          +6288215527644                          2015-08-28 16:00:32                          2015-08-28 16:01:32                          081325415747                                              TS100                          SUKSES
            [2] => 1042300                          SY3345                          +6288215527644                          2015-08-22 18:45:41                          2015-08-22 18:48:55                          081325415747                                              TS20                          SUKSES
            [3] => 1037299                          SY3345                          jpcom_cs                          2015-08-14 20:08:49                          2015-08-14 20:11:58                          081325415747                                              TS50                          SUKSES
        )

    [1] => 
    [2] => 
)
&#13;
&#13;
&#13;

那么,请教我如何将每个TR / TD插入到mysql数据库中,如何使用表:TRID,MEMBERID等。

1 个答案:

答案 0 :(得分:0)

假设第一行包含表头并且它们保持不变,我已根据代码数组的外观创建了以下代码(我考虑了一个示例表名,代码是根据您的要求定制的)

    <?php
$test_array = Array
(
array
        (
            'Trx ID                  Member ID                  NoHP Pengirim                  Waktu Mulai                  Waktu Selesai                  NoHP Di isi                  Kode Voucher                  Keterangan',
            '1045629                          SY3345                          +6288215527644                          2015-08-28 16:00:32                          2015-08-28 16:01:32                          081325415747                                              TS100                          SUKSES',
             '1042300                          SY3345                          +6288215527644                          2015-08-22 18:45:41                          2015-08-22 18:48:55                          081325415747                                              TS20                          SUKSES',
             '1037299                          SY3345                          jpcom_cs                          2015-08-14 20:08:49                          2015-08-14 20:11:58                          081325415747                                              TS50                          SUKSES'
        ),
array(),
array()
);

//This is to filter out empty array elements to reduce number of iterations
$test_array = array_filter($test_array);

//String to hold values to be inserted
$str_values_to_insert = '';

foreach($test_array as $value)
{
    foreach($value as $key => $value_to_insert)
    {
        //Skip the headers section here
        if($key === 0)
        {
            continue;
        }
        else
        {
            $str_values_to_insert.= "('".str_replace('                          ', "', ", $value_to_insert)."'), ";
        }
    }

}

$str_values_to_insert = rtrim($str_values_to_insert, ', ');

$str_insert_sql = "INSERT INTO TABLE XYZ ('Trx ID','Member ID', 'NoHP Pengirim', 'Waktu Mulai', 'Waktu Selesai', 'NoHP Di isi                  Kode Voucher', 'Keterangan') VALES {$str_values_to_insert}";

print_r($str_insert_sql);
exit;
?>

希望这有帮助