2016-07-24 10 views
0

で解析配列これは私がこの配列を作成していますどのように私のコントローラでCodeIgniterの:私は私の<code>data array</code><code>users</code>と<code>books</code> に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++ 
         ) 

       ) 

     ) 

) 
を得る配列であります

この配列には、ユーザーインデックスとそのブックインデックス内のそのユーザーに割り当てられている書籍の1つの特定のユーザーがあります。私は各ブックを表示することができますテーブルに1つの行を生成できる方法でこの配列を解析する必要がありますrを分離した行に入れます。他の要素がある間だけ1つは、ユーザデータである - これは私が

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

答えて

0

最初の要素が侵入者であるので、あなたの親レベルの配列は、それの要素と矛盾しているを印刷するのにした形式で、この配列 を解析するために私をhelpeください。各書籍データ。私たちは、時にはもっと多くの本が返ってくることがあると推測しています。そのような形で一貫性を保つために、私は最初にユーザーデータを保持する2つの配列とそれらの配列を分離し、2番目の配列はブック配列を保持します。今

$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)ouldは動作しますが、DBからのデータを少なくして、常にDRY方法論を使用しようとします。ここでは、必要な別の書籍IDのために返される多くのオブジェクト(配列からのユーザーキー)があります。これは非常に高価なアプローチであり、book idやそれに類する機能のために、DB内でconcatを使用する方法をチェックすることができます。

関連する問題