简体   繁体   English

Codeigniter:在视图中解析数组

[英]Codeigniter :Parsing array in view

I have 2 array indexes in my data array users and books this is how i am creating this array in my controller 我的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);
}

following is the array that i get 以下是我得到的数组

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++
                        )

                )

        )

)

This array has one specific user in user index and books assigned to that user in book index, I've to parse this array in a way that can generate one row in a table where i can show each book assigned to a user in a separrate row . 该数组在用户索引中有一个特定的用户,在书索引中有分配给该用户的书,我必须以一种可以在表中生成一行的方式解析该数组,在该表中我可以分别显示分配给用户的每本书排。 Please helpe me to parse this array this is the format i've to print 请帮我解析这个数组,这是我要打印的格式

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

Your parent level array is inconsistent with it's elements since first element is an interloper - only that one is user data while other elements are each book data. 您的父级数组与其元素不一致,因为第一个元素是闯入者-只有一个是用户数据,而其他元素都是书本数据。 We can assume that sometimes there could be much more books returned. 我们可以假设有时还会返回更多的书。 To keep consistency in that manner I would separate those arrays in two arrays which first holds user data and second one holds books array. 为了以这种方式保持一致性,我将那些数组分成两个数组,第一个数组保存用户数据,第二个数组保存books数组。

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

Now, you can loop users and dedicate appropriate book using book 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;
}

Check this if works. 检查是否可行。 Although this (sh|c)ould work, see to return less data from DB and always try to use DRY methodology. 尽管(sh | c)可行,但请参见从数据库返回较少的数据,并始终尝试使用DRY方法。 Here you have many objects returned (user key from your array) just for different book id needed. 在这里,您返回了许多对象(阵列中的用户密钥),仅用于所需的不同书本ID。 It is pretty much expensive approach and you can check how to use concat in DB for book id or similar function. 这是非常昂贵的方法,您可以检查如何在数据库中将concat用于书籍ID或类似功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM