如何使用PHP在HTML中创建表
我有5张图片存储在文件夹中,它们的链接存储在数据库中。 我想把它们放在一个表中,每行有三列如何使用PHP在HTML中创建表,php,html,mysql,Php,Html,Mysql,我有5张图片存储在文件夹中,它们的链接存储在数据库中。 我想把它们放在一个表中,每行有三列 <body> <center> <table border='1'> <?php $host=""; $username=""; $password=""; $db_name="fruits_db"; $tbl_name="fruits_tbl"; $connection=mysqli_connect("$host","$username","$password"
<body>
<center>
<table border='1'>
<?php
$host="";
$username="";
$password="";
$db_name="fruits_db";
$tbl_name="fruits_tbl";
$connection=mysqli_connect("$host","$username","$password","$db_name");
if (mysqli_connect_errno())
{
echo "The application has failed to connect to the mysql database server: " .mysqli_connect_error();
}
$result = mysqli_query($connection, "SELECT * FROM fruits_tbl")or die("Error: " . mysqli_error($connection));
$num_rows=mysqli_num_rows($result);
$rows = $num_rows/3;
for($i=1; $i<=$rows ; $i++)
{
echo "<tr>";
for($j=1; $j<=3; $j++)
{
while($row = mysqli_fetch_array($result))
{
echo
("<td width='180px' height='200px'>"
."<div class = 'fruit_image'>"
."<img src='"
.$row['fruit_image']
."'/>"
."</div>"
."<div class = 'fruit_title'>"
.$row['fruit_name']
."</div>"
."</td>"
);
}
}
echo "</tr>";
}
mysqli_close($connection);
?>
</table>
</center>
</body>
</html>
我创建的上述代码包含两个FOR循环。脚本应该计算表中的行数,然后除以3(HTML表中每行的列数)。
我不知道这段代码哪里出错了。您正在循环第一个表格单元格中的所有结果。 请尝试以下方法:
for($i=1; $i<=$rows ; $i++) {
echo "<tr>";
for($j=1; $j<=3; $j++) {
$row = mysqli_fetch_array($result);
if ($row) {
echo(
"<td width='180px' height='200px'>"
."<div class = 'fruit_image'>"
."<img src='"
.$row['fruit_image']
."'/>"
."</div>"
."<div class = 'fruit_title'>"
.$row['fruit_name']
."</div>"
."</td>"
);
}
}
echo "</tr>";
}
for($i=1;$i您正在循环第一个表格单元格中的所有结果。
请尝试以下方法:
for($i=1; $i<=$rows ; $i++) {
echo "<tr>";
for($j=1; $j<=3; $j++) {
$row = mysqli_fetch_array($result);
if ($row) {
echo(
"<td width='180px' height='200px'>"
."<div class = 'fruit_image'>"
."<img src='"
.$row['fruit_image']
."'/>"
."</div>"
."<div class = 'fruit_title'>"
.$row['fruit_name']
."</div>"
."</td>"
);
}
}
echo "</tr>";
}
for($i=1;$i与您的同时($row=mysqli\u fetch\u array($result)){}
在您的第一个for循环中,它将在外部循环运行第二次/第三次之前,遍历您的所有行
这里有另一种方法-
$counter = 1;
// start 1st row
echo "<tr>";
while($row = mysqli_fetch_array($result)){
// if the 4th cell, end last row, and start new row
if ($counter%3==1){
echo "</tr><tr>";
}
echo
"<td width='180px' height='200px'>"
."<div class = 'fruit_image'>"
."<img src='"
.$row['fruit_image']
."'/>"
."</div>"
."<div class = 'fruit_title'>"
.$row['fruit_name']
."</div>"
."</td>";
// increase the counter
$counter++;
}
// close the last row
echo "</tr>";
$counter=1;
//从第一排开始
回声“;
while($row=mysqli\u fetch\u数组($result)){
//如果是第四个单元格,则结束最后一行,并开始新行
如果($counter%3==1){
回声“;
}
回声
""
.""
.""
.""
.""
.$row[“水果名称”]
.""
."";
//增加计数器
$counter++;
}
//关闭最后一行
回声“;
使用您的while($row=mysqli\u fetch\u array($result)){}
在第一个for循环中,它将在外部循环运行第二次/第三次之前,遍历您的所有行
这里有另一种方法-
$counter = 1;
// start 1st row
echo "<tr>";
while($row = mysqli_fetch_array($result)){
// if the 4th cell, end last row, and start new row
if ($counter%3==1){
echo "</tr><tr>";
}
echo
"<td width='180px' height='200px'>"
."<div class = 'fruit_image'>"
."<img src='"
.$row['fruit_image']
."'/>"
."</div>"
."<div class = 'fruit_title'>"
.$row['fruit_name']
."</div>"
."</td>";
// increase the counter
$counter++;
}
// close the last row
echo "</tr>";
$counter=1;
//从第一排开始
回声“;
while($row=mysqli\u fetch\u数组($result)){
//如果是第四个单元格,则结束最后一行,并开始新行
如果($counter%3==1){
回声“;
}
回声
""
.""
.""
.""
.""
.$row[“水果名称”]
.""
."";
//增加计数器
$counter++;
}
//关闭最后一行
回声“;
如果您只想在每3条记录后使用新行格式化显示,可以使用模数运算符:
$cntr = 0;
echo '<tr>';
while($row = mysqli_fetch_array($result)) {
$cntr++;
echo '
<td width="180px" height="200px">
<div class="fruit_image">
<img src="'.$row['fruit_image'].'" />
</div>
<div class="fruit_title">'.$row['fruit_name'].'</div>
</td>';
if ($cntr % 3 == 0 && $cntr != $num_rows)
echo '</tr><tr>';
}
echo '</tr>';
$cntr=0;
回声';
while($row=mysqli\u fetch\u数组($result)){
$cntr++;
回声'
“.$row['fruit_name']”
';
如果($cntr%3==0&&$cntr!=$num\u行)
回声';
}
回声';
但是,请记住,目前提供的所有解决方案可能会给您留下最后一行,其中包含一个或两个td元素。如果需要,您可以使用空的
列进行填充。如果您只想在每3条记录后使用新行格式化显示,您可以使用模数运算符:
$cntr = 0;
echo '<tr>';
while($row = mysqli_fetch_array($result)) {
$cntr++;
echo '
<td width="180px" height="200px">
<div class="fruit_image">
<img src="'.$row['fruit_image'].'" />
</div>
<div class="fruit_title">'.$row['fruit_name'].'</div>
</td>';
if ($cntr % 3 == 0 && $cntr != $num_rows)
echo '</tr><tr>';
}
echo '</tr>';
print "<center><table border=1>
<tr>
<td>id</td>
<td>name</td>
<td>company</td>
<td>branch</td>
<td>job title</td>
<td>contact</td>
<td>email</td>
<td>mgs</td>
</tr>";
while($row=mysql_fetch_array($query))
{
print "<tr>";
for ($i=0;$i<=(count($row)/2);$i++)
{
print "<td>$row[$i]</td>";
} print"</tr>";
}
}
else{echo " <p>No Records Found</p>";}
$cntr=0;
回声';
while($row=mysqli\u fetch\u数组($result)){
$cntr++;
回声'
“.$row['fruit_name']”
';
如果($cntr%3==0&&$cntr!=$num\u行)
回声';
}
回声';
但是请记住,到目前为止提供的所有解决方案可能会给您留下最后一行,其中包含一个或两个td元素。如果需要,您可以使用空的
列来填充它。print”
print "<center><table border=1>
<tr>
<td>id</td>
<td>name</td>
<td>company</td>
<td>branch</td>
<td>job title</td>
<td>contact</td>
<td>email</td>
<td>mgs</td>
</tr>";
while($row=mysql_fetch_array($query))
{
print "<tr>";
for ($i=0;$i<=(count($row)/2);$i++)
{
print "<td>$row[$i]</td>";
} print"</tr>";
}
}
else{echo " <p>No Records Found</p>";}
身份证件
名称
公司
分支
职位名称
接触
电子邮件
mgs
";
while($row=mysql\u fetch\u array($query))
{
打印“”;
对于($i=0;$iprint)
身份证件
名称
公司
分支
职位名称
接触
电子邮件
mgs
";
while($row=mysql\u fetch\u array($query))
{
打印“”;
对于($i=0;$i
名称
等级
名称
等级
非常感谢…它正在工作。我还必须将$rows=$num\u rows/3;
更改为$rows=$num\u rows%3;
这实际上对较大的数字不起作用。它应该是$rows=ceil($num\u rows/3)
谢谢,所以这应该总是四舍五入到下一个最高的整数值。一直在寻找这个…顺便问一下,在循环中有没有一种方法我只能加载6个单元格(每行3列)在每一页上,让它从上一页的中断处恢复?…也就是说,从数据库的1-6行加载所有项目,然后在下一页上,在下一页加载第7-12行和第13-18行,等等…非常感谢…它正在工作。我还必须将$rows=$num_rows/3;
更改为$rows=$num_rows%3;
,即实际上,对于较大的数字不起作用。它应该是$rows=ceil($num_rows/3);
谢谢,所以这应该总是将它四舍五入到下一个最高整数值。一直在寻找这个…顺便问一下,在循环中有没有办法我只能加载6个单元格(每行3列)在每一页上,让它从上一页的中断处恢复?…也就是说,从数据库的1-6行加载所有项目,然后在下一页上,在下一页上加载第7-12行和第13-18行,等等…谢谢。我也会尝试这段代码。但是我不太担心丢失的td元素,因为我不会使用该区域。但是r出于纯html原则的考虑,也许我应该担心…?只要显示正常,这并不重要。谢谢。我也会尝试这段代码。不过,我不太担心缺少td元素,因为我不会使用该区域。但是出于纯html原则的考虑,也许我应该担心…?只要显示正常,就可以了这并不重要。您的内部while()循环正在完全使用DB结果,因此第二个和后续的$j循环将没有数据可处理,因为查询中没有结果。您的内部while()循环正在完全消耗DB结果,因此第二个和后续的$j循环将没有数据可处理,因为查询没有结果。如何使用最小代码行获取和打印表此代码。它也可以在函数和类中使用。如何使用最小代码行获取和打印表此代码它可以在函数中使用,也可以在类中使用。