donglu3087 2011-07-28 10:24
浏览 46
已采纳

嵌套的foreach codeigniter

In codeigniter 1.73 i'm trying to display books by category. so if i have a category called cars, i should see a list of books within cars. so i tried a nested foreach loop to accomplish this but can't seem to get it to work.

<?php

class Book_model extends Model {


    function books_by_category()
    {
        $this->db->select('*');
        $this->db->from('categories');
        $this->db->join('books', 'books.category_id = categories.id');

        $query = $this->db->get();
        return $query->result();        
    }
}

then in the view:

foreach($data as $category) {

  if (sizeof($category['books']))
  {
    foreach($category['books'] as $book)
    {
      <li>$book->book_number anchor("book/chapters/$book->id", $book->book_title)</li>  
    }
  } else {
    // show 'no books'
  }
}

controller:

function index() {

    $data = array();

    if($query = $this->book_model->books_by_category())
    {
        $data['books_by_category'] = $query;

        foreach($query as $row)
        {
            if (!isset($data[$row['id']]))
            {
                $data[$row['id']] = $row;
                $data[$row['id']]['books'] = array();
            }  


            $data[$row['id']]['books'][] = $row;


        }                   

        $data['main_content'] = 'books_view';
        $this->load->view('includes/template', $data);      
    }
}
  • 写回答

4条回答 默认 最新

  • douzhi7754 2011-07-28 14:26
    关注

    A couple of points

    function books_by_category()
    {
        $this->db->select('*');
        $this->db->from('categories');
        $this->db->join('books', 'books.category_id = categories.id');
    
        $query = $this->db->get();
        return $query->result();        
    }
    

    This code returns all columns for categories and books that have a category set, is this really what you are trying to do? The function name 'books_by_category' would suggest to me that you are only interested in books for one specific category, from looking at your question I'm guessing this isn't the case.

    Presuming you are trying to get all books but group them by category I would do the following:

    model

    function get_books()
    {
        $this->db->select('*');
        $this->db->from('books');
        $this->db->join('categories', 'categories.id = books.category_id');
        $query = $this->db->get();
    
        if($query->num_rows() == 0)
        {
            #no books
            return false;
        }
        return $query->result();
    }
    

    controller

    function index() {
        $data = array();
    
        $book_list = $this->book_model->get_books();
        if($book_list)
        {
            $categories = array();
            $books_by_category = array();
    
            foreach($book_list as $book)
            {
                $category_id = $book['category.id'];
                $category_name = $book['category.name'];
    
                if(array_key_exists($category_id, $categories))
                {
                    $categories[$category_id] = $category_name;
                }
    
                if(array_key_exists($category_id, $books_by_category))
                {
                    $books_by_category[$category_id] = array();
                }
    
                $books_by_category[$category_id][] = $book;
            }
    
            $data['categories'] = $categories;
            $data['books_by_category'] = $books_by_category;
        }
    
        $data['main_content'] = 'books_view';
        $this->load->view('includes/template', $data);      
    }
    

    and the view

    <?php if(isSet($books_by_category)) : ?>
    
    <?php foreach($books_by_category as $category => $book_list): ?>
    
    <p>Category: <?php echo $categories[$category]; ?></p>
    
    <ul>
    <?php foreach($book_list as $book) : ?>
    
    <li><?php echo $book->book_number; ?>. <?php echo anchor('bible/chapters/' . $book->id, $book->book_title); ?></li>
    
    <?php endforeach; ?>
    </ul>
    
    <?php endforeach; ?>
    
    <?php else: ?>
    
    <p>Sorry dude, no books.</p>
    
    <?php endif; ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?