【问题标题】:PHP For Loop - No Output After LoopPHP For循环 - 循环后没有输出
【发布时间】:2016-09-17 13:10:52
【问题描述】:

我有一个 foreach 循环,它工作正常 - 但由于某种原因,在它发生后任何超出循环的东西都不会输出。例如 echo $cpaTotal;不显示任何内容。

For 循环输出完美

我哪里出错了?

    $key = '84abf';
    $from = '2016/7/1';
    $to = '2016/9/17';
    $url = '/api/affreporting.asp?key='.$key.'&reportname=QuickSummaryReportDetailed&reportformat=xml&reportmerchantid=0&reportstartdate='.$from.'&reportenddate='.$to;
    $clean_xml = str_ireplace(['SOAP-ENV:', 'SOAP:'], '', file_get_contents($url));
    $xml = simplexml_load_string($clean_xml);
    $obj = json_decode(json_encode($xml));

    $cpaTotal = 'test';

    foreach($obj->Body->reportresponse->row as $row)
    {

        $CPACommission = $row->CPACommission;
        $merchant = $row->merchantname;
        $Product1NetRevenue = $row->Product1NetRevenue;

        echo "Merchant: ".$merchant. " | ";
        echo "CPA Commission: ".$CPACommission." | ";
        echo "Net Revenue: ".$Product1NetRevenue."</br>";
        echo "<hr>";

    }
    echo $cpaTotal;

输出 (html)

Merchant: <strong>mFortune</strong> | CPA Commission: <strong> &pound;2270</strong> | Downloads: <strong>34</strong> | Net Revenue: <strong>&pound;-161.4</strong><br>Merchant: <strong>PocketWin</strong> | CPA Commission: <strong> &pound;1330</strong> | Downloads: <strong>22</strong> | Net Revenue: <strong>&pound;-134.77</strong><br>Merchant: <strong>Mr Spin</strong> | CPA Commission: <strong> &pound;680</strong> | Downloads: <strong>31</strong> | Net Revenue: <strong>&pound;13.22</strong><br>

【问题讨论】:

  • 你没有设置 $cpaTotal
  • 我不明白为什么这不起作用,而且它不起作用也没有任何意义。您需要向我们提供minimal reproducible example
  • 抱歉刚刚更新 where $cpaTotal = 'test';包括
  • 什么是?
  • 我已添加完整代码 - 显然 URL 不起作用,我不得不更改密钥

标签: php for-loop


【解决方案1】:

如果您有时间并愿意在代码中添加一些额外内容(仅用于调试目的),那么您可能想尝试以下代码。请注意,下面的代码与您的代码之间基本上没有太大区别,除了输出没有立即回显的事实。

这样做的唯一原因是您可以简单地将echo $output 行注释掉,以查看echo $cpaTotal 是否显示。如果它显示,那么这应该是一个提示,你的一些 HTML 标记来自你的数据(在 foreach 循环块内)被吞了。

<?php

    $key        = '84abf';
    $from       = '2016/7/1';
    $to         = '2016/9/17';
    $url        = '/api/affreporting.asp?key='.$key.'&reportname=QuickSummaryReportDetailed&reportformat=xml&reportmerchantid=0&reportstartdate='.$from.'&reportenddate='.$to;
    $clean_xml  = str_ireplace(['SOAP-ENV:', 'SOAP:'], '', file_get_contents($url));
    $xml        = simplexml_load_string($clean_xml);
    $obj        = json_decode(json_encode($xml));               
    $cpaTotal   = 'test';
    $output     = '';

    foreach($obj->Body->reportresponse->row as $row){                   
        $CPACommission      = $row->CPACommission;
        $merchant           = $row->merchantname;
        $Product1NetRevenue = $row->Product1NetRevenue;

        // INSTEAD OF INSTANT ECHO,
        // TRY DELAYED ECHO BY HOLDING YOUR OUTPUT
        // INSIDE A VARIABLE LIKE THIS:                 
        $output .= "Merchant: "         . $merchant             . " | ";
        $output .= "CPA Commission: "   . $CPACommission        . " | ";
        $output .= "Net Revenue: "      . $Product1NetRevenue   . "</br>";
        $output .= "<hr>";                  
    }

    // BE ALSO AWARE THAT YOUR DATA CONTAINS RAW HTML...
    // SOMETIMES, UNCLOSED HTML TAGS OR SEMANTICALLY INACCURATE TAGS
    // COULD CAUSE A "SWALLOW" EFFECT.... WHICH MEANS, YOUR DATA IS ACTUALLY RENDERED
    // BUT NOT VISIBLE IN THE BROWSER DUE TO THE AFOREMENTIONED ERRORS.
    echo $output;
    echo $cpaTotal;

【讨论】:

  • 谢谢你,我现在就去试试——我对 PHP 不是很熟练,但会试一试!
【解决方案2】:

&lt;/br&gt; 不是有效的 HTML,请使用 &lt;br&gt; or &lt;br /&gt; 来兼容 HTML 和 XHMTL。

【讨论】:

    【解决方案3】:

    您应该在循环的最后一行使用&lt;hr /&gt; 而不是只使用&lt;hr&gt;

    【讨论】:

    • 那呢?
    • 我已经完全删除了
      行,仍然没有运气
    猜你喜欢
    • 2011-09-30
    • 1970-01-01
    • 2022-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多