1

我想显示每个用户的数据。但它显示每个表和每个用户的所有数据。所以我想要实现的是为该用户显示表上的所有数据这是我的代码

<?php foreach ($employee as $emp) { ?>

  <table id="employee-content">
     <h3 class="color-orange"><?php echo $emp['members']['username']; ?></h3>
        <tr>
          <td><p class="personal-management-content"><?php echo $emp['posts']['title']; ?></p></td>
          <td><p class="personal-management-content padding-left50" class="padding-left50"><?php echo $emp['posts']['title']; ?></p></td>
          <td><p class="personal-management-content padding-left50" class="padding-left50"><?php echo $emp['posts']['deadline']; ?></p></td>
        </tr>

<?php } ?>'

这是 $employee 的 sql 查询

* on controller 
   public function employee_management() {
        $posts = $this->Post->getPostByMember();
        $this->set("employee", $posts);
    }

* on model
    public function getPostByMember() {
        $post = "SELECT username, content, deadline, title FROM members INNER JOIN posts ON posts.member_id=members.id";
//        debug($user);
        $con = $this->getDataSource();
        $get = $con->fetchAll($post);
//        debug($get);
        return $get;
    }

和 print_r $employee

Array (
 [0] => Array (
   [members] => Array (
     [username] => admin )
   [posts] => Array (
     [content] => asdsadsad
     [deadline] => 2012-06-01
     [title] => sad ) ) 
 [1] => Array (
   [members] => Array (
     [username] => admin )
   [posts] => Array (
     [content] => asd
     [deadline] => 2012-06-20
     [title] => sad ) ) 
 [2] => Array (
   [members] => Array (
     [username] => admin )
   [posts] => Array (
     [content] => dasdw2
     [deadline] => 2012-06-19
     [title] => sdas ) ) 
 [3] => Array (
   [members] => Array (
     [username] => guest )
   [posts] => Array (
     [content] => s
     [deadline] => 2012-06-08
     [title] => wara ) )

输出是示例屏幕截图 http://i46.tinypic.com/209n56s.jpg

所以我想要实现的是例如用户“管理员”他的所有标题、描述、截止日期将显示在单个框或 . 其他用户也是如此。

请帮助提前谢谢

4

1 回答 1

2

所以...看起来您的数据库结果当前的结构如下:

  • 结果 1
    • 用户名 1
    • 邮政
  • 结果 2
    • 用户名 1
    • 邮政
  • 结果 3
    • 用户名 2

等等,你希望你的显示器看起来像:

  • 用户名 1
    • 邮政
    • 邮政
  • 用户名 2
    • 邮政

在我看来,实现你想要的最简单的方法是从你的 SELECT 的结果中创建一个新数组。

<?php

$posts_by_emp=array();
foreach ($employee as $emp) {
  $posts_by_emp[$emp['members']['username']][]=array(
    'title' => $emp['posts']['title'];
    'descr' => $emp['posts']['title'];
    'deadline' => $emp['posts']['deadline']
  );
}

$hfmt = "<tr>\n    <th colspan='3' class='color-orange'>%s</th>\n  </tr>";
$bfmt = "<tr>\n"
      . "    <td class='personal-management-content'>%s</td>\n"
      . "    <td class='personal-management-content padding-left50'>%s</td>\n"
      . "    <td class='personal-management-content padding-left50'>%s</td>\n"
      . "  </tr>";

print "  <table id='employee-content'>";
foreach ($posts_by_emp as $username => $items) {
  // Print header
  printf($hfmt, $username);
  foreach ($items as $item) {
    // Print post
    printf($bfmt, $item['title'], $item['descr'], $item['deadline']);
  }
}
print "</table>\n";

这当然是未经测试的,因为我没有你的数据。根据您的数据库的结构,您可以通过创建一个更容易解析的不同 SQL 查询来使用更少的 PHP 来做到这一点。

于 2012-06-27T13:56:15.730 回答