2

I am trying to fetch some values from the database and I'm getting this error

A PHP Error was encountered
Severity: Notice
Message: Undefined index: name
Filename: views/pro_view.php
Line Number: 12

My controller:

$this->load->model('profile_model');

$data['profile_data'] = $this->profile_model->get_records();

$this->load->view('pro_view', $data);

Model:

function get_records()
{
    $r = $this->uri->segment(3);
    $query = $this->db->get_where('profile', array('id' => $r));
    return $query->result();
}

View:

<input type="text" name="name" value="<?php echo $profile_data["name"]  ?>" />

if I var_dump(($profile_data); I'm getting everything in an array.

I read other similar questions and their answers, no luck.

2
  • By default CI return as a object and you need to echo like this in view for every value which you want to print $profile_data[0]->name Commented Sep 17, 2015 at 10:36
  • this is just what I needed. Commented Sep 17, 2015 at 10:44

2 Answers 2

4

If you need to fetch single row then use

$query = $this->db->get_where('profile', array('id' => $r));
$ret = $query->row();
return $ret->name;

Controller

$data['name'] = $this->profile_model->get_records();

$this->load->view('pro_view', $data);

View

<input type="text" name="name" value="<?php echo $name;?>" />

And for your present code you need to use foreach loop

 foreach (profile_data as $row)
   { ?>
<input type="text" name="name" value="<?php echo $row->name;?>" />

  <?php }?>

result()

This method returns the query result as an array of objects, or an empty array on failure. Typically you’ll use this in a foreach loop

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

6 Comments

i have like 20 type of values in row, you know like a normal profile. Name, phone, etc.
You need to use foreach loop as i post in my answer
i was getting an error. $profile_data[0]->name in the view worked for me.
@Saty error is because not accessing the value using the key index in and he is using function for single row
But using my code there is no change to get any error. I'm providing answer for both fetch single row or fetching object from database!!!
|
1

Controller

    $this->load->model('profile_model');

    $data['profile_data'] = $this->profile_model->get_records();

    $this->load->view('pro_view', $data);

Model

$r = $this->uri->segment(3);
$this->db->select('*');
$this->db->from('profile');
$this->db->where('id',$r);
$q = $this->db->get();
if($q->num_rows() > 0){
$row = $q->row();
return $row->name;
}
return false;

view

<input type="text" name="name" value="<?php echo $name;?>" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.