如果声明如何保持全球反击?

时间:2014-07-23 18:25:55

标签: php

我目前正在尝试解析大型PHP数据数组,并将每10,000个联系人写入单独的XML文件。我已经找到了成功完成此操作的方法,但出于某种原因,我的全局计数器$i并未通过我的一些if语句保持一致。这是嵌套在我的while($row=oci_fetch_array($array)语句中的代码,之后我对数据进行了一些验证。我还在此之前直接设置变量$i=0;

global $i;

if ($i<10001) {
    global $file1;
    fwrite($file1, '<contact contactID="'.$row["UNIQUE ID"].'" action="AddOrModify">
        <contactField name="LastName">'.$row["LAST NAME"].'</contactField>
        <contactField name="FirstName">'.$row["FIRST NAME"].'</contactField>
        <contactField name="MiddleName">'.$row["MIDDLE INITIAL"].'</contactField>
        <contactField name="Country">'.$row["COUNTRY"].'</contactField>
        <contactField name="Code">'.$row["CODE"].'</contactField>
        <contactField name="CustomField" customName="'.$row["CUSTOM LABEL 1"].'">'.$row["CUSTOM VALUE 1"].'</contactField>
        <contactField name="CustomField" customName="'.$row["CUSTOM LABEL 2"].'">'.$row["CUSTOM VALUE 2"].'</contactField>
        <contactField name="CustomField" customName="'.$row["CUSTOM LABEL 3"].'">'.$row["CUSTOM VALUE 3"].'</contactField>
        <contactField name="CustomField" customName="'.$row["CUSTOM LABEL 4"].'">'.$row["CUSTOM VALUE 4"].'</contactField>
        <contactField name="CustomField" customName="'.$row["CUSTOM LABEL 5"].'">'.$row["CUSTOM VALUE 5"].'</contactField>
        <contactField name="CustomField" customName="'.$row["CUSTOM LABEL 6"].'">'.$row["CUSTOM VALUE 6"].'</contactField>
        <contactField name="CustomField" customName="'.$row["CUSTOM LABEL 7"].'">'.$row["CUSTOM VALUE 7"].'</contactField>
        <contactField name="CustomField" customName="'.$row["CUSTOM LABEL 8"].'">'.$row["CUSTOM VALUE 8"].'</contactField>

        <contactPointList>
            <contactPoint type="Voice">
                <contactPointField name="Label">'.$row["PHONE LABEL 1"].'</contactPointField>
                <contactPointField name="CountryCode">'.$row["PHONE 1 COUNTRY CODE"].'</contactPointField>
                <contactPointField name="Number">'.$row["PHONE 1"].'</contactPointField>
                <contactPointField name="Extension">'.$row["PHONE EXTENSION 1"].'</contactPointField>
            </contactPoint>

            <contactPoint type="Voice">
                <contactPointField name="Label">'.$row["PHONE 2 LABEL"].'</contactPointField>
                <contactPointField name="CountryCode">'.$row["PHONE 2 COUNTRY CODE"].'</contactPointField>
                <contactPointField name="Number">'.$row["PHONE 2"].'</contactPointField>
                <contactPointField name="Extension">'.$row["PHONE EXTENSION 2"].'</contactPointField>
            </contactPoint>

            <contactPoint type="Email">
                <contactPointField name="Label">'.$row["EMAIL LABEL 1"].'</contactPointField>
                <contactPointField name="Address">'.$row["EMAIL 1"].'</contactPointField>
            </contactPoint>
        </contactPointList>
    </contact>');

    $i++;
}

if ($i>10000 and $i<20001) {
    global $file2;
    fwrite($file2,'<contact contactID="'.$row["UNIQUE ID"].'" action="AddOrModify">
        <contactField name="LastName">'.$row["LAST NAME"].'</contactField>
        <contactField name="FirstName">'.$row["FIRST NAME"].'</contactField>
        <contactField name="MiddleName">'.$row["MIDDLE INITIAL"].'</contactField>
        <contactField name="Country">'.$row["COUNTRY"].'</contactField>
        <contactField name="Code">'.$row["CODE"].'</contactField>
        <contactField name="CustomField" customName="'.$row["CUSTOM LABEL 1"].'">'.$row["CUSTOM VALUE 1"].'</contactField>
        <contactField name="CustomField" customName="'.$row["CUSTOM LABEL 2"].'">'.$row["CUSTOM VALUE 2"].'</contactField>
        <contactField name="CustomField" customName="'.$row["CUSTOM LABEL 3"].'">'.$row["CUSTOM VALUE 3"].'</contactField>
        <contactField name="CustomField" customName="'.$row["CUSTOM LABEL 4"].'">'.$row["CUSTOM VALUE 4"].'</contactField>
        <contactField name="CustomField" customName="'.$row["CUSTOM LABEL 5"].'">'.$row["CUSTOM VALUE 5"].'</contactField>
        <contactField name="CustomField" customName="'.$row["CUSTOM LABEL 6"].'">'.$row["CUSTOM VALUE 6"].'</contactField>
        <contactField name="CustomField" customName="'.$row["CUSTOM LABEL 7"].'">'.$row["CUSTOM VALUE 7"].'</contactField>
        <contactField name="CustomField" customName="'.$row["CUSTOM LABEL 8"].'">'.$row["CUSTOM VALUE 8"].'</contactField>

        <contactPointList>
            <contactPoint type="Voice">
                <contactPointField name="Label">'.$row["PHONE LABEL 1"].'</contactPointField>
                <contactPointField name="CountryCode">'.$row["PHONE 1 COUNTRY CODE"].'</contactPointField>
                <contactPointField name="Number">'.$row["PHONE 1"].'</contactPointField>
                <contactPointField name="Extension">'.$row["PHONE EXTENSION 1"].'</contactPointField>
            </contactPoint>

            <contactPoint type="Voice">
                <contactPointField name="Label">'.$row["PHONE 2 LABEL"].'</contactPointField>
                <contactPointField name="CountryCode">'.$row["PHONE 2 COUNTRY CODE"].'</contactPointField>
                <contactPointField name="Number">'.$row["PHONE 2"].'</contactPointField>
                <contactPointField name="Extension">'.$row["PHONE EXTENSION 2"].'</contactPointField>
            </contactPoint>

            <contactPoint type="Email">
                <contactPointField name="Label">'.$row["EMAIL LABEL 1"].'</contactPointField>
                <contactPointField name="Address">'.$row["EMAIL 1"].'</contactPointField>
            </contactPoint>
        </contactPointList>
    </contact>');

    $i++;
}

if ($i>20000 and $i<30001) {
    global $file3;

    fwrite($file3, '<contact contactID="'.$row["UNIQUE ID"].'" action="AddOrModify">
        <contactField name="LastName">'.$row["LAST NAME"].'</contactField>
        <contactField name="FirstName">'.$row["FIRST NAME"].'</contactField>
        <contactField name="MiddleName">'.$row["MIDDLE INITIAL"].'</contactField>
        <contactField name="Country">'.$row["COUNTRY"].'</contactField>
        <contactField name="Code">'.$row["CODE"].'</contactField>
        <contactField name="CustomField" customName="'.$row["CUSTOM LABEL 1"].'">'.$row["CUSTOM VALUE 1"].'</contactField>
        <contactField name="CustomField" customName="'.$row["CUSTOM LABEL 2"].'">'.$row["CUSTOM VALUE 2"].'</contactField>
        <contactField name="CustomField" customName="'.$row["CUSTOM LABEL 3"].'">'.$row["CUSTOM VALUE 3"].'</contactField>
        <contactField name="CustomField" customName="'.$row["CUSTOM LABEL 4"].'">'.$row["CUSTOM VALUE 4"].'</contactField>
        <contactField name="CustomField" customName="'.$row["CUSTOM LABEL 5"].'">'.$row["CUSTOM VALUE 5"].'</contactField>
        <contactField name="CustomField" customName="'.$row["CUSTOM LABEL 6"].'">'.$row["CUSTOM VALUE 6"].'</contactField>
        <contactField name="CustomField" customName="'.$row["CUSTOM LABEL 7"].'">'.$row["CUSTOM VALUE 7"].'</contactField>
        <contactField name="CustomField" customName="'.$row["CUSTOM LABEL 8"].'">'.$row["CUSTOM VALUE 8"].'</contactField>

        <contactPointList>
            <contactPoint type="Voice">
                <contactPointField name="Label">'.$row["PHONE LABEL 1"].'</contactPointField>
                <contactPointField name="CountryCode">'.$row["PHONE 1 COUNTRY CODE"].'</contactPointField>
                <contactPointField name="Number">'.$row["PHONE 1"].'</contactPointField>
                <contactPointField name="Extension">'.$row["PHONE EXTENSION 1"].'</contactPointField>
            </contactPoint>

            <contactPoint type="Voice">
                <contactPointField name="Label">'.$row["PHONE 2 LABEL"].'</contactPointField>
                <contactPointField name="CountryCode">'.$row["PHONE 2 COUNTRY CODE"].'</contactPointField>
                <contactPointField name="Number">'.$row["PHONE 2"].'</contactPointField>
                <contactPointField name="Extension">'.$row["PHONE EXTENSION 2"].'</contactPointField>
            </contactPoint>

            <contactPoint type="Email">
                <contactPointField name="Label">'.$row["EMAIL LABEL 1"].'</contactPointField>
                <contactPointField name="Address">'.$row["EMAIL 1"].'</contactPointField>
            </contactPoint>
        </contactPointList>
    </contact>');

    $i++;
}

这似乎工作得非常完美,但$ file1中的最后一次联系也显示为$file2中的第一个联系人(同样适用于$file2$file3) 。我曾多次尝试弄乱if ($i>x and $i<y)语句,但似乎无法做到恰到好处。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

在条件体结束时$i++$i = 1000后,下一个条件变为真。连续条件或incerement迭代计数器使用else if () {}一次 - 在所有if语句之后(在while循环块结束时)。

看起来只有文件名依赖于迭代计数器 - 如果它保持不变,则无需重复整个联系人体。像这样举例如:

$files = ['filename1.xml', 'filename2.xml',...];
$i = 0;
while (...) {
    $file_index = intval($i / 1000);
    if (!isset($files[$file_index])) { break; }
    //to be sure all data is written (no break) you need to define more files or assign names with variable: $filename = 'file_' . $file_index;
    $filename = $files[$file_index];
    fwrite($filename, '...');
    $i++;
}

如果您需要知道何时更改文件(将xml标头添加到字符串?),您可以使用以下条件:

if ($i % 1000 == 0) { ... }