shank
shank

Reputation: 373

Populating Dropdown with values returned from Mysql database

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

Upvotes: 0

Views: 38

Answers (2)

muya.dev
muya.dev

Reputation: 977

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

Upvotes: 0

Don&#39;t Panic
Don&#39;t Panic

Reputation: 41810

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.

Upvotes: 1

Related Questions