PHP变量不在while循环中更新

时间:2016-03-14 17:27:29

标签: php woocommerce

我正在编写一个小型PHP脚本,以根据客户发票生成CSV文件。我注意到在我的回显线上生成订单,只有$ order_date正在更新。其他变量没有变化。如果我回显$ rowprops [1],我确实看到我正确地走了行集。

我不太了解PHP以发现我的错误。

if ($result_daterange_orders = mysqli_query($con, $query_daterange_orders))
{
echo "Date,Order #,Gross,VAT,Net,VAT registration notes";
while ($row = mysqli_fetch_row($result_daterange_orders)) {
    $order_id = $row[0];
$query_is_order_eu = "SELECT 1 from wp_postmeta WHERE post_id = " . $order_id . " AND meta_key = '_shipping_country' AND meta_value IN
(
'AT',
'BE',
'BG',
'CH',
'CY',
'CZ',
'DE',
'DK',
'EE',
'ES',
'FI',
'FR',
'GB',
'GR',
'HR',
'HU',
'IE',
'IT',
'LT',
'LU',
'LV',
'MT',
'NL',
'PL',
'PT',
'RO',
'SE',
'SI',
'SK',
'IM',
'MC'
)";
if ($result_is_order_eu = mysqli_query($con, $query_is_order_eu)) {
    if (mysqli_num_rows($result_is_order_eu) != 0) { /* Order is in EU */
        mysqli_free_result($result_is_order_eu);
        $properties_query = "SELECT meta_key, meta_value FROM wp_postmeta WHERE post_id = " . $order_id . " AND meta_key = '_wcpdf_invoice_date'
            OR meta_key = '_wcpdf_invoice_number' OR meta_key = '_order_tax' OR meta_key = '_order_shipping_tax'
            OR meta_key = '_order_total'";
        if ($result_properties = mysqli_query($con, $properties_query)) {
            if (mysqli_num_rows($result_properties) != 0) { /* We have properties */
            $order_date;
            $invoice_id;
            $gross;
            $vat_total = 0;
            $vat_calced = 0;
            $net;
            $vnotes = "";
            while ($rowprops = mysqli_fetch_row($result_properties)) {
                //echo $rowprops[1]; different every time
                if (strcmp($rowprops[0], "_wcpdf_invoice_number") === 0) 
                    $invoice_id = $rowprops[1]; 
                if (strcmp($rowprops[0], "_wcpdf_invoice_date") === 0)
                    $order_date = $rowprops[1];
                if (strcmp($rowprops[0], "_order_total") === 0)
                    $gross = $rowprops[1];
                if (strcmp($rowprops[0], "_order_tax") === 0) {
                    $vat_total += $rowprops[1];
                    $vat_calced +=1;
                }
                if (strcmp($rowprops[0], "_order_shipping_tax") === 0) {
                    $vat_total += $rowprops[1];
                    $vat_calced +=1;
                }
                if ($vat_calced == 2 && $vat_total === 0) /* VAT free customer */
                    $vnotes = "Order ID " . $order_id . " is VAT exempt. Registration number is "; 
                if ($vat_calced == 2 && $vat_total !== 0) /* Work out bottom line */
                    $net = $gross - $vat_total;
            }
            // Format: Date, Order #, Gross, VAT, Net, VAT reg notes //
            mysqli_free_result($result_properties);
            echo $order_date . "," . $invoice_id . "," . $gross . "," . "," . $vat_total . "," . $net . "," . $vnotes . "\n";   
            }
        }

    }
}
}
mysqli_free_result($result_daterange_orders);
}
mysqli_close($con);
?>

谢谢 - 我不确定为什么只有$ order_date正在更新。起初我担心我正在重复循环但是如果你检查我的注释回声,它每次都会打印一个唯一的值。

2 个答案:

答案 0 :(得分:0)

在我看来,在完全运行循环之后回复你,因此fetch_row的每次迭代都没有被回显,只有最后一次获取。

你的外部循环在order_date上,所以它会改变,因为echo在该循环内部,而它仍然在内部循环之外,它正在迭代你的其他变量信息。

while ($rowprops = mysqli_fetch_row($result_properties)) {
    if ()...
    if ()...
    //echo is inside this inner loop so vars will update with every iteration
    echo $order_date . "," . $invoice_id . "," . $gross . "," . "," . $vat_total . "," . $net . "," . $vnotes . "\n";   
}
mysqli_free_result($result_properties);

答案 1 :(得分:0)

在这种情况下,很难说究竟是什么问题,因为存在嵌套循环,内循环结果取决于发布到方法的数据。例如。 $query_daterange_orders - 这个变量总是一样吗?

尝试将问题分解为更小的部分。 1 - 从第1行开始 - 确保$ query_daterange_orders始终相同。然后更深入,检查下一行的结果。