1

I have following print_r ($rolePermissions); array result and wondering how can i access only permIDdynamically.

 Array ( [0] => Array ( [0] => stdClass Object ( [roleID] => 1 [permID] => 1 ) [1] => stdClass Object ( [roleID] => 1 [permID] => 2 ) [2] => stdClass Object ( [roleID] => 1 [permID] => 3 ) [3] => stdClass Object ( [roleID] => 1 [permID] => 4 ) [4] => stdClass Object ( [roleID] => 1 [permID] => 5 ) [5] => stdClass Object ( [roleID] => 1 [permID] => 6 ) [6] => stdClass Object ( [roleID] => 1 [permID] => 7 ) [7] => stdClass Object ( [roleID] => 1 [permID] => 8 ) [8] => stdClass Object ( [roleID] => 1 [permID] => 9 ) [9] => stdClass Object ( [roleID] => 1 [permID] => 10 ) [10] => stdClass Object ( [roleID] => 1 [permID] => 11 ) [11] => stdClass Object ( [roleID] => 1 [permID] => 12 ) [12] => stdClass Object ( [roleID] => 1 [permID] => 13 ) [13] => stdClass Object ( [roleID] => 1 [permID] => 14 ) [14] => stdClass Object ( [roleID] => 1 [permID] => 15 ) [15] => stdClass Object ( [roleID] => 1 [permID] => 16 ) [16] => stdClass Object ( [roleID] => 1 [permID] => 17 ) [17] => stdClass Object ( [roleID] => 1 [permID] => 18 ) [18] => stdClass Object ( [roleID] => 1 [permID] => 19 ) [19] => stdClass Object ( [roleID] => 1 [permID] => 20 ) [20] => stdClass Object ( [roleID] => 1 [permID] => 21 ) [21] => stdClass Object ( [roleID] => 1 [permID] => 22 ) [22] => stdClass Object ( [roleID] => 1 [permID] => 23 ) [23] => stdClass Object ( [roleID] => 1 [permID] => 24 ) ) [1] => Array ( [0] => stdClass Object ( [roleID] => 2 [permID] => 2 ) [1] => stdClass Object ( [roleID] => 2 [permID] => 3 ) [2] => stdClass Object ( [roleID] => 2 [permID] => 4 ) ) [2] => Array ( [0] => stdClass Object ( [roleID] => 3 [permID] => 4 ) ) [3] => Array ( ) ) 

I am accessing with following code:

$rolePermission[0]->permID

But i am unable to get all the records because results can be varied each time and index 0 is not feasible way to do this. It displays some records and pop up errors in others.

Error messages:
Undefined offset: 0
Message: Trying to get property of non-object

any help ?

1
  • 1
    Have provided with a solution for the request that you have asked the question. Have a try and share thoughts. Commented Oct 7, 2016 at 5:28

3 Answers 3

1

Either loop over the array:

$ids = array();
foreach($rolePermission as $permissionObject){
    $ids[] = $permissionObject->permID;
}

Or check that the key exists before accessing it:

if(isset($rolePermission[0])) {
    $id = $rolePermission[0]->permID;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Specifically you may want to take a look at is_a also: php.net/manual/en/function.is-a.php, you can check to see if the object is a stdClass too
I would suggest an instanceof statement rather than an is_a statement. Eg: $rolePermission[0] instanceof stdClass
0

Note: When you use the print_r() it will output the array with stdClass Object in CI when you are pulling out a value from the Database.

There is a solution to display the array without using the stdClass Object while iterating.

Example: Consider $final consider the array and while using print_r() it displayed the stdClass Object.

  • You have to use the loop as follows so that it avoids the stdClass Object while printing it.

Code:

This code you can use it for the values to retrieve from the DB using the Controller and Model.

If single row of the output is retrieved from the DB

<?php
foreach($final->result() as $single)
{
   //You can print the variable values over here as follows (E.g) echo $single->id    
}
?>

If Multiple row of the output is retrieved from the DB

<?php
$row=array();
foreach($final->result() as $single)
{
   //You can store it as an array here if you are going on with multiple loops
   $row[] = $single; 
}
print_r($row); // here you can save it as an another array
?>

How should the model Code look like if you are using `->result()` in the foreach to get the values

Here is the sample that your model should look like if you are using the above methods for the retrieving of the output.

employee_model.php

<?php
class Employee_model extends CI_Model{
function __construct() {
parent::__construct();
}

public function getEmployees()
{
    $this->db->select('*');
    $this->db->from('employee');
    $this->db->where('delete_status','0');
    $this->db->where('status','1');
    $this->db->order_by('id','DESC');
    $query = $this->db->get();
    return $query;
}
?>

How to call the model from the controller

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends Layout_Controller {

public function __construct(){
     parent::__construct();
     $this->load->library("pagination");
     $this->load->model('employee_model');
     $this->load->model('ajax_model');         
 }

 public function employee_listing()
 {
     $result['all_employee'] = $this->employee_model->getEmployees(); // getEmployees is the function name in the employee_model
     $this->load->view('frontend/employee_list',$result);
 }

Comments

0

in Model write this code. last line will get the value of expire column only and will pass to controller. then you can access it form view

 function album_expire($user_id ,$album_iid)
{
  $this->db->select('expire');
  $this->db->from('mw_album');
  $this->db->where('user_id', $user_id); 
  $this->db->where('id', $album_iid); 
  return $this->db->get()->row()->expire;
}

in controller pass value to view

$data['album_expire'] = $this->albums_model->album_expire($user_id ,$album_iid);

in view access it like this

<?php echo $album_expire; ?>

Comments

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.