0

I am getting user profile fields from the database and want to display in my view but don't know to do this with the master layout.

this is my model

function fetchProfile($id,$page){
 $query = $this->db->query("SELECT * FROM staff_master WHERE Id='$id'");
 return $query->result(); 
}

this is my controller:

public function edit($id,$page){
    $data['query'] =  $this->StaffModel->fetchProfile($id,$page);
    $data= array('content'=>'view_staff_edit');
    $this->load->view('template_master',$data);
}

I am also trying to find a solution. I am passing user Id and another by URL (get method).

  • Take a look at this: https://stackoverflow.com/a/9446865/5530965 – dokgu Feb 14 '18 at 21:32
  • 1
    This is the official documentation: https://codeigniter.com/user_guide/general/views.html#adding-dynamic-data-to-the-view – dokgu Feb 14 '18 at 21:33

4 Answers4

2

You are overwriting $data['query'] when you assign the array next:

$data['query'] =  $this->StaffModel->fetchProfile($id,$page);
$data= array('content'=>'view_staff_edit');

Either do:

$data= array('content'=>'view_staff_edit');
$data['query'] =  $this->StaffModel->fetchProfile($id,$page); // note position

Or:

$data = array(
    'content' = 'view_staff_edit',
    'query' => $this->StaffModel->fetchProfile($id,$page),
);

Access in view via $query and $content.

Unrelated:

You are also missing $page in your query, and its generally a good idea to declare gets as null if not set or you will get a notice: public function edit($id=null,$page=null){

Alex
  • 9,215
  • 8
  • 39
  • 82
2

Your overriding your first declaration of variable $data what you can do is to initialize them both at the same time.

Controller

public function edit($id,$page){
    $data = array(
        'query'   => $this->StaffModel->fetchProfile($id,$page),
        'content' => 'view_staff_edit'
    );
    $this->load->view('template_master',$data);
}

Then access it on your View file

<h1><?php echo $content ?></h1>

<?php foreach($query as $result): ?>

    <p><?php echo $result->id ?></p>

<?php endforeach; ?>
Riyenz
  • 2,498
  • 2
  • 9
  • 23
0

Try doing something like this:

public function edit($id,$page) {

    $data['query'] =  $this->StaffModel->fetchProfile($id,$page);
    $data['content']= 'view_staff_edit';
    $this->load->view('template_master',$data);

}
Omar Einea
  • 2,478
  • 7
  • 23
  • 35
0

YOUR MODEL

function fetchProfile($id){
    return $this->db->get_where('Id' => $id)->result_array(); 
}

YOUR CONTROLLER

public function edit($id,$page){
$data = array(
    'query'   => $this->StaffModel->fetchProfile($id),
    'content' => 'view_staff_edit',   
);
$this->load->view('template_master',$data);
}

YOUR "template_master" VIEW

<?php foreach ($query as $res){?>
<span><?php echo $res['Id'];?></span> //User html content according to your requirements ALSO you can add all columns retrieved from database
<?php } ?> 
Danish Ali
  • 2,354
  • 3
  • 15
  • 26