0

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.

1 Answer 1

1

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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.