【问题标题】:php generated td overlaps data when data is longer than column width当数据长于列宽时php生成的td重叠数据
【发布时间】:2014-06-21 12:28:12
【问题描述】:

我为“详细信息”生成了以下列

 echo '<td width="100" align="left" bgcolor="#FFFFFF">Cheque (' . $row4['number'] . ')'. $row1['date_added'] .'</td>';

这为我提供了下表的总体结果,其中特定列数据在太长时会重叠。我该怎么办?我曾尝试在两者之间添加'&lt;br/&gt;',但它从未奏效。

很遗憾,此表没有外部 CSS 文件,但以下是完整表

<table cellspacing="1" width="900" cellpadding="3" bgcolor="#CCCCCC" style="line-height:0px;margin-left: auto;margin-right: auto">
            <col width="64" span="10" />
            <tr height="46">
                <td height="46" width="600" colspan="10" bgcolor="#FFFFFF"><h1>Blah Blah</h1></td>               
            </tr>
            <?php
            include './DatabaseConnection.php';
            $db = new DatabaseConnection();
            $db->createDatabaseConnection();
            $cusid = $_GET['cusid'];
            $startDate=$_GET['startDate'];
            $endDate=$_GET['endDate'];

            $query2 = mysql_query("select cusname from customers where cusid='$cusid'");
            while ($row5 = mysql_fetch_array($query2)) {
                echo '<tr height="20" width="1000">
                <td height="10" colspan="10" bgcolor="#F3F3F3"><h2>Payment History of ' . $row5['cusname'] . ' (Credit)</h2></td>

            </tr><tr height="20">
                <td width="120" height="20" bgcolor="#FFFFFF"><h3>Date Added</h3></td>
                <td width="120" bgcolor="#FFFFFF"><h3 align="center">Invoice Number</h3></td>
                <td width="120" bgcolor="#FFFFFF"><h3 align="center">Invoice Amount</h3></td>
                <td width="120" bgcolor="#FFFFFF"><h3 align="center">Payed Amount</h3></td>
                <td width="120" bgcolor="#FFFFFF"><h3 align="center">Detail</h3></td>
                <td width="120" bgcolor="#FFFFFF"><h3 align="center">Balance</h3></td>
            </tr>';
            }

         //$query = mysql_query("SELECT * from payments where cusid ='$cusid'  and ingrtype = '1' and payment_status = '1' and (date_added >= '$startDate' and date_added <= '$endDate') order by invoiceGRN_id") or die(mysql_error());
         $query = mysql_query("SELECT * from payments where cusid ='$cusid'  and ingrtype = '1' and payment_status = '1' and (date_added >= '$startDate' and date_added <= '$endDate') order by invoiceGRN_id") or die(mysql_error());

            $lastinvoicenumber = null;

            while ($row1 = mysql_fetch_array($query)) {

                $invoiceAmount = $row1['subtotal'];
                $payedAmount = $row1['payment'];

                $invoicenumber = $row1['invoiceGRN_id'];
                $invoicetotal = 0;
                //$balanceAmount = $invoiceAmount - $payedAmount; 
                $balanceAmount = $invoiceAmount; 

                //diluk
                $queryinv = mysql_query("SELECT invoice_subtotal from invoice where invoice_number ='$invoicenumber' limit 1") or die(mysql_error());
                //diluk
                while ($rowinv = mysql_fetch_array($queryinv)) {

                    $invoicetotal = $rowinv['invoice_subtotal'];

                }


                echo '<tr height="20">
                <td width="100" align="left" bgcolor="#FFFFFF">' . $row1['date_added'] . '</td>';

                if($lastinvoicenumber!=$invoicenumber){
                echo '<td width="100" height="20" bgcolor="#FFFFFF">' . $row1['invoiceGRN_id'] . '</td>';
                echo '<td width="100" align="right" bgcolor="#FFFFFF">' . $invoicetotal . '</td>';
                }else{

                  echo '<td width="100" height="20" bgcolor="#FFFFFF"></td>';  
                  echo '<td width="100" align="right" bgcolor="#FFFFFF"></td>';  
                }




                echo '<td width="100" align="right" bgcolor="#FFFFFF">' . $row1['payment'] . '</td>';

                $invId = $row1['invoiceGRN_id'];
                if ($row1['paymentType'] == "Cheque") {
                    $query3 = mysql_query("select * from cheque where invoiceno='$invId'");
                    if ($row4 = mysql_fetch_array($query3)) {
                        echo '<td width="100" align="left" bgcolor="#FFFFFF">Cheque (' . $row4['number'] . ')'. $row1['date_added'] .'</td>';
                    }else{

                         echo '<td width="100" align="left" bgcolor="#FFFFFF">Cheque -' . $row1['date_added'] .'</td>';
                    }
                } else {
                    echo '<td width="100" align="left" bgcolor="#FFFFFF">' . $row1['paymentType'] . '</td>';
                }

                echo '<td width="100" align="right" bgcolor="#FFFFFF">' . $balanceAmount . '</td>
            </tr>';

                $lastinvoicenumber = $invoicenumber;
            }

            echo '<tr height="20" width="1000">
                <td height="10" colspan="10" bgcolor="#F3F3F3"><h2></h2></td></tr>';

            $queryx = mysql_query("SELECT * from invoice where customer_id ='$cusid' and payment_type !='Credit' group by invoice_number order by invoice_date") or die(mysql_error());
            while ($rowx = mysql_fetch_array($queryx)) {

                echo '<tr height="20">
                <td width="100" align="left" bgcolor="#FFFFFF">' . $rowx['invoice_date'] . '</td>
                <td width="100" height="20" bgcolor="#FFFFFF">' . $rowx['invoice_number'] . '</td>
                    <td width="100" height="20" bgcolor="#FFFFFF">' . $rowx['invoice_subtotal'] . '</td>
                <td width="100" height="20" bgcolor="#FFFFFF">' . $rowx['invoice_subtotal'] . '</td>';                               

                 $invId1 = $rowx['invoice_number'];
                if ($rowx['payment_type'] == "Cheque") {
                    $queryx1 = mysql_query("select * from cheque where invoiceno='$invId1'");
                    if ($rowx2 = mysql_fetch_array($queryx1)) {
                        echo '<td width="100" align="left" bgcolor="#FFFFFF">Cheque (' . $rowx2['number'] . ')</td>';
                    }
                } else {
                    echo '<td width="100" align="left" bgcolor="#FFFFFF">' . $rowx['payment_type'] . '</td>';
                }

                echo '<td width="100" align="right" bgcolor="#FFFFFF">0</td>
            </tr>';
            }

            ?>
        </table>

【问题讨论】:

  • 你能把这张表的css部分贴出来吗?
  • @Daan 不幸的是,这没有外部 CSS,但我现在已经包含了表格的完整代码

标签: php html css


【解决方案1】:

你可以检查

echo '<td width="100" align="left" bgcolor="#FFFFFF"><div style="width:200px; word-wrap: break-word;overflow: auto;">Cheque (' . $row4['number'] . ')'. $row1['date_added'] .'</div></td>';

【讨论】:

  • 它让数据消失了!
【解决方案2】:

我也遇到了同样的问题,只要在每个&lt;tr&gt;标签的末尾添加\n就可以了

echo "&lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt; \n"

【讨论】:

  • \n 这是没有意义的
【解决方案3】:

您需要考虑两件事:

1. table-layout: fixed;这个用于表格,并且

2. word-wrap: break-word;td 本身。

详细看这个问题:How to force table cell <td> content to wrap?

【讨论】:

    【解决方案4】:

    我通过将表格的“line-height”属性设置为 20px 来解决问题,如下所示,

    <table cellspacing="1" width="900" cellpadding="3" bgcolor="#CCCCCC" style="line-height:20px;margin-left: auto;margin-right: auto">
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-12
      • 2020-10-01
      • 1970-01-01
      • 2019-11-03
      • 2021-07-03
      • 1970-01-01
      相关资源
      最近更新 更多