0

I am not able to populate the dropdown by concatenating firstname and lastname from a mysql database. I tried some queries but none of them seem to work. I know i have an error in my query but not able to figure out what is going on. Also i have pasted my MVC code below along with the image of my table.

My controller code is: exits.php

function admin_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');
    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');
    $data['groups'] = $this->exit_common->get_all_names();

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

        if($data['error_message'] == 'Record '.$action.' successfully'){
            $data['row'] = new stdClass();
            $data['row']->id = $this->uri->segment(3);
            $data['row']->status = 1;
        }

    }

My Model Code is: exit_common.php

function get_all_names(){

    $query = $this->db->query('SELECT firstname,lastname FROM pr_users_details');
    echo $this->db->last_query();
    die;

    return $query->result();
}

My view code is: backend_add_new_exit.php

<select class="form-control">
    <?php 

    foreach($groups as $row)
    { 
        echo '<option value="'.$row->firstname.'">'.$row->lastname.'</option>';
    }
    ?>
</select>

My Mysql table is : enter image description here

4
  • what is error ?? explain more. Unclear Commented Aug 2, 2016 at 16:09
  • Why not do the name creation in MySQL: SELECT concat(firstname,' ',lastname) as Name Commented Aug 2, 2016 at 16:15
  • solved .. but the view is not rendered properly from the controller Commented Aug 2, 2016 at 16:31
  • 1
    Please edit your question to clarify these questions: 1) What is the current output of your code? and 2) How is that different from what you want it to be? Commented Aug 2, 2016 at 16:55

2 Answers 2

1

In your get_all_names() function, you echo the query and die before returning the result. die will stop your script immediately.

Based on the table definition you showed, your query does not appear to have any errors, but there are many possible reasons it could fail other than SQL syntax errors.

Sign up to request clarification or add additional context in comments.

Comments

0

Try this on your model exit_common.php

<?php

function get_all_names(){
		$query = $this->db->get('pr_users_details');
		if ($query->num_rows() > 0)
		{
		   return $query->result();
		}
	}

?>

On you Views use the below code instead

<select class="form-control">
<?php foreach($groups as $row):?>
	  <option value="<?php echo $row->firstname; ?>"> <?php echo $row->lastname; ?> </option>
	<?php endforeach; ?>
</select>

Hope that will help

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.