【问题标题】:Make a new line in html table row from fetching in database从数据库中获取在 html 表行中创建一个新行
【发布时间】:2016-03-09 13:54:27
【问题描述】:

我想将图像与文本一起显示,并使用 php 从数据库中获取 这是代码

<table id = "menu-container" border = "2">
    <caption>Main Dish<caption>
    <tr>
    <?php
        if($result->num_rows > 0) 
        {                                   
            while($row = $result->fetch_assoc()) 
            {                                               
    ?>                    
            <td class = "wrapword">

                <img src = "data:image/jpeg; base64 , <?php echo base64_encode($row['product_image']); ?>"
                height = "290" width = "350">
                <br>
                &nbsp;Name: <?php echo $row['product_name']; ?>
                <br>
                &nbsp;Price: Php<?php echo $row['product_price']; ?> 
                <label id = "add-to-list">Add to list</label>

            </td>

    <?php
            }
        }
        else
        {
            echo "No Menu Found";
        }
    ?> 
    </tr>
</table>

这是该代码的图片或输出 当表格数据超过4的限制并且其他数据将进入底部时,我想在表格行中创建一个新行或创建一个新行,这可能吗?我希望我的解释清楚。

【问题讨论】:

  • 可能这是一个 HTML 标记问题。您能否向我们展示 HTML 结果以便我们提供帮助?
  • 实际上这是我使用的唯一 html 代码。抱歉,您能否更具体地说明您对我显示 html 结果的含义。所以我不会误解它谢谢。
  • 那是 .php 文件,它是“呼应”HTML 代码(类似于呈现页面)。我要问的是该渲染的结果,可能是使用从 chrome 开发工具复制的整个表格元素:右键单击 -> 表格上的“检查元素”

标签: javascript php html css


【解决方案1】:

这基本上是 Munawir 想要达到的目标。它有一个简单的计数器,检查它是否可以被 4 整除,没有余数。如果是,则开始新行。如果没有,则留在当前行。

if($result->num_rows > 0) 
    {            
        $c = 0;                       
        while($row = $result->fetch_assoc()) {
            // If it's not divisible by 4 without remainder - Same Row
            if(!($c % 4 == 0)){ 
                // echo <td> content
            } else {
                echo "</tr>"; //Close last row
                echo "<tr>"; //Open new row
                // echo <td> content
            }
            $c++;
        }
    } else {
        //...
    } ?>

    </tr> <!-- End last row -->
</table>

编辑

解决方案甚至可以简化为:

while($row = $result->fetch_assoc()) {
    if($c % 4 == 0){ 
        echo "</tr><tr>";
    }

    // echo <td> content

    $c++;
}

【讨论】:

    【解决方案2】:

    您需要一个变量来计算已收集的物品数量。如果在您的情况下该变量等于 4,则必须清空收集变量并再次收集

    <table id = "menu-container" border = "2">
        <caption>Main Dish<caption>
    
        <?php
        $html="";
        $i=1;
            if($result->num_rows > 0) 
            {                                   
                while($row = $result->fetch_assoc()) 
                {                                               
                    $html .= ' <td class = "wrapword">
                        <img src = "dat[![enter image description here][1]][1]a:image/jpeg; base64 , '.base64_encode($row['product_image']).'" height = "290" width = "350">
                        <br>
                        &nbsp;Name: '.$row['product_name'];.'<br>
                        &nbsp;Price: Php '.$row['product_price'].'<label id = "add-to-list">Add to list</label>
    
                    </td>';
    
                    if($i==4){
                        echo $html = '<tr>'.$html.'<tr>';
                        $html="";
                        $i=1;
                    }
                    $i++;
                }
            }
            else
            {
                echo "No Menu Found";
            }
        ?> 
    </table>
    

    【讨论】:

    • $i 应该以0 开头并重置为0。否则每行将只有 3 个结果。要么将if($i == 4)更改为if($i &gt; 4)
    【解决方案3】:

    <table id = "menu-container" border = "2">
        <caption>Main Dish<caption>
        <tr>
        <?php
            if($result->num_rows > 0) 
            {            
                $i=0;
                while($row = $result->fetch_assoc()) 
                {        
        ?>                    
                <td class = "wrapword">
    
                    <img src = "data:image/jpeg; base64 , <?php echo base64_encode($row['product_image']); ?>"
                    height = "290" width = "350">
                    <br>
                    &nbsp;Name: <?php echo $row['product_name']; ?>
                    <br>
                    &nbsp;Price: Php<?php echo $row['product_price']; ?> 
                    <label id = "add-to-list">Add to list</label>
    
                </td>
    
        <?php
                $i++;
    		    if($i%4==0 && $i<$result->num_rows) echo '</tr><tr>';
                }
            }
            else
            {
                echo "No Menu Found";
            }
        ?> 
        </tr>
    </table>

    【讨论】:

      猜你喜欢
      • 2014-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-08
      • 1970-01-01
      • 2021-06-07
      • 2012-01-20
      相关资源
      最近更新 更多