使用foreach显示结果

时间:2011-06-18 18:07:08

标签: php mysql foreach

我正在尝试编写某种论坛主页,这是基于其他人所做的工作。这是我到目前为止所得到的:

论坛

<?php
 $crumbs = explode(",", $user['data']['depts']);

 foreach ($crumbs as &$value) {
     $data = $db->query("SELECT * FROM tbl_depts WHERE id = '" . $value . "'");
     $crumb = $data->fetch_assoc();
     $data = $db->query("SELECT * FROM tbl_forums WHERE deptid = '" . $value . "'");
     $forumcount = $data->num_rows;
     $forum = $data->fetch_assoc();
?>


<div class="h3top"><?php echo $crumb['name']; ?></div>
<div class="info2alt">
<?php
     while (($row = $data->fetch_array()) !== FALSE) {
         $forum[] = $row;
     }

     foreach ($forum as $row) {

         if ($forumcount >= 1) {
             if ($forum['lastpost'] == "") {
                 $forum['lastpost'] = "--";
             }
?>
            <div class="info4">
                <table width="100%" border="0" cellspacing="1" cellpadding="4">
                    <tr>
                        <td width="55%" align="left" valign="middle" class="zebraodd"><a href="/forums/viewforum?id=<?php echo $forum['fid']; ?>"><?php echo $row['name']; ?></a></td>
                        <td width="10%">Threads: <?php echo $forum['threadcount']; ?><br />Posts: <?php echo $forum['postcount']; ?></td>
                        <td width="35%">Last Post: <?php echo $forum['lastpost']; ?></td>
                    </tr>
                </table>
            </div>
<?php
 }
?>
        <?php if ($forumcount >= 1) { ?>
            <div class="info3bottom"></div>

            <?php
        }
    }
    ?>
</div>
<?php
 }
?>

这是表格中的当前数据:

Table Data

现在,当我尝试使用此代码时,我得到了这个:Issue,这样第一个显示了很多次,但是,下一个没有出现。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:4)

您想要使用$value['threadcount']等。$forum是包含所有行的数组。我想知道你得到一些输出......

fetch_assoc()只会从结果集中获取一行。你必须使用循环来填充数组:

while(($row = $data->fetch_assoc()) !== FALSE) {
  $forum[] = $row;
}

然后,您可以使用foreach循环迭代$forum数组:

foreach($forum as $row) {
  echo htmlspecialchars($row['threadcount']);
  // etc.
}

试图修复这个烂摊子......

<?php

$crumbs = explode(",", $user['data']['depts']);

foreach ($crumbs as $crumb) { ?>
  <div class="h3top"><?php echo htmlspecialchars($crumb['name']);?></div>
<?
}

$result = $db->query("SELECT * FROM tbl_forums WHERE deptid = " . (int)$id . "");
$forumcount = $result->num_rows;

while(($row = $data->fetch_assoc()) !== FALSE) {
  if ($row['lastpost'] == "") { $row['lastpost'] = "--";}
?>
  <div class="info2alt">
  <div class="info4">
  <table width="100%" border="0" cellspacing="1" cellpadding="4">
  <tr>
  <td width="55%" align="left" valign="middle" class="zebraodd"><a href="/forums/viewforum?id=<?php echo urlencode($forum['fid']); ?>"><?php echo htmlspecialchars($forum['name']); ?></a></td>
  <td width="10%">Threads: <?php echo htmlspecialchars($forum['threadcount']); ?><br />Posts: <?php echo htmlspecialchars($forum['postcount']); ?></td>
  <td width="35%">Last Post: <?php echo htmlspecialchars($forum['lastpost']); ?></td>
  </tr>
  </table>
  </div>

  <div class="info3bottom"></div>
<?php
}
?>