簡體   English   中英

使PHP中表的新實例彼此並排出現

[英]Making new instances of a table in PHP appearing beside each other

我試圖用數據庫中的產品的圖像和名稱創建一個表,在第一次輸入一個圖像和文本之后,我想要在其旁邊創建一個新的圖像和文本。 現在,它們被創建為新的行,使其向下延伸很長,但是我不知道該如何解決。 這是我的代碼。

<?php

ini_set('display_errors', 'On');
require_once 'dbconfig.php';

$result = mysqli_query($con,"SELECT * FROM produkt JOIN bild ON bild.idBild = produkt.idProdukt JOIN ingrediens ON ingrediens.idIngrediens = produkt.idProdukt");
?>

<html>
<head>
<title>Produktsida</title>

<style type= "text/css">

table.blubb {
    display: block;
    width: 100px;
}

th {
font-family: Arial, Helvetica, sans-serif;
font-size: .7em;
background: #666;
color: #FFF;
padding: 2px 6px;
border-collapse: separate;
border: 2px solid #000;
}

td {
font-family: Arial, Helvetica, sans-serif;
font-size: .7em;
border: 2px solid #DDD;
text-align:center;
}

</style>
</head>
<body>
<table class = "blubb">

<?php
while($row = mysqli_fetch_array($result))
{
    $imgData = base64_encode($row['Bild']);
    ?>
    <tr>
        <td>
            <img src="data:image/jpeg;base64,<?php echo $imgData ?>" />     </td>
    </tr>
    <tr>
        <td>
            <?php echo $row['Produkt_Namn'] ?>
        </td>
    </tr>
<?php } ?>
</table>
<?php
mysqli_close($con);
?>
</body>
</html>

我認為這就是您想要的:

[IMAGE]    [IMAGE]    [IMAGE]    [IMAGE]
Text       Text       Text       Text   

[IMAGE]    [IMAGE]    [IMAGE]    [IMAGE]
Text       Text       Text       Text 

...

在這種情況下,請嘗試以下操作:

<table class = "blubb">
<tr>
    <td>
        <?php
        //Variable that counts how many products are displayed on one line
        $productsOnLineCounter = 0;
        while($row = mysqli_fetch_array($result))
        {
         $imgData = base64_encode($row['Bild']);
         echo "
             <table>
                <tr>
                    <td>
                        <img src='data:image/jpeg;base64,{$imgData}' />
                    </td>
                </tr>
                <tr>
                    <td>
                        {$row['Produkt_Namn']}
                    </td>
                </tr>
            </table>
         ";
        //$productsOnLineCounter +1
        $productsOnLineCounter ++;
            //Replace '4' with your own number
            //If 4 products are displayed on one line, create a new line
            if ($productsOnLineCounter == 4) {
                echo "
                        </td>
                    </tr>
                    <tr>
                        <td>
                ";
            //Reset $productsOnLineCounter (new line created)
            $productsOnLineCounter = 0;
            }
        } 
        ?>
    </td>
</tr>
</table>

請注意,我沒有更改您的其余代碼,僅更改了

<table class="blubb"></table> (關閉blubb-class-table)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM