简体   繁体   English

PHP 变量未在 while 循环中更新

[英]PHP variable not updating in while loop

I am writing a small PHP script to generate a CSV file based on customer invoices.我正在编写一个小的 PHP 脚本来根据客户发票生成一个 CSV 文件。 I'm noticing that on my echo line to generate the order, only $order_date is updating.我注意到在我的回声线上生成订单,只有 $order_date 正在更新。 The other variables aren't changing.其他变量没有变化。 If I echo $rowprops[1], I do see that I am walking the rowset correctly.如果我回显 $rowprops[1],我确实看到我正在正确地走行集。

I don't know enough about PHP to spot my mistake.我对 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);
?>

Thanks -- I'm not sure why only $order_date is updating.谢谢——我不确定为什么只有 $order_date 正在更新。 At first I was concerned that I was repeating the loop but if you check my commented echo, it prints a unique value every time.起初我担心我会重复循环,但是如果您检查我的注释回显,它每次都会打印一个唯一值。

Looks to me like you're echoing after the loop has been run through entirely, and therefore each iteration of fetch_row is not being echoed, only the last fetch.在我看来,您在循环完全运行后正在回显,因此 fetch_row 的每次迭代都没有被回显,只有最后一次获取。

Your outer loop is on order_date, so that will change because echo is inside that loop, while it remains outside the inner loop which is iterating through your other variable info.您的外循环在 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);

In this case, It's hard to say what exactly the problem is, as there are nested loops, and inner loop result depends on what data is posted to a method.在这种情况下,很难说到底是什么问题,因为存在嵌套循环,内循环结果取决于发布到方法的数据。 Eg.例如。 $query_daterange_orders - is this variable always the same? $query_daterange_orders - 这个变量总是一样的吗?

Try to break the problem into smaller parts.尝试将问题分解为更小的部分。 1 - starting from line 1 - make sure that $query_daterange_orders is always the same. 1 - 从第 1 行开始 - 确保 $query_daterange_orders 始终相同。 and then go deeper, checking results on the next line.然后更深入,检查下一行的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM