1

我有 5 张图片存储在一个文件夹中,它们的链接存储在数据库中。我想把它们放在每行三列的表中。

<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 表中每行的列数)。我想知道这段代码哪里出错了。

4

5 回答 5

1

在您while($row = mysqli_fetch_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>";
于 2013-06-02T01:17:22.900 回答
0
    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>";}
于 2015-02-10T08:43:11.917 回答
0

如果您只想在每 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>';

但是请记住,到目前为止提供的所有解决方案都可能会在最后一行留下一个或两个 td 元素。如果您愿意,可以使用空<td>&nbsp;</td>列填充它。

于 2013-06-02T01:27:16.033 回答
0

您正在遍历第一个表格单元格中的所有结果。尝试这样的事情:

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>";
}
于 2013-06-02T01:14:34.817 回答
0
<?php
function studentTable($name,$grade){
echo "<tr> <td>$name</td><td>$grade</td></tr>";
}
?>
    <table style="width:100%" border="solid">
<tr>
<th>Name</th>
<th>Grade</th> 
 </tr>
 <?php studentTable("Sarah", 90) ?>
于 2019-04-14T07:42:42.893 回答