0

我设法显示了数据库中的图像,但是它只是连续显示,没有表格中的任何格式。我必须做一个额外的 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>
4

2 回答 2

0

您当前的代码将输出如下内容

<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>,但同样,我不确定为什么需要将图像定义为表头。

编辑

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

尝试:

<?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>
于 2013-03-25T00:23:13.040 回答
0

如果我正确理解您的要求,您需要一个图像网格。网格是一个 2 条目表,因此您确实需要有 2 个循环。行的每个条目(即每一列)都来自第一个循环,每一行都来自第二个循环,调用内部的第一个循环。

我将继续下一个片段。但是,您没有给出一些未知的限制,因此我将假设其中一些。例如,我将假设您的数据作为一维数组返回。

<?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和调用,并用你的两行替换了哑数字,第一行是图像,第二个是链接/名称。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>

作为结论,请注意您不应该使用 atable来实现您想要的。我的回答有点骇人听闻(这$nbColumns不是很好),但没有什么更好的方法可以使用tables. 虽然使用floats 可能是一个更好的解决方案,但它不再是 PHP 相关的问题......


编辑2:

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

于 2013-03-25T15:28:14.370 回答