0

我正在尝试从 CSV 文件创建 SQL INSERT 语句。我已成功打开并读取文件。我什至以表格格式输出文件。但是,我在 for 循环中所做的更改(例如何时$c == 0不起作用)。它只是像在 csv 文件中一样输出到表中。这就是我想要改变的!为了保持这个例子,我试图让“John Doe”这个名字成为“john”和“Doe”。CSV 文件的名称为一个,我想分成第一个和最后一个。

此外,电话号码也没有改变。更改它们的代码以$c == 5. 有趣的是当我把它们放在这里时:http: //ideone.com/HfGXJk它工作正常。

<?php

fgetcsv_PHP();

function fgetcsv_PHP()
{
if (($handle = fopen("guests.csv", "r")) !== FALSE)
{

    $length = 1000;
    $delimiter = ",";

    $fname = array();
    $lname = array();
    $address = array();
    $city = array();
    $state = array();
    $zip = array();
    $phone = array();
    $email = array();

    //print opening table
    echo "<table border='1'>\n";


    while ( ( $data = fgetcsv( $handle, $length, $delimiter ) ) !== FALSE )
    {
        // Count number of array elements in $data
        $num = count($data);

        // Print opening table row HTML tag
        echo "<tr>\n";

        //loop through array
        for ($c=0; $c < $num; $c++)
        {
            if ($c == 0)
            {
                $name = $c;
                $name = explode(" ",$name);
                $first = array_shift($name);
                $last = array_pop($name);
                array_push($fname, $first);
                array_push($lname, $last);
                echo "<td>".$data[$first]."</td>\n";
            }
            if ($c == 1)
            {
                array_push($address, $c);
                echo "<td>".$data[$c]."</td>\n";
            }
            if ($c == 2)
            {
                array_push($city, $c);
                echo "<td>".$data[$c]."</td>\n";
            }
            if ($c == 3)
            {
                array_push($state, $c);
                echo "<td>".$data[$c]."</td>\n";
            }
            if ($c == 4)
            {
                array_push($zip, $c);
                echo "<td>".$data[$c]."</td>\n";
            }
            if ($c ==5)
            {
                $phnum = $c;
                $phnum = preg_replace('~[^0-9]~','',$phnum);
                array_push($phone, $phnum);
                echo "<td>".$data[$phnum]."</td>\n";
            }
            if ($c == 6)
            {
                array_push($email, $c);
                echo "<td>".$data[$c]."</td>\n";
            }
        }

        // Print closing table row HTML tag
        echo "</tr>\n";
    }

    // Print close table HTML tag
    echo "</table>";

    // Close the file pointed to by $handle
    fclose($handle);
}
}
?>
4

2 回答 2

1

您发布到其他站点的代码不是您在此处发布的代码。如果这行得通,那很好。跟这个关系不大:

if ($c ==5)
{
    $phnum = $c;
    $phnum = preg_replace('~[^0-9]~','',$phnum);
    array_push($phone, $phnum);
    echo "<td>".$data[$phnum]."</td>\n";
}

$phnum。您要做的第一件事是将其设置为$c,即5。然后删除 中的所有非数字字符5,将结果推送到您似乎不使用的数组中,然后输出$data[$phnum],即$data[5]原始数据。

于 2013-11-06T22:48:00.477 回答
1

读取名称的部分,将 $name 设置为 0,在不存在的空间将其分解,将数组的第一个元素(来自分解)中的 0 放入 $first 并输出 $data[$first],意思是 $data[ 0] - 原始值。

重构为 PHP 5.5:

$file = new SplFileObject('guests.csv', 'r');
$file->setFlags(SplFileObject::READ_CSV);
$file->setCsvControl(',', '"');

$converter = function($traversable) {
  foreach ($traversable as $data) {
    list($first, $last) = explode(' ', $data[0]);
    $address = $data[1];
    $city = $data[2];
    $state = $data[3];
    $zip = $data[4];
    $phone = preg_replace('([^\d])', '', $data[5]);
    $email = $data[6];

    $result = array(
      'first' => $first,
      'last' => $last,
      'address' => $address,
      'city' => $city,
      'state' => $state,
      'zip' => $zip,
      'phone' => $phone,
      'email' => $email,
    );
    yield $result;
  }
};

foreach ($converter($file) as $data) {
  var_dump($data);
} 
于 2013-11-06T23:29:32.607 回答