【问题标题】:alignment problems in php while loopphp while循环中的对齐问题
【发布时间】:2014-02-02 04:01:23
【问题描述】:

在这里我没有任何与编码相关的问题。我正在尝试从多个数据库表中获取详细信息。获取结果工作正常。但是对齐是我的问题。

您可以看到一张图片。在该图片中,凭证状态栏工作正常。但是,另外两列(房间状态和用餐状态值)总是在最后一行。如何正确对齐这些(房间和膳食状态)值(如凭证状态列)?我希望你能理解我的问题...

      <?php
        echo "<table width=1090 border=1 style=\"border: #ddd;\" align=center cellspacing=4 cellpadding=10>";
        echo "<tr class=thvoucher>";
        echo "<th width=30>Voucher Number</th>";
        echo "<th width=80>Reference Number</th>";
        echo "<th width=200>Guest Name</th>";
        echo "<th width=150>Voucher Status</th>";
        echo "<th width=150>Room Status</th>";
        echo "<th width=150>Meal Status</th>";
        echo "</tr>";
        for($i = $start; $i < $end; $i++) 
        {
            if ($i == $total_results) 
            {
                break;
            }
            while($row = $stmt->fetch())
            {
               $voucherid = $row['VoucherID'];
           $ref = $row['VoucherReference'];
           $gname = $row['GuestName'];
           $vstatus = $row['ActiveStatus'];
           echo "<tr class=voucherstyle" . $cls . ">";
           echo "<td>$voucherid</td>";    **// FIRST TD**
           echo "<td>$ref</td>";    **// SECOND TD**
           echo "<td>$gname</td>";     **// THIRD TD**

                      if( $vstatus != 'empty' )       **// FOURTH TD**
        {
            if( $vstatus == 'Y' )
            {
                echo "<td class=green >Active</td>";
            }
            else if( $vstatus == 'N' )
            {
                echo "<td class=red >Inactive</td>";
            }
        }
        else
            {
                echo "<td><form method=post onsubmit=\"return mstatusvalidate(this)\"; action='voucherstatus_update.php?id_meal= $voucherid '><select name=mealstatus><option value=empty></option><option value=active>Active</option><option value=inactive>Inactive</option></select><input type=submit class=update_status title=\"Update $gname voucher status\" value=Update></form></td>";
            }
            }

            while($r_row = $r_stmt->fetch())
            {
                $rstatus = $r_row['ActiveStatus'];

                        // same like above if else statement      **// FIFTH TD**
            }       

            while($m_row = $m_stmt->fetch())
            {
                $mstatus = $m_row['ActiveStatus']; 

                        // same like above if else statement  **// SIXTH TD**       
            }
echo "</tr>";
            }
            echo "</table>";
            ?>

【问题讨论】:

  • 您没有关闭循环内的 标记。此外,您声明了 6 个表格标题..但您只输出了其中的 3 个。
  • 您发布的代码似乎没有为您无法对齐的单元格发出任何 HTML。我看不出我们能如何提供帮助。
  • @Yami :我用过 6 td。我没有在这里发布 if else 语句代码。所以,只有你没有看到剩余的 td。我在 if else 语句中使用了另外 3 个 td。我已经更新了我的代码。再看

标签: php html css pdo alignment


【解决方案1】:

我使用这个 while 条件 while(($row = $stmt-&gt;fetch()) &amp;&amp; ($r_row = $r_stmt-&gt;fetch()) &amp;&amp; ($m_row = $m_stmt-&gt;fetch())) 让它工作了。在我使用 三个独立的 while 循环之前。现在,我有一些其他的想法。我试过了,现在它工作正常,对齐也很完美。在我没有发布我的 if else 语句代码之前。所以,每个人都认为我只使用了 3 个 td。很抱歉让大家感到困惑......我已经在下面发布了我的正确编码......

     <?php
        echo "<table width=1090 border=1 style=\"border: #ddd;\" align=center cellspacing=4 cellpadding=10>";
        echo "<tr class=thvoucher>";
        echo "<th width=30>Voucher Number</th>";
        echo "<th width=80>Reference Number</th>";
        echo "<th width=200>Guest Name</th>";
        echo "<th width=150>Voucher Status</th>";
        echo "<th width=150>Room Status</th>";
        echo "<th width=150>Meal Status</th>";
        echo "</tr>";
        for($i = $start; $i < $end; $i++) 
        {
            if ($i == $total_results) 
            {
                break;
            }
            while(($row = $stmt->fetch()) && ($r_row = $r_stmt->fetch()) && ($m_row = $m_stmt->fetch()))
            {
                $voucherid = $row['VoucherID'];
                $ref = $row['VoucherReference'];
                $gname = $row['GuestName'];
                $vstatus = $row['ActiveStatus'];
                echo "<tr class=voucherstyle" . $cls . ">";
                echo "<td>$voucherid</td>";       **1st TD**
                echo "<td>$ref</td>";      **2nd TD**
                echo "<td>$gname</td>";      **3rd TD**
                if( $vstatus != 'empty' )      **4th TD**
                {
                    if( $vstatus == 'Y' )
                    {
                        echo "<td class=green >Active</td>";
                    }
                    else if( $vstatus == 'N' )
                    {
                        echo "<td class=red >Inactive</td>";
                    }
                }
                else
                    {
                        echo "<td><form method=post onsubmit=\"return mstatusvalidate(this)\"; action='voucherstatus_update.php?id= $voucherid '><select name=mealstatus><option value=empty></option><option value=active>Active</option><option value=inactive>Inactive</option></select><input type=submit class=update_status title=\"Update $gname voucher status\" value=Update></form></td>";
                    }

                $rstatus = $r_row['ActiveStatus'];
                // if else statement      **5th TD**

                $mstatus = $m_row['ActiveStatus'];
                // if else statement      **6th TD**
                echo "</tr>";
            }
        }
        echo "</table>";
        ?>

【讨论】:

    【解决方案2】:

    试试这个

    <?php
    echo "<table width=1090 border=1 style=\"border: #ddd;\" align=center cellspacing=4 cellpadding=10>";
    echo "<tr class=thvoucher>";
    echo "<th width=30>Voucher Number</th>";
    echo "<th width=80>Reference Number</th>";
    echo "<th width=200>Guest Name</th>";
    echo "<th width=150>Voucher Status</th>";
    echo "<th width=150>Room Status</th>";
    echo "<th width=150>Meal Status</th>";
    echo "</tr>";
    for($i = $start; $i < $end; $i++) 
    {
        if ($i == $total_results) 
        {
            break;
        }
        while($row = $stmt->fetch())
        {
            $voucherid = $row['VoucherID'];
            $ref = $row['VoucherReference'];
            $gname = $row['GuestName'];
            $vstatus = $row['ActiveStatus'];
            echo "<tr class=voucherstyle" . $cls . ">";
            echo "<td>$voucherid</td>";    **// FIRST TD**
            echo "<td>$ref</td>";    **// SECOND TD**
            echo "<td>$gname</td>";     **// THIRD TD**
    
            if( $vstatus != 'empty' )       **// FOURTH TD**
            {
                if( $vstatus == 'Y' )
                {
                    echo "<td class=green >Active</td>";
                }
                else if( $vstatus == 'N' )
                {
                    echo "<td class=red >Inactive</td>";
                }
            }
            else
            {
                echo "<td><form method=post onsubmit=\"return mstatusvalidate(this)\"; action='voucherstatus_update.php?id_meal= $voucherid '><select name=mealstatus><option value=empty></option><option value=active>Active</option><option value=inactive>Inactive</option></select><input type=submit class=update_status title=\"Update $gname voucher status\" value=Update></form></td>";
            }
    
            while($r_row = $r_stmt->fetch())
            {
                $rstatus = $r_row['ActiveStatus'];
    
                // same like above if else statement      **// FIFTH TD**
            }       
    
            while($m_row = $m_stmt->fetch())
            {
                $mstatus = $m_row['ActiveStatus']; 
    
                        // same like above if else statement  **// SIXTH TD**       
            }
            echo "</tr>";
        }
    }
    echo "</table>";
    

    ?>

    【讨论】:

    • 编辑了答案。试试吧。第五和第六 while 循环需要在第一个 while 循环内。检查上面的答案
    • 我已经试过了。它行不通。如果我喜欢这个房间和用餐状态值(全部)显示在第一行...
    【解决方案3】:

    您的第一行中有 6 个元素,但我只看到 3 个 td 元素,并且在后续行中没有 tr 结束标记。这可能是您的问题。

    我怀疑粗略检查一下您的源输出会很清楚。

    【讨论】:

    • 我用了 6 td。我没有在这里发布 if else 语句代码。所以,只有你没有看到剩下的 3 td。我在 if else 语句中使用了另外 3 个 td。我已经更新了我的代码。再看
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签