I am fetching data from a database like this in a Library Management System
- Details about user from specific id
- Get all the books assigned to the user from books_assigned table
- Get details of the assigned books from books table
This is what i am doing
public function userhistory($id){
$query= $this->db->get_where('user',array(
'id' => $id,
));
$result['user']= $query->result();
$query_books = $this->db->get_where('book_assign', array(
'user_id' =>$result['user'][0]->id,
));
foreach ($query_books->result() as $key => $value) {
$result['assigned_books']= $query_books->result();
$query_book = $this->db->get_where('books',array (
'id' => $query_books->result()[$key]->book_id)
);
$result['books_details'][]= $query_book->result();
}
echo '<pre>';
print_r($result);
echo '</pre>';
die;
}
This is how i get result for print_r($result);
Array
(
[user] => Array
(
[0] => stdClass Object
(
[id] => 5
[name] =>
[email] => [email protected]
[password] => test
[role] =>
[status] => 1
)
)
[assigned_books] => 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
)
)
[books_details] => Array
(
[0] => Array
(
[0] => stdClass Object
(
[id] => 1
[title] => PHP Made Easy
[author] => Dietel & Dietel
[serial_no] => 232323
[qty] => 8
[row_no] => 1
[col_no] => 2
[status] => 1
[category_id] => 1
[description] => This is a book about php
)
)
)
)
Now what i want is books_details should be within assigned_books array , for example if book with id 1 is assigned i want to get details for this book on assigned_books index against id 1 instead of getting it on a different index called books_details can someone help me to change my logic and fix this ,