0

Good Day! I have this code and I don't know what could be the problem with my code 'casue I can't echo the value of variable in view.

This is my controller:

public function generate()
{
    $name    =   $this->input->post('name');

    $this->data['generated'] = $this->user_models->generate_name($name);
    $this->load->view('templates/header');
    $this->load->view('generated_user_views', $this->data);
    $this->load->view('templates/footer');
}

The Model:

function generate_name($name)
{
    $this->db->select('*');
    $this->db->from('user');
    $this->db->where('name', $name);
    $query = $this->db->get();

    return $query->result_array();
}

In View:

<?php
    $view_name = $generated['name'];
    $view_department = $generated['department'];
?>
<div class="form-group">
    <label for="name" class="col-sm-2 control-label emp">Name</label>
    <div class="col-sm-10">
        <input type="text" class="typeahead form-control" id="name" placeholder="Name" name="name" autocomplete="off"><?php echo $view_name; ?></input>
    </div>
</div>
<div class="form-group">
    <label for="department" class="col-sm-2 control-label emp">Department</label>
    <div class="col-sm-10">
        <input type="text" class="typeahead form-control" id="department" placeholder="Department" name="department" autocomplete="off"><?php echo $view_department; ?></input>
    </div>
</div>

I don't know why I always have an error like:

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

A PHP Error was encountered Severity: Notice Message: Undefined index: department Filename: views/generated_user_views.php Line Number: 13

1
  • can you a post what is the value of $this->data before you pass it to the view? Commented May 13, 2014 at 6:46

2 Answers 2

1

In your model you are selecting all the data from your table so there is a multidimensional array returned,In view you need to loop over your results,see docs

foreach ($generated as $row)
{

   echo $row['name'];
   echo $row['department'];
} 

Or i guess if you need a single record from your table then you can use $query->row_array();in your model and in view you can access them as

   $view_name =$generated['name'];
   $view_department = $generated['department'];
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

$name = $this->input->post('name');
$department = $this->user_models->generate_name($name);

$this->data = array('generated' => array('name' => $name, 'department'=> $department);

1 Comment

method generate_name returns array from user table, not only department value

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.