显示每个用户的数据

时间:2012-06-26 03:31:05

标签: php sql

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

<?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

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

请提前帮助谢谢

1 个答案:

答案 0 :(得分: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来执行此操作。