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 7816395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:51:13+00:00 2026-06-02T05:51:13+00:00

For example, if I run one query in model: public function list_users() { $q

  • 0

For example, if I run one query in model:

public function list_users() { 
       $q = "SELECT user_id, username FROM users";
       return $q->result_array();
}

And now, to lists posts from that user, I need to refer to it’s id within this function:

   public function list_posts() { 
           $q = "SELECT post_id, post_title, post_content FROM posts 
                 WHERE user_id = what??";
           return $q->result_array();
   }

OK both of these functions are in Model. Now, How to use RESULT from list_users() in list_posts(). Please have in mind that I need to pass ARRAY of IDs and, to use it only for particular id from list_users() which also returns ARRAY

I know I can use joined query, but that’s not the point at all, as I have lots of queries that I need to split

  • 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-06-02T05:51:15+00:00Added an answer on June 2, 2026 at 5:51 am

    why arent you using a JOIN statement, and making two queries into one?

    This will reduce db load, decrease query times, and also reduce clutter in your models.

    SELECT p.post_id, p.post_title, p.post_content, u.user_id, u.username FROM posts p LEFT JOIN users u ON u.user_id = p.user_id
    

    you can also do this using active records. Which will avoid having to use full blown queries, and more of a CI methodology to SQL.

    http://codeigniter.com/user_guide/database/active_record.html

    $this->db->select('p.post_id, p.post_title, p.post_content, u.user_id, u.username');
    $this->db->from('posts p');
    $this->db->join('users u', 'u.user_id = p.user_id');
    $q = $this->db->get();
    
    $q->result();
    

    Edit:
    You can return the value as an object.. IE: $this->user_id then reference it in the posts function. Ideally you should call the first function in your Controller, return $user_id and then reference that in your next function.. This is definitely not best case though, you should use JOINs as they are less taxing on the db.

    //controller
    
    function test(){
      $users = $this->exampleModel->list_users();
      //manipulate user data if needed
      $posts = $this->exampleModel->list_posts($users);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way I can run a query from one database in DB2
This query takes 16 seconds to run SELECT WO.orderid FROM WebOrder as WO INNER
I used to have one function to any select query in my script, but
Situation: I'm trying run an https store (xcart) under one domain secure.example.com and I
struct GPattern() { int gid; .... } class Example() { public: void run(string _filename,
after these for example $result = mysql_query(SELECT id, name FROM mytable) i tried to
I would like to run a simple query to get all users of a
What I have is a mySQL query that will select some drinks and return
i have a few developers who constantly put If null checks For example: Run(Order
I'm trying to run the example program for POSIX message queues found in the

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.