Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I could not retrive a value from controller page [duplicate]

I am using codeigniter framework. I have a table named client. I need to get the first name from the table and send it to my view page. I have a function in model file to retrieve the client data. This is my model- function:

public function get_cli($id)
    {
    $this->db->select('*');
        $this->db->from('client');
        $this->db->where('id', $id);
        $res = $query->result();
        $row = $res[0];
        return $row->first_name;

    }

Here I get my client first name. This is my controller file. Here I have a function to send the data from the client table to my view page.

public function ticket($id)
    {
        $id=$this->uri->segment(4);
        $data['id']=$this->uri->segment(4);
        $data['client']=$this->billing_model->get_cli($id);
        $data['employee']=$this->employee_model->emp_name();
        $data['main_content'] = 'admin/billing/ticket_page';
        $this->load->view('includes/template', $data);  
     }

My view page.

<label>Client Id:<?=$id?></label>
<input type="text" value="<?=$client['first_name'];?>"/>

I am getting a white screen. Why is this happenening? Can someone help me with the code?

like image 996
Anu Avatar asked Nov 20 '25 21:11

Anu


1 Answers

Change your model function to this:

public function get_cli($id)
{
    $this->db->select('*');
    $this->db->from('client');
    $this->db->where('id', $id);
    $query = $this->db->get();
    $result = $query->result_array();
    return $result[0]['first_name']; //returning only the first name key value of first row(0th row) of the result;
}

Then On the view page:

<input type="text" value="<?=$client;?>"/>

Which will print only the first name;

Another approach:

Change your model function to this:

public function get_cli($id)
{
    $this->db->select('*');
    $this->db->from('client');
    $this->db->where('id', $id);
    $query = $this->db->get();
    $result = $query->result_array();
    return $result; //returning the whole array;
}

Then On the view page:

<input type="text" value="<?=$client[0]['first_name'];?>"/>

Which will print only the first name value from the 0th(first row) of the array;

like image 67
Saswat Avatar answered Nov 23 '25 13:11

Saswat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!