【问题标题】:while loop inside foreach loop returns only 1 rowforeach 循环内的 while 循环仅返回 1 行
【发布时间】:2016-05-22 22:22:17
【问题描述】:

我正在尝试使用 php 和 ajax 输出表格。现在我的函数从 1 个表中获取数据,然后迭代该表的行以从不同的表中获取行,然后形成一个表。为了实现这一点,我在 foreach 循环中放置了 while 循环,第一个表中的数据在数组中,所以我使用 forean 对数组进行迭代,然后放入 while 循环。 这导致只有 1 行。需要帮助

public function GetReportData()
{
   $status_list = $this->GetStatusList();

    foreach ($status_list as $status)
    {
        $staus_orignal = $status->orignal_status;
        $status_site =  $status->site_status;
        try
        {
        $db = $this->GetDBHandle();
        $start_date = '05/01/2015';
        $end_date = '05/02/2015';
        $affiliate_id = 0;

        $output_string = '';
        $output_string .=  '<table class="tg">
          <tr>
            <th class="tg-031e"><span style="color:#fff;">Disposition Type</span></th>
            <th class="tg-yw4l"><span style="color:#fff;">Lead Count</span></th>
            <th class="tg-yw4l"><span style="color:#fff;">Revenue</span></th>
          </tr>';

        $query = "exec affiliate_portal_report_data_select $staus_orignal, $affiliate_id,'". $start_date."','". $end_date."'";
        $result = odbc_exec ( $db, $query );

        if ( !$result )
        {
             throw new Exception ( 'Error from  ' . $query . ': ' . odbc_errormsg() );
        }

        else
        {
            while ( odbc_fetch_row ( $result ) )
            {

               $lead_count = odbc_result( $result, 'leadcount' );
               $revenue = odbc_result( $result, 'revenue' );
               $output_string .= '<tr>
                <td class="tg-yw4l">'.$status_site.'</td>
                <td class="tg-yw4l">'.$lead_count.'</td>
                <td class="tg-yw4l dollar">'.$revenue.'</td>
              </tr>';
            }

        }
        }

    catch ( Exception $e )        
        {
        $error_status = $e->getMessage();
        print $error_status;
        }

    }
    $output_string .=  '</table>';
    return $output_string;
}

【问题讨论】:

  • 如果你放一个“var_dump(odbc_num_rows ($result));”在 while 循环上方,它说你在 $result 对象中有多少行?
  • 看起来你需要在 foreach 循环外部初始化你的$output_string

标签: php sql-server foreach while-loop


【解决方案1】:

正如 @Don'tPanic 在 cmets 中所说,您必须在循环外初始化您的 $output_string 变量。

与实际一样,您正在为每一行重新创建一个空字符串。
如果您通过循环另一个数组或字符串来构建一个数组或字符串,请记住在外部声明变量,在循环内部进行增量。

将您的代码更改为:

$output_string = '';

foreach ($status_list as $status) {
    // ...
    $output_string .= 'yourstring';
    // ...
}

【讨论】:

    【解决方案2】:

    第 16 行有一个 $output_string = '';。您在 foreach 循环的每次迭代中都重新初始化输出,因此您只会得到最后一行。

    我不完全确定你的问题,但如果这段代码应该产生 one 表,那么你可以完全摆脱$output_string = '';,把这个:

    $output_string =  '<table class="tg">
    <tr>
        <th class="tg-031e"><span style="color:#fff;">Disposition Type</span></th>
        <th class="tg-yw4l"><span style="color:#fff;">Lead Count</span></th>
        <th class="tg-yw4l"><span style="color:#fff;">Revenue</span></th>
    </tr>';
    

    foreach 循环之前,然后离开:

    $output_string .=  '</table>';
    

    foreach 循环之后(就像它已经是一样)。

    但是如果你的代码应该生成多个表,那么你仍然需要去掉$output_string = '';,你可以将&lt;th&gt;部分留在原处,但你会需要将$output_string .= '&lt;/table&gt;'; 移入 foreach 循环,否则你会得到一堆未闭合的表格标签。

    【讨论】:

      猜你喜欢
      • 2017-11-09
      • 2016-03-14
      • 2021-09-24
      • 1970-01-01
      • 2017-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多