2016-02-11 74 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循環,否則你最終會得到一堆未封閉的表標籤。