在数据库中获取html表行中的新行

时间:2016-03-09 13:54:28

标签: javascript php html css

我希望显示图像以及文本和使用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>

enter image description here

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

3 个答案:

答案 0 :(得分:2)

这基本上是穆纳维尔试图达到的目的。它有一个简单的计数器,检查它是否可被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++;
}

答案 1 :(得分:1)

您需要一个变量来计算已经收集的项目数量。如果在您的情况下该变量等于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>

答案 2 :(得分:1)

Monday 2016-03-10
15:00
16:00
17:00
Tuesday 2016-03-11
15:00
16:00
17:00
18:00