【问题标题】:Codeigniter :Parsing array in viewCodeigniter:在视图中解析数组
【发布时间】:2016-07-24 19:44:34
【问题描述】:

我的 data array usersbooks 中有 2 个数组索引 这就是我在控制器中创建这个数组的方式

  public function getuserhistory($id){
            $query= $this->db->get_where('book_assign', array(
                'user_id'       =>  $id, 
            ));
            return $query->result();
        }
        public function booksrecord($id){
            $books= $this->db->get_where('books',array('id' =>  $id));
            return  $books->result();
        }
public function history($id){
        $results['user']= $this->usermodel->getuserhistory($id);
        foreach ($results as   $value) {
            foreach  ($value as   $subvalue) {
                $results[]['books']= $this->usermodel->booksrecord($subvalue->id);
            }
        }
        $data['data'] = $results;
 $this->load->view('history', $data);
}

下面是我得到的数组

Array
(
    [user] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 1
                    [user_id] => 5
                    [book_id] => 1
                    [date_issue] => 2016-07-24 00:00:00
                    [date_return] => 2016-07-25 00:00:00
                )

            [1] => stdClass Object
                (
                    [id] => 2
                    [user_id] => 5
                    [book_id] => 2
                    [date_issue] => 2016-07-24 00:00:00
                    [date_return] => 2016-07-25 00:00:00
                )

        )

    [0] => Array
        (
            [books] => Array
                (
                    [0] => stdClass Object
                        (
                            [id] => 1
                            [title] => PHP Made Easy 
                            [author] => Dietel & Dietel 
                            [serial_no] => 232323
                            [qty] => 9
                            [row_no] => 1
                            [col_no] => 2
                            [status] => 1
                            [category_id] => 1
                            [description] => This is a book about php 
                        )

                )

        )

    [1] => Array
        (
            [books] => Array
                (
                    [0] => stdClass Object
                        (
                            [id] => 2
                            [title] => C++
                            [author] => Dietel & Dietel 
                            [serial_no] => 232323
                            [qty] => 9
                            [row_no] => 1
                            [col_no] => 2
                            [status] => 1
                            [category_id] => 1
                            [description] => This is a book about c++
                        )

                )

        )

)

这个数组在用户索引中有一个特定的用户,并且在图书索引中分配给该用户的图书,我必须以一种可以在表格中生成一行的方式解析这个数组,我可以在其中显示分配给用户的每本书在一个单独的行中。请帮我解析这个数组 这是我要打印的格式

<tr>
    <th>User id </th>
    <th>Book Title </th>
    <th>Date Issued</th>
    <th> Date Return</th>
    <th> Action</th>
 </tr>

【问题讨论】:

    标签: php arrays codeigniter mysqli


    【解决方案1】:

    您的父级数组与其元素不一致,因为第一个元素是一个闯入者 - 只有一个是用户数据,而其他元素是每个书籍数据。我们可以假设有时可能会有更多的书被退回。为了以这种方式保持一致性,我会将这些数组分成两个数组,第一个数组保存用户数据,第二个数组保存书籍数组。

    $user = array_shift($parent);
    $books = $parent;// convinient naming for later use
    

    现在,您可以循环用户并使用图书 ID 指定合适的图书

    if (count($user))
    {
        foreach($user as $obj)
        {
            $rows = "<tr><td>$obj->user->id</td>";
            foreach($books as $book)
            {
                if($book->id == $obj->book_id)
                {
                    $rows .= "<td>$book->title</td>";
                }
                break;
            }
            $rows .= "<td>$obj->date_issued</td>";
            $rows .= "<td>$obj->date_returned</td>";
            $rows .= "<td>Action</td></tr>";
        }
        echo $rows;
    }
    

    如果有效,请检查此项。 虽然这 (sh|c) 可行,但请参阅从 DB 返回更少的数据,并始终尝试使用 DRY 方法。在这里,您返回了许多对象(来自您的数组的用户密钥),只是针对需要的不同书籍 ID。这是非常昂贵的方法,您可以检查如何在 DB 中使用 concat 来获取书籍 ID 或类似功能。

    【讨论】:

      猜你喜欢
      • 2016-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多