在多个连续的行和列中使用echo在表中显示图像


Display image using echo in tables in multiple yet continuous rows and columns

我设法从数据库中显示图像,但它只是在表中显示连续而没有任何格式。我需要做一个额外的if循环或while循环吗?

<table cellpadding='4' cellspacing='0'>
<?php
include 'dbFunctions.php';
$arrStocks = executeSelectQuery("SELECT name,id,image,price FROM stocks");
for ($countStocks = 0; $countStocks < count($arrStocks); $countStocks++) {
  $name  = $arrStocks[$countStocks]['name'];
  $image = $arrStocks[$countStocks]['image'];
  $id    = $arrStocks[$countStocks]['id'];
  $price = $arrStocks[$countStocks]['price'];
?>
  <thead>
    <tr>
      <th class="timgG">
        <h4><?php echo '<img src="stocks/' . $image . '">' ?></h4>
      </th>
      <th class="timgG">
        <?php echo '<a href="stockDetails.php?id=' . $id . '">' . $name . '</a>'; ?>
      </th>
    </tr>
<?php
}
?>
  </thead>
</table>

您当前的代码将输出类似

的内容
<table>
  <thead>
  <tr>
    <th><h4><img /></h4></th>
    <th><a>...</a></th>
  </tr>
  <thead>
  <tr>
    <th><h4><img /></h4></th>
    <th><a>...</a></th>
  </tr>
  <thead>
  <tr>
    <th><h4><img /></h4></th>
    <th><a>...</a></th>
  </tr>
  ... 
</thead>

不是格式良好的HTML。我不认为你需要在标题中有图像,或者你的表的每一行都是表列标题。

相反,尝试使用<td></td>而不是<th></th>,并将打开的<thead>置于for循环之外。

您可能还想考虑您的表标题将是什么-如果它们是您的arrStocks查询的第一行,那么您可以添加一个if语句来改变您的第一个表行的输出,使它们成为<th></th>,但再次,我不确定为什么您需要将图像定义为表标题。

编辑

唯一可能破坏表输出的是include的内容。

试题:

<?php
include 'dbFunctions.php';
$arrStocks = executeSelectQuery("SELECT name,id,image,price FROM stocks");
?>
<table cellpadding='4' cellspacing='0'>
<?
for ($countStocks = 0; $countStocks < count($arrStocks); $countStocks++) {
  $name  = $arrStocks[$countStocks]['name'];
  $image = $arrStocks[$countStocks]['image'];
  $id    = $arrStocks[$countStocks]['id'];
  $price = $arrStocks[$countStocks]['price'];
?>
    <tr>
      <td class="timgG">
        <?php echo '<img src="stocks/' . $image . '" alt="">' ?>
      </td>
      <td class="timgG">
        <?php echo '<a href="stockDetails.php?id=' . $id . '">' . $name . '</a>'; ?>
      </td>
    </tr>
<?php
}
?>
</table>

如果我正确理解了您的问题,您需要一个图像网格。一个网格是一个包含两个条目的表,所以你需要有两个循环。一行的每一项(即每一列)都来自第一个循环,每一行都来自第二个循环,在里面调用第一个循环。

我将使用下一个代码片段。还有一些未知的约束条件你没有给出,所以我会假设其中的一些。例如,我将假设您的数据作为一维数组返回。

<?php
$data = array('one', 'two', 'three', 'four', 'five', 'six');
$nbColumns = 3;
?>
<table>
  <?php
    for($row = 0; $row * $nbColumns < count($data); ++$row) {
  ?>
  <tr>
    <?php
    for($col = 0; $col < $nbColumns; ++$col) {
    ?>
      <td><?php echo $data[$row * $nbColumns + $col]; ?></td>
    <?php
    }
    ?>
  </tr>
  <?php
  }
  ?>
</table>

这将产生:

<table>
  <tr>
    <td>one</td>
    <td>two</td>
    <td>three</td>
  </tr>
  <tr>
    <td>four</td>
    <td>five</td>
    <td>six</td>
  </tr>
</table>

您应该能够根据自己的需要调整这段代码。

如果有什么需要我解释的,请告诉我,以便我可以编辑我的答案/片段。


编辑:

我不确定是否明白你需要我解释的内容。

无论如何,我只是在我的代码片段中重新注入了您的初始代码,但我没有做任何特别的事情。基本上,我只是在顶部插入include和call to executeSelectQuery,并用你的两行替换了愚蠢的数字,第一行将是图像,第二行将是链接/名称。

这应该工作,但我不能保证它会,因为我没有你的代码的所有细节。

<?php
include 'dbFunctions.php';
$arrStocks = executeSelectQuery("SELECT name,id,image,price FROM stocks");
$nbColumns = 3;
?>
<table>
  <?php
    for($row = 0; $row * $nbColumns < count($arrStocks); ++$row) {
  ?>
  <tr>
    <?php
    for($col = 0; $col < $nbColumns; ++$col) {
      $index = $row * $nbColumns + $col;
      $name  = $arrStocks[$index]['name'];
      $image = $arrStocks[$index]['image'];
      $id    = $arrStocks[$index]['id'];
      $price = $arrStocks[$index]['price'];
    ?>
      <td>
        <img src="stocks/<?php echo $image; ?>" alt="<?php echo $name; ?>">
        <br>
        <a href="stockDetails.php?id=<?php echo $id; ?>"><?php echo $name; ?></a>
      </td>
    <?php
    }
    ?>
  </tr>
  <?php
  }
  ?>
</table>

作为结论,请注意,您不应该使用table来实现您想要的。我的回答有点粗俗($nbColumns不是很好),但是没有更好的方法来实现你想要使用table s。使用float s可能是一个更好的解决方案,但它将不再是一个PHP相关的问题…


EDIT2:

编辑并测试了我的最后一个片段,现在可以工作了;-)