2016-02-11 62 views
2

我想输出一个使用php和ajax表。现在我的函数从1个表中获取数据,然后迭代该表的行以从不同的表中获取行,然后形成一个表。为了实现这一点,我已经把循环放在foreach循环中,第一个表的数据在数组中,所以我使用forean在数组上迭代,然后放置while循环。 这导致只有1行。需要帮助而foreach循环内循环只返回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; 
} 
+0

如果你把“var_dump(odbc_num_rows($ result));”在while循环之上,它说你在$ result对象中有多少行? – Hiphop03199

+1

看起来您需要初始化您的'$ output_string' _outside_ foreach循环。 –

回答

0

正如所说的@ Don'tPanic的评论,你必须初始化循环您$output_string变量之外。

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

你的代码更改为:

$output_string = ''; 

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

上有16行要重新初始化您的foreach循环的每次迭代输出$output_string = '';,所以你将只得到了最后一排。在foreach之前

$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>'; 

我不是从你的问题完全肯定,但如果这个代码应该产生一个表,那么你可以得到完全摆脱$output_string = '';,把这个循环,离开这个:

$output_string .= '</table>'; 

foreach循环(就像它已经是)。

但是如果你的代码应该产生多个表,那么你仍然需要摆脱$output_string = '';,你可以离开<th>部分,在那里它是,但你需要移动$output_string .= '</table>';的foreach循环,否则你最终会得到一堆未封闭的表标签。