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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:49:46+00:00 2026-05-26T18:49:46+00:00

Im new to codeigniter and php, mysql and i am making a codeigniter app,

  • 0

Im new to codeigniter and php, mysql and i am making a codeigniter app, and have a question about db querys using active records.

I want to loop through the “users” table and at the same time get a table called cv where the userid is equal to owner_id in the cv records. The cv table has a lot of columns like school, start_date, end_date, grades etc. Heres how i did it:

The controller:

function index() {
    $data['members'] = $this->user_model->_search_members();
    $data['main_content'] = 'agency/start';
    $this->load->view('site_view', $data);
}

The model:

function _search_members()
{

    $this->db->select('id,username,first_name,last_name,company,presentation,title_1,title_2,title_3,last_login,user_pic,counties,municipalities,birthday,gender,webpage')->from('users');
    $query = $this->db->get();
    if ($query->num_rows() > 0) {
        return $query->result();
    }
}

function _get_cv($id) {
    $this->db->select()->where('owner_id',$id);
    $query = $this->db->get('cv');

    return $query->result();
}

The view:

<section id="main">

<h2>Search members</h2>

<table>
    <tr>
        <td></td>
        <td>User</td>
        <td>Gender</td>
        <td>Name</td>
        <td>Title</td>
        <td>Location</td>
        <td>Age</td>
        <td>School</td>
    </tr>
    <?php foreach ($members as $member) : ?>
        <?php $cvs = $this->user_model->_get_cv($member->id); ?>
        <tr>
            <td><img src="<?=base_url()?>images/users/thumbs/<?=$member->user_pic;?>" alt=""></td>
            <td><a href="profile/view/<?php echo $member->id; ?>"><?php echo $member->username;?></a></td>
            <td><?php echo $member->gender;?></td>
            <td><?php echo $member->first_name; ?> <?php echo $member->last_name; ?></td>
            <td><?php echo $member->title_1; ?> / <?php echo $member->title_2; ?> / <?php echo $member->title_3; ?></td>
            <td><?php echo $member->counties; ?> i <?php echo $member->municipalities; ?></td>
            <td><?php echo $member->birthday;?></td>
            <td>
                <?php foreach ($cvs as $cv) : ?>
                    <?php echo $cv->school; ?>
                <?php endforeach; ?>
            </td>
        </tr>
    <?php endforeach; ?>
</table>

Now my question is, is this a good way to do it? I also want to be able to search through the users and cv tables on the page with a form i am creating later. Should i do a join on the tables instead to able to make better search queries or can i still do it this way?

Any help appreciated.

Regards

George

  • 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-26T18:49:46+00:00Added an answer on May 26, 2026 at 6:49 pm

    Use it like this:

    $this->db->select('*');
    $this->db->from('users');
    $this->db->join('cv', 'users.id = cv.owner_id','left');
    $query = $this->db->get();
    

    For more information:
    http://codeigniter.com/user_guide/database/active_record.html

    Updated:

    $this->db->select('*');
    $this->db->from('users');
    $this->db->join('cv', 'users.id = cv.owner_id','left');
    $data = $this->db->get();
    
    $group_cv = $users = array();
    foreach ($data as $k=>$v) {
        $group_cv[$v['id']][] = $v;
    }
    
    foreach ($data as $k=>$v) {
        $users[$v['id']] = $v;
    }
    
    ?>
    
    <section id="main">
    
    <h2>Search members</h2>
    
    <table>
        <tr>
            <td></td>
            <td>User</td>
            <td>Gender</td>
            <td>Name</td>
            <td>Title</td>
            <td>Location</td>
            <td>Age</td>
            <td>School</td>
        </tr>
        <?php foreach ($users as $member) : ?>
            <tr>
                <td><img src="<?=base_url()?>images/users/thumbs/<?=$member->user_pic;?>" alt=""></td>
                <td><a href="profile/view/<?php echo $member->id; ?>"><?php echo $member->username;?></a></td>
                <td><?php echo $member->gender;?></td>
                <td><?php echo $member->first_name; ?> <?php echo $member->last_name; ?></td>
                <td><?php echo $member->title_1; ?> / <?php echo $member->title_2; ?> / <?php echo $member->title_3; ?></td>
                <td><?php echo $member->counties; ?> i <?php echo $member->municipalities; ?></td>
                <td><?php echo $member->birthday;?></td>
                <td>
                    <?php foreach ($group_cv[$member['id']] as $cv) : ?>
                        <?php echo $cv->school; ?>
                    <?php endforeach; ?>
                </td>
            </tr>
        <?php endforeach; ?>
    </table>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to PHP/MySQL and super-new to CodeIgniter.. I have information in many MySQL
I am new to PHP codeigniter, how to get checkbox values using php Codeigniter
I'm using the CodeIgniter PHP framework. I have a simple form that has an
I'm new to codeigniter this is my case: I have a homepage with a
I'm new to MVC (I'm using codeigniter) and was wondering where I should put
I'm building a very small web ERP application with PHP / mySQL / CodeIgniter
I am new to CodeIgniter, but not new to PHP, and I was wondering
i have download the new codeigniter 2.0 and put my controller,model and view files
I am new to codeigniter, and not so new to PHP. I am developing
I am pretty new to codeigniter. I do know php. How can I accomplish

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.