sarah
sarah

Reputation: 15

Display query single result without a loop in CodeIgniter

I want to display a single row data without looping through the results which are retrieved from database using a search function. How can I display first name and last name without the loop?

Following is the view

  <?php echo form_open('payroll/get/', ['id' => 'get', 'class' => 'form-horizontal']); ?>
  <?php echo form_close(); ?>
  <?php foreach ($emp as $E) :?>
  <h3>Employee name: <?php echo $E->first_name .'&nbsp'. $E->last_name ?></h3>
  <?php endforeach ?>

Controller

public function get(){
        $key= $this->input->post('get');
        if(isset($key) and !empty($key)){
            $data['allowance']=$this->payroll_model->get($key);
            $data['emp'] = $this->payroll_model->get_name($key);
            $data ['main_view'] = 'payroll/manage_sal';
            $this->load->view('layouts/main', $data);
        }

I am getting the results, but I have to loop through them in order to be displayed.

Upvotes: 0

Views: 306

Answers (1)

Ahmed Sheashaa
Ahmed Sheashaa

Reputation: 132

In your payroll_model, make sure you are returning an object that has a single result using

$query->row()

Then you will not have to use a loop, your html will be

<h3>Employee name: <?php echo $emp->first_name .'&nbsp'. $emp->last_name ?></h3>

Upvotes: 1

Related Questions