2012-01-06 24 views
1

我正在循環一個數組並構建表。 HTML然後被髮送到DOMPDF。但是,如果HTML格式不正確,DOMPDF將不會創建PDF。我想這就是我的情況。這是我的循環:來自PHP循環的格式錯誤的HTML

<?php foreach($credits as $credit) : ?> 
     <?php if($credit['credit_type'] == "short") : ?> 
      <table width="100%" cellpadding="0" cellspacing="0" border="0" style="margin:0px 0px 15px 0px;"> 
       <tr> 
        <td><strong><?php echo $credit['category_title']; ?></strong></td> 
       </tr> 
       <tr> 
        <td><?php echo $credit['credit_heading']; ?></td> 
       </tr> 
      </table> 
     <?php endif; ?> 

     <?php if($credit['credit_type'] == "long") : ?> 
      <?php if($credit['category_title'] != $oldvalue) : ?> 
       <table width="100%" cellpadding="0" cellspacing="0" border="0" style="margin:0px 0px 15px 0px;"> 
       <tbody> 
      <?php endif; ?> 
       <tr> 
        <?php if($credit['category_title'] != $oldvalue) : ?> 
          <td width="25%"><strong><?php echo trim($credit['category_title']); ?></strong></td>     
          <td width="25%"><strong>Title</strong></td> 
          <td width="25%"><strong>Role</strong></td> 
          <td width="25%"><strong>Director</strong></td> 
        <?php endif; ?> 
       </tr> 
       <tr> 
        <td width="25%"><?php echo $credit['credit_heading'];?></td> 
        <td width="25%"><?php echo $credit['credit_title']; ?></td> 
        <td width="25%"><?php echo $credit['credit_role']; ?></td> 
        <td width="25%"><?php echo $credit['credit_director']; ?></td> 
       </tr> 
       <?php if($credit['category_title'] != $oldvalue) : ?> 
        </tbody> 
        </table> 
       <?php endif; ?> 
       <?php $oldvalue = $credit['category_title']; ?> 
     <?php endif; ?> 
    <?php endforeach; ?> 

我不能爲我的生活工作出哪個標籤我不關閉。如果任何人都可以提供一些見解,那將是晶圓廠!

具體來說,循環創建顯示某些標題的行,然後在類別標題更改時再吐出更多行。

+0

最簡單的方法就是用'ob_start()'和'ob_end()'獲得整個HTML,但爲什麼你的PDF庫不工作與「格式不正確的HTML」...? – evotopid 2012-01-06 17:06:00

+0

那會怎麼樣?我將如何使用它? – Udders 2012-01-06 17:10:09

+0

您使用的是哪個版本的dompdf? dompdf可以處理格式錯誤的HTML,儘管有一些格式化問題會導致渲染失敗。另外,如果HTML在結構上不正確,則渲染可能不符合您的期望。 – BrianS 2012-01-06 18:01:36

回答

0

這可能是一個簡單的解決方案,但也許不是最好的:

,我建議你使用PHP的整潔類(最終你必須先安裝...)
Here is the link for the Tidy class Manual

在第一行:

ob_start(); 

此命令緩衝區的一切什麼是你的follwing腳本outputed。
下面的代碼應該添加在您的文件的末尾,或者您想要顯示輸出的地方。
它首先獲得與ob_get_contents()緩衝區,並清除代碼。
請注意,您最終必須根據需要更改配置參數,這確實非常非常重要。

$raw_output = ob_get_clean(); 

$config = array('indent' => true, 'output-xhtml' => true, 'wrap' => 0); 
$tidy = new Tidy; 
$tidy->parseString($raw_output, $config, 'utf8'); 
$tidy->cleanRepair(); 

echo $tidy; 

本實施例的代碼被的the example on php.net原始版本修改。

希望能幫到你。

0

如果不知道更多關於您的數據的信息,有點難以解析。例如,爲什麼「短期」信用表打開並關閉記錄,但「長期」信用表是以先前記錄爲條件的?是因爲你有一個扁平的數據結構,所以相關的數據顯示爲一系列連續的行?如果情況是這樣的話,如果數據更規範一些,情況會更容易。即您可以遍歷每個信用記錄,然後分別通過詳細信息。修復您的數據結構的任何可能性?

分析您的代碼,您的問題似乎在代碼的第二部分的邏輯。您正在循環結束時設置變量$oldvalue的值。這是關閉表格的邏輯之後。因此,如果解析具有相同類別標題的兩條記錄,則第二條記錄會將其表格行完全輸出到表格外(不必介意它也將具有完全空行)。此外,如果你有很長的信用類型,那麼表格永遠不會關閉。

話雖這麼說,你有什麼我猜你可能需要像以下工作:

// build a dummy "previous" record for the first iteration so the conditionals don't break. 
<?php $previous_credit = array('credit_type'=>null,'category'=>null); ?> 
<?php foreach($credits as $credit) : ?> 
    <?php if($credit['credit_type'] == "short" || ($previous_credit['credit_type'] == "long" && $previous_credit['category'] != $credit['category'])) : ?> 
       </tbody> 
       </table> 
    <?php endif; ?> 

    <?php if($credit['credit_type'] == "short") : ?> 
     <table width="100%" cellpadding="0" cellspacing="0" border="0" style="margin:0px 0px 15px 0px;"> 
      <tr> 
       <td><strong><?php echo $credit['category_title']; ?></strong></td> 
      </tr> 
      <tr> 
       <td><?php echo $credit['credit_heading']; ?></td> 
      </tr> 
     </table> 
    <?php endif; ?> 

    <?php if($credit['credit_type'] == "long") : ?> 
     <?php if($credit['category_title'] != $previous_credit['category_title']) : ?> 
      <table width="100%" cellpadding="0" cellspacing="0" border="0" style="margin:0px 0px 15px 0px;"> 
      <tbody> 
      <tr> 
       <td width="25%"><strong><?php echo trim($credit['category_title']); ?></strong></td>     
       <td width="25%"><strong>Title</strong></td> 
       <td width="25%"><strong>Role</strong></td> 
       <td width="25%"><strong>Director</strong></td> 
      </tr> 
     <?php endif; ?> 
      <tr> 
       <td width="25%"><?php echo $credit['credit_heading'];?></td> 
       <td width="25%"><?php echo $credit['credit_title']; ?></td> 
       <td width="25%"><?php echo $credit['credit_role']; ?></td> 
       <td width="25%"><?php echo $credit['credit_director']; ?></td> 
      </tr> 
    <?php endif; ?> 
    <?php $previous_credit = $credit; ?> 
<?php endforeach; ?> 

<!-- one last table close for the last record --> 
</tbody></table> 

(這是一些醜陋的代碼,我沒有時間繼續修改,所以...社區維基以防其他人想要清理它。)