Search code examples
phpcodeignitercodeigniter-3codeigniter-query-builder

Codeigniter - Sql Databases


I use 2 tables. First one of the table is Department. I want to show Departments to in User's Profile Page. The user can edit Department in Profile Page. But firstly he can see own department in Profile Page and Profile Edit Page. But There is nothing Department Field in both Profile Page and Profile Edit Page. Maybe Can I do that?

my code is below :

Controller of Profile:

public function index()
    {

        $viewData = new stdClass();
        $viewData->user = $this->db->get("user")->result();
        $viewData->departments = $this->db->get("department")->result();
        $this->lang->load('content', $this->session->userdata('userLang'));
        $this->load->view('profile', $viewData);
    }

My view of Profile:

<aside class="profile-info col-lg-9">
                <section class="panel">
                    <div class="bio-graph-heading">
                        <?php echo $this->lang->line('profile_top_profile_text'); ?>
                    </div>
                    <div class="panel-body bio-graph-info">
                        <h1><?php echo $this->lang->line('profile_text_informations'); ?></h1>
                        <div class="row">
                            <div class="bio-row">
                                <p><span><?php echo $this->lang->line('profile_first_name'); ?></span>: <?php echo $this->session->userdata('people_name'); ?></p>
                            </div>
                            <div class="bio-row">
                                <p><span><?php echo $this->lang->line('profile_last_name'); ?> </span>: <?php echo $this->session->userdata('people_surname'); ?></p>
                            </div>
                            <div class="bio-row">
                                <p><span>Username </span>: <?php echo $this->session->userdata('people_username'); ?></p>
                            </div>
                            <div class="bio-row">
                                <p><span>Email </span>: <?php echo $this->session->userdata('people_email'); ?></p>
                            </div>
                            <div class="bio-row">
                                <p><span>Language</span>: <?php if ($this->session->userdata('people_lang') == 'en') echo 'English'; else { echo 'Arabic'; } ?></p>
                            </div>
                            <div class="bio-row">
                                <p><span>Department </span>: <?php if ($this->session->userdata('people_department') == $departments->departmentId) { echo $departments->departmentName;} ?></p>
                            </div>
                            <div class="bio-row">
                                <p><span>User Type</span>: <?php if ($this->session->userdata('people_department') == 'en') echo 'English'; else { echo 'Arabic'; } ?></p>
                            </div>
                            <div class="bio-row">
                                <p><span>User Status</span>: <?php echo ($this->session->userdata('people_status') == 1) ? "<span class='label label-success''>Active</span>" : "<span class='label label-danger''>Deactive</span>" ?></p>
                            </div>
                            <div class="bio-row">
                                <p><span>Registered Date </span>: <?php echo $this->session->userdata('people_date'); ?></p>
                            </div>
                            <div class="bio-row">
                                <p><span>Modified Date </span>: <?php echo $this->session->userdata('people_mdate'); ?></p>
                            </div>
                        </div>
                    </div>
                </section>
            </aside>

Department Database:

CREATE TABLE `department` (
  `departmentId` int(11) NOT NULL AUTO_INCREMENT,
  `departmentOffice` int(11) NOT NULL,
  `departmentName` varchar(80) COLLATE utf8_turkish_ci NOT NULL,
  `departmentAuthPerson` int(11) DEFAULT NULL,
  PRIMARY KEY (`departmentId`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8 COLLATE=utf8_turkish_ci;

Should I do that? Or what can I do for this? Thanks for your helps, big community.


Solution

  • Hope this will help you:

    Use row() instead of result() in your index method

    public function index()
    {
       $viewData = new stdClass();
       $viewData->user = $this->db->get("user")->result();
       $viewData->departments = $this->db->get("department")->row();
       $this->lang->load('content', $this->session->userdata('userLang'));
       $this->load->view('profile', $viewData);
    }