简体   繁体   中英

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

  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.

$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. Here you have many objects returned (user key from your array) just for different book id needed. It is pretty much expensive approach and you can check how to use concat in DB for book id or similar function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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