Search code examples
phpmysqlcodeignitercodeigniter-2

Trying to print values returned from multidimentional array as dropdown in codeigniter


No able to display firstname and lastname as dropdown in codeigniter view. Now its displaying blank dropdown. Attached MVC code for reference. Model is returning the correct data i.e when i try to do this print_r($data['groups']); die; in my controller it gives the output as below

Array ( [0] => Array ( [id] => 62 [userid] => 67 [emp_code] => 050 [emp_shift] => General Shift [emp_category] => Full Time [cccode] => [prefix] => Mr. [firstname] => Divya [middlename] => [lastname] => Darshini [designation_id] => 9 [division_id] => [branch_id] => 5 [company] => [email] => shashankbhat11@gmail.com [personal_email] => [nickname] => [url] => [birthday] => 23-09-1990 [panno] => [passportno] => [passportdate] => [joining_date] => 08-07-2016 [resignation_date] => [team_id] => [tag_ids] => [im] => [facebook] => [twitter_handle] => [linkedin_id] => [instagram] => [googleplus] => [maidenname] => [profileimage] => [blood_group] => B Positive [gender] => Female [married] => [anniversary] => 01-01-1970 [ctc] => [mood] => [doc_type] => [org_to] => [org_from] => [org] => [role_title] => [org_skills] => [scanned_doc_work] => [pass_year] => [adm_year] => [degree] => [univ] => [edu_scanned_doc] => [declare_home] => [declare_menu1] => [declare_family] => [declare_menu4] => [declare_menu7] => [declare_menu8] => [declare_menu2] => [declare_menu3] => [s_cluster_id] => 1 [status] => 1 [deleted] => 0 [registrationtime] => 1466579573 [timemodified] => 0 [modifierid] => 0 [alternate_email] => [notice_period] => 0 ) ) 

I am just trying to print the firstname and lastname as dropdown values.

My view code is:

<table>
<tr>
<td>
Name
</td>
<td>

<select class="form-control" name="name">
<?php 
foreach($groups as $row){ 
    echo '<option  value="'.$row->firstname.'">'.$row->firstname.' '.$row->lastname.'</option>';
}
?>
</select>
</td>
</tr>

<tr>
<td>
Exit Type
</td>

<td>
<select class="form-control" id="security_question_1" name="exit_type">
   <option name="exit_type" value="" selected>Select a option</option>
   <option name="exit_type" value="Absconding">Absconding</option>
</select>
</td>
</tr>

<tr></tr>
<tr>
<td>
<label>Absconding Since</label>
</td>
<td>
<div class="date" data-date="12-02-2012" data-date-format="mm-dd-yyyy" data-date-viewmode="years">
<input placeholder="Absconding Since" class=" m-wrap col-md-8 form-control " id="enddt" type="text" name="abscondingsince" value="<?php if($row->requested_date!='') echo date("d-m-Y",$row->requested_date); ?>" required/>
</td>
</tr>

<tr>
<td>
<label>Date of contact via Phone</label>
</td>
<td>
<div class="date" data-date="12-02-2012" data-date-format="mm-dd-yyyy" data-date-viewmode="years">
<input placeholder="Date of contact via Phone" class=" m-wrap col-md-8 form-control " id="enddtt" type="text" name="dateofcontactviaphone" value="<?php if($row->requested_date!='') echo date("d-m-Y",$row->requested_date); ?>" required/>

</td>
</tr>
<td>
<label>
Response/Outcome from attempt to contact through phone
</label>
</td>
<td>
<textarea class="form-control" name="commentsphone"></textarea>
</td>

<tr>
<td>
<label>Date of contact via email</label>
</td>
<td>
<div class="date" data-date="12-02-2012" data-date-format="mm-dd-yyyy" data-date-viewmode="years">
<input placeholder="Date of contact via email" class=" m-wrap col-md-8 form-control " id="enddttt" type="text" name="dateofcontactviaemail" value="<?php if($row->requested_date!='') echo date("d-m-Y",$row->requested_date); ?>" required/>
</td>
</tr>
<td>
<label>
Response/Outcome from attempt to contact through email
</label>
</td>
<td>
<textarea class="form-control" name="commentsemail"></textarea>
</td>

<tr>

<td>
Attach Email Sent to Employee<input multiple="multiple" name="userfile1[]" size="20" type="file" />
</td>

<td>
Attach Phone Logs made to Employee<input multiple="multiple" name="userfile2[]" size="20" type="file" />
</td>
</tr>
</tr>
</table>

My controller code is :

function manager_add_absconding(){
    global $SITE,$USER;
	 $data = array();
	 $data['row'] = new stdClass();
	 $data['row'] = $this->admin_init_elements->set_post_vals($this->input->post());
	 $data['offices']=$this->mod_common->get_all_offices();
	 $clients = currentuserclients();
	 $data['roles'] = $this->mod_common->get_cat_array('designation','status',"1' AND id > '0",'designation');
			
	 $data['reasons'] = $this->exit_common->get_all_reasons();
	 $id = $this->uri->segment(3);

	 //$data['roles'] = $this->exit_common->get_cat_array('designation','status',"1' AND id > '0",'designation');
			get_city_state_country_array($data,array('cityid'=>$data['row']->cityid)); 
    $data['error_message'] = '';
	$data['row']->id = $this->uri->segment(3);
	$data['id'] = $this->uri->segment(3);			
	$data['action'] = 'add';
	$data['heading'] = 'Add';			
	$data['msg_class'] = 'sukses';
	$data['path']=$path;		
	$post_action = $this->input->post('action');

	if($post_action=='add' || $post_action =='update' ){
		$post_array = $this->input->post();
		$action = ($post_action == 'add')?'inserted':'updated';
		
        //echo '<pre>';print_r($SITE);die;

		$post_array['exit_type'] = 'Employee Initiated';
		$data['managerid'] = $this->exit_common->get_managerids($id);
		$data['error_message'] = $this->exit_common->add_new_absconding_alert($post_array,$action);
					
	    if($data['error_message'] == 'Record '.$action.' successfully'){
			$data['row'] = new stdClass();
			$data['row']->id = $this->uri->segment(3);	
			$data['row']->status = 1;
		}
	}

	$data['grievance_types'] = $this->mod_common->get_user_allowed_leaves();
	
    if($data['row']->id>0){
	    $data['action'] = 'update';
	    $data['heading'] = 'Edit';
	    $data['rows'] = $this->mod_common->get_leave_request($data['row']->id);
	    $clid = $data['row']->id;	
	    $data['row']->id = $clid;
	}

	//$data['my_detail'] = $this->mod_common->get_my_details($USER->id);
	$data['my_detail'] = $this->exit_common->get_details_profile($USER->id,'users_details','userid');	
	$data['my_detail']->userdetail = $this->exit_common->get_details($USER->id,'users');
	get_address($data['my_detail'],ADDRESS_TYPE1,$USER->id);
	$data['cities']=$this->exit_common->get_array_frontend('city');
	$data['notice_period']=$this->exit_common->get_notice_period($USER);
	$data['groups'] = $this->exit_common->get_all_names_by_user($USER);
	$this->data['maincontent'] = $this->load->view('maincontents/manager_add_new_exit', $data,true);
    $this->load->view('layout', $this->data);
}

My model code is :

function get_all_names_by_user($USER){

        $this->db->select('g.*,firstname,lastname');
	$this->db->from('pr_users_details as g'); 
	$this->db->where('userid', $USER->id); 
	//$this->db->join($this->myTables['pr_users_details'].' as ud','ud.userid = g.userid');
	//$this->db->join('pr_users_details as ud','ud.userid = g.userids');
	/*	$this->db->join($this->myTables['users_details'].' as ud','ud.userid = g.userid');
	$this->db->join('pr_resignation_type as gt','gt.id = g.sr_type');*/
	$query=$this->db->get();	
	$return	= $query->result_array();
	return $return;
}


Solution

  • Change your code to

    function get_all_names_by_user($USER){
    
    
                $this->db->select('g.*,firstname,lastname');
                $this->db->from('pr_users_details as g'); 
                $this->db->where('userid', $USER->id); 
                //$this->db->join($this->myTables['pr_users_details'].' as ud','ud.userid = g.userid');
                //$this->db->join('pr_users_details as ud','ud.userid = g.userids');
    
                /*  $this->db->join($this->myTables['users_details'].' as ud','ud.userid = g.userid');
                $this->db->join('pr_resignation_type as gt','gt.id = g.sr_type');*/
                $query=$this->db->get();    
    
                $return = $query->result();
    
                return $return;
    
    
    
    
                }
    

    Just change the $return = $query->result_array(); to $return = $query->result();

    For example if you are using result_array() then the would be like

    $query = $this->db->query("YOUR QUERY");
    
    foreach ($query->result_array() as $row)
    {
            echo $row['title'];
            echo $row['name'];
            echo $row['body'];
    }
    

    where as for result() it would be

    $query = $this->db->query("YOUR QUERY");
    
    foreach ($query->result() as $row)
    {
            echo $row->title;
            echo $row->name;
            echo $row->body;
    }
    

    Refrence:- https://www.codeigniter.com/userguide3/database/results.html