Welcome Guest, Not a member yet? Register   Sign In
Undefined variable: admin_id
#1
Bug 

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
Reply
#2

As, the error message says, line 16 in C:\xampp\htdocs\dgos\application\views\admins\admin-profile.php has not been set.
Reply
#3

(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>
Reply
#4

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
Reply
#5

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.
Reply
#6

(This post was last modified: 01-11-2019, 04:53 AM by InsiteFX.)

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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#7

(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
Reply
#8

Try to add
if (!empty($admin_id)) {
// your code
}
Reply
#9

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.
Reply
#10

@Wouter60,

These are old posts the new users just reply to them to get recognized.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB