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

  • Home
  • SEARCH
  • 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 890951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:48:55+00:00 2026-05-15T13:48:55+00:00

In this codeigniter model, I am trying to push the category name of each

  • 0

In this codeigniter model, I am trying to push the category name of each product.
I want the array to look like:

    Array
(
    [0] => Array
        (
        [id] => 1
        [name] => Game 1!
        [category_id] => 3
        [category] => games //the category element is in the [0] array.
    )

    [1] => Array
        (
            [id] => 2
            [name] => Game 2
            [category_id] => 3
            [category] => games
        )

here’s how the array looks right now:

Array
(
    [0] => Array
        (
            [category] => games //i want this to be in the [1] array
        )

    [1] => Array
        (
        [id] => 1
        [name] => Game 1!
        [category_id] => 3
    )

[2] => Array
    (
        [category] => games
    )

[3] => Array
    (
        [id] => 2
        [name] => Game 2
        [category_id] => 3
    )

here’s my function that gets all the products and puts them in the array.

function getAllProducts(){
    $data=array();
    $Q=$this->db->get('products');
    if($Q->num_rows() > 0){
        foreach($Q->result_array() as $row){
            $Q2=$this->db->query("select name FROM categories 
            where id={$row['category_id']}");
            if($Q2->num_rows() > 0){
                foreach($Q2->result_array() as $row2){
                    //Trouble Here: dont know how to push this into the array.
                    //is there a function that i can put in the $data[___] 
                    //area so that it knows it is in the [0] element, then [1],etc?
                    $data[]['category']=$row2['name'];  
                }

            }    
            $data[]=$row;
        }
    }
    $Q->free_result();
    return $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-15T13:48:56+00:00Added an answer on May 15, 2026 at 1:48 pm

    Either do:

    foreach($Q->result_array() as $key=>$row){
            $data[$key]=$row;
            $Q2=$this->db->query("select name FROM categories 
            where id={$row['category_id']}");
            if($Q2->num_rows() > 0){
                foreach($Q2->result_array() as $row2){
                    $data[$key]['category']=$row2['name'];  
                }
    
            }    
    
        }
    

    or even better, make the right query:

    function getAllProducts(){
        $data=array();
        $Q=$this->db->query("SELECT p.*, c.name as category FROM products p, LEFT JOIN categories c  ON c.id=p.category_id");
        if($Q->num_rows() > 0){
            foreach($Q->result_array() as $row){
                 $data[] = $row;
            }
        }
        $Q->free_result(); // <- needed here? don't know
        return $data;
    }
    

    I don’t know CodeIgniter, so I don’t know whether this is totally correct. But in general this is much better, as you reduce the number of DB access which is always good.
    This way, you have only one query instead of n+1 if you have n products.

    P.S.: I bet CodeIgniter also provides some APIs to make a JOIN. Read the documentation.

    I just saw, that you could do this:

    $this->db->select('*');
    $this->db->from('products');
    $this->db->join('categories', 'categories.id = products.category_id', 'left');
    
    $Q = $this->db->get();
    

    Make use of the API.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Kohana/CodeIgniter, I can have a URL in this form: http://www.name.tld/controller_name/method_name/parameter_1/parameter_2/parameter_3 ... And then
I'm trying to build VERY basic model->controller->view structure with CodeIgniter. I'd rather use models
I'm trying to build my first app on CodeIgniter. This is also my first
I'm trying to build my first app on CodeIgniter. This is also my first
How do you implement this in CodeIgniter?
This might seem like a stupid question I admit. But I'm in a small
I am trying to code my first codeigniter project. I have a login controller
I'm trying to access a model for global system settings. It should be loaded
I'm trying to get simplepie to loop through a few rss feeds using codeigniter
I', trying to re-format a date from a table in Codeigniter. The Controller is

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.