Question

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

Was it helpful?

Solution

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'];

OTHER TIPS

Try this:

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

$this->data = array('generated' => array('name' => $name, 'department'=> $department);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top