gpt4 book ai didi

php - 在 PHP 中使表的新实例彼此并排出现

转载 作者:行者123 更新时间:2023-11-28 11:02:44 25 4
gpt4 key购买 nike

我正在尝试用数据库中产品的图像和名称制作一个表格,在第一次输入一个图像和文本之后,我想在它旁边创建一个新的。现在它们被创建为新行,使其成为一个长长的向下帖子,但是我不知道如何解决这个问题。这是我的代码。

<?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)

关于php - 在 PHP 中使表的新实例彼此并排出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22384117/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com