尝试打印从multidimentional数组返回的值作为codeigniter

时间:2016-08-23 07:00:08

标签: php mysql codeigniter codeigniter-2

无法在codeigniter视图中显示firstname和lastname作为下拉列表。现在它显示空白下拉列表。附加MVC代码供参考。模型正在返回正确的数据,即当我尝试执行此print_r($ data ['groups']); 死;在我的控制器中,它输出如下

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 ) ) 

我只是想将firstname和lastname打印为下拉值。

我的观看代码是:

<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>

我的控制器代码是:

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);
}

我的型号代码是:

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;
}

2 个答案:

答案 0 :(得分:1)

将您的代码更改为

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;




            }

只需更改$ return = $ query-&gt; result_array(); to $ return = $ query-&gt; result();

例如,如果您使用的是result_array(),那么就像

$query = $this->db->query("YOUR QUERY");

foreach ($query->result_array() as $row)
{
        echo $row['title'];
        echo $row['name'];
        echo $row['body'];
}

对于result(),它将是

$query = $this->db->query("YOUR QUERY");

foreach ($query->result() as $row)
{
        echo $row->title;
        echo $row->name;
        echo $row->body;
}

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

答案 1 :(得分:0)

使用codeigniter的内置下拉功能代替html选择标记。

<?php
$attributes = 'class = "form-control" id = "group"';
echo form_dropdown('group',$group,set_value('firstname'),$attributes);
?>