CodeIgniter Forums

Full Version: Undefined variable: admin_id
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
New to Codeigniter need help




A PHP Error was encountered
Severity: Notice
Message: Undefined variable: admin_id
Filename: admins/admin-profile.php
Line Number: 16
Backtrace:
File: C:\xampp\htdocs\dgos\application\views\admins\admin-profile.php
Line: 16

Function: _error_handler

File: C:\xampp\htdocs\dgos\application\controllers\Admins.php
Line: 47

Function: view

File: C:\xampp\htdocs\dgos\index.php

Line: 315

Function: require_once
As, the error message says, line 16 in C:\xampp\htdocs\dgos\application\views\admins\admin-profile.php has not been set.
(01-10-2019, 11:28 PM)ciadmin Wrote: [ -> ]As, the error message says, line 16 in C:\xampp\htdocs\dgos\application\views\admins\admin-profile.php has not been set.
here is the view page I define it please take a  look 
 


 <div class="card-body">
                                    <?php
                                        $profile_info = $this->db->get_where('admin' , array('admin_id' => $admin_id))->result_array();
                                        foreach($profile_info as $row):?>
                                        <div class="mx-auto d-block">
                                            <img class="rounded-circle mx-auto d-block" src="<?php echo base_url().'assets/' ; ?>images/icon/avatar-11.jpeg" alt="Card image cap">

                                            <h5 class="text-sm-center mt-2 mb-1"><?php echo $row['name'];?></h5>
                                            <div class="location text-sm-center">
                                                <i class="fa fa-map-marker"></i> Karachi, Pakistan</div>
                                        </div>
Yes sir but I define this variable in admin-profile. Should I define that variable again in a controller of this view page or not
I don't see $admin_id defined there. I see an array, where the key 'admin_id' references some variable $admin_id, which must be defined elsewhere or you will get an undefined variable error message. Hmmmmm.
For one you should not be running database queries in the view that should be done in the controller.

Then assign the result to the $data array pass it to the view then loop through it.
(01-11-2019, 12:48 AM)ciadmin Wrote: [ -> ]I don't see $admin_id defined there. I see an array, where the key 'admin_id' references some variable $admin_id, which must be defined elsewhere or you will get an undefined variable error message. Hmmmmm.

Alright thanks
Try to add
if (!empty($admin_id)) {
// your code
}
Make sure you have an array key named 'admin_id' in your controller, and pass the array to the view.

Controller:
PHP Code:
$data['admin_id'] = 123;
$data['other_stuff'] = 'Other stuff';
$this->load->view('admins\admin-profile'$data); 
This way, the $admin_id variable is also available in your view.

Even better: get the $profile_info records in your controller instead of the view.
Controller:
PHP Code:
$data['profile_info'] = $this->db->get_where('admin' , array('admin_id' => $admin_id))->result_array();
$this->load->view('admins\admin-profile'$data); 
Now, in your view, $profile_info is an array that you can loop through.

And even better than that: create a model to retrieve the records from your database and use CI's query builder.
Model (Admin_model.php):
PHP Code:
public function get_admin_profile($admin_id)
{
 
  $query $this->db->where('admin_id'$admin_id)->get('admin');
 
  if ($query->num_rows() == 0) {
 
    return FALSE;
 
  else {
 
    return query->result_array();
 
  }


Controller:
PHP Code:
$this->load->model('admin_model');
$data['profile_info'] = $this->admin_model->get_admin_profile(123);
$this->load->view('admins\admin-profile'$data); 

This will keep your controllers lean. Load the model in any controller where you need the admin profile.
@Wouter60,

These are old posts the new users just reply to them to get recognized.