我从PHP循环中格式化HTML [英] Ill formatted HTML from a PHP loop

查看:133
本文介绍了我从PHP循环中格式化HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在循环一个数组并构建表。 HTML然后被发送到DOMPDF。但是,如果HTML格式不正确,DOMPDF将不会创建PDF。我想这就是我的情况。这里是我的循环:

 <?php foreach($ credit as $ credit):?> 
<?php if($ credit ['credit_type'] ==short):?>
< tr>
< td>< strong><?php echo $ credit ['category_title']; ?>< /强>< / 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):?>
< tbody>
<?php endif; ?>
< tr>
<?php if($ credit ['category_title']!= $ oldvalue):?>
< td width =25%>< strong><?php echo trim($ credit ['category_title']); ?>< /强>< / TD>
< td width =25%>< strong>标题< / strong>< / td>
< td width =25%>< strong>角色< / 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; ?>

我不能为我的生活找出哪个标签不会关闭。如果有人可以提供一些洞察力,这将是fab!

特别是,循环创建显示一些标题行,然后吐出更多的行,每当类别标题更改。

解决方案

如果不知道更多关于数据的信息,有点困难。例如,为什么短期信用表是打开和关闭的记录表,但长期信用表是以前一条记录为条件的?是因为你有一个扁平的数据结构,所以相关的数据显示为一系列连续的行?如果情况是这样的话,如果数据更规范一些的话,事情会更容易些。即你可以遍历每个信用记录,然后通过细节分开。修正你的数据结构的任何可能性?



分析你的代码,你的问题似乎是在代码的第二部分的逻辑。您在循环结束时设置变量 $ oldvalue 的值。这是关闭表的逻辑之后。因此,如果解析两个具有相同类别标题的记录,则第二个记录将完全在表格外输出表格行(不必介意它也将具有完全空行)。另外,如果你有一个很长的信用类型,那么表格将永远不会被关闭。



这就是说,和你一样,我猜你可能需要如下所示:

  //为第一次迭代构建一个虚拟的前一个记录,以便条件不会中断。 
<?php $ previous_credit = array('credit_type'=> null,'category'=> null); ?>
<?php foreach($ credit 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):?>
< tr>
< td>< strong><?php echo $ credit ['category_title']; ?>< /强>< / 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']):?>
< tbody>
< tr>
< td width =25%>< strong><?php echo trim($ credit ['category_title']); ?>< /强>< / TD>
< td width =25%>< strong>标题< / strong>< / td>
< td width =25%>< strong>角色< / 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; ?>

<! - 最后一个记录的最后一个表关闭 - >
< / tbody>< / table>

(这是一些难看的代码,我没有时间去修改它,所以...社区维基,以防其他人想清理它。)


I am looping through an array and building tables. The HTML is then sent to DOMPDF. However, DOMPDF will not create the PDF if the HTML is ill formatted. I assume that is what is happening in my case. Here is my loop:

<?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; ?>

I cannot for the life of me work out which tag I am not closing. If anyone could give some insight, that would be fab!

Specifically, the loop is creating rows that show some headings, and then spit out futher rows whenever the category title changes.

解决方案

It's a bit difficult to parse without known more about your data. For example, why is a table for "short" credit open and closed with the record, but the table for "long" credit is conditional on the previous record? Is it because you have a flat data structure so related data shows up as a series of consecutive rows? If that's the case things would be easier if the data were a bit more normalized. I.e. you could iterate through each credit record then through the details separately. Any possibility of fixing your data structure?

Analyzing the code you have, your problem appears to be in the logic for the second section of the code. You are setting the value of the variable $oldvalue at the end of the loop. This is after the logic that closes the table. So if you parse two records that have the same category title the second record will output it's table rows completely outside a table (never mind that it will also have a completely empty row). Additionally, if you have a short credit type following a long the table will never be closed.

That being said, working with what you have I'm guessing you may need something like the following:

// 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>

(That's some ugly code and I don't have time to keep revising it, so ... community wiki in case anyone else wants to clean it up.)

这篇关于我从PHP循环中格式化HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆