Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6193471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:07:35+00:00 2026-05-24T03:07:35+00:00

In codeigniter 1.73 i’m trying to display books by category. so if i have

  • 0

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);      
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T03:07:35+00:00Added an answer on May 24, 2026 at 3:07 am

    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; ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In CodeIgniter I'm using a base CRUD My_model , but I have this small
Within CodeIgniter, I'm doing a select, retrieving some blogposts. But I also want the
Using CodeIgniter, how do I access and display text entered into an input field
Using CodeIgniter, I am trying to place several images onto my view page as
i am using codeigniter 2.1.0 and mysql database. in my admin panel i have
If CodeIgniter has _remap() that gets called before it touches any method in a
Using Codeigniter, I have a controller function name search that takes in parameters state
I am using CodeIgniter 2.1.0 and MySQL database. I have uploaded an image through
CodeIgniter 2.0.2 syntax error blank page i have CodeIgniter 2.0.2 (CI) and its annoing,
Using CodeIgniter I am trying to create a link that the user can click

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.