簡體   English   中英

Codeigniter:在視圖中解析數組

[英]Codeigniter :Parsing array in view

我的data array users有2個數組索引並books這就是我在控制器中創建此數組的方式

  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>

您的父級數組與其元素不一致,因為第一個元素是闖入者-只有一個是用戶數據,而其他元素都是書本數據。 我們可以假設有時還會返回更多的書。 為了以這種方式保持一致性,我將那些數組分成兩個數組,第一個數組保存用戶數據,第二個數組保存books數組。

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

現在,您可以循環用戶並使用書號指定專用的書

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)可行,但請參見從數據庫返回較少的數據,並始終嘗試使用DRY方法。 在這里,您返回了許多對象(陣列中的用戶密鑰),僅用於所需的不同書本ID。 這是非常昂貴的方法,您可以檢查如何在數據庫中將concat用於書籍ID或類似功能。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM