0

i'm newbie codeigniter programmer.

i want to use function and all variable from this function to another function in same class controller. this my code

function page()
{   

    $page_id = $this->uri->segment(3);
    $page_details = $this->m_module->submenu($page_id)->row_array();
    $data['title']= $page_details['sub_title'];
    $data['menu'] = $page_details['title'];
    $data['submenu'] = $page_details['sub_title'];
    $data['link'] = $page_details['sub_target'];
    $data['page_id'] = $page_id;
}

function employee()
    {
        $data['employee']= $this->m_module->employee()->result_array();
        $this->page();      
        $this->template->display($data['link'],$data);
    }

the problem is variable function page() can't call in function employee().

this i get at browser

A PHP Error was encountered

Severity: Notice

Message: Undefined index: link

Filename: controllers/Module.php

Line Number: 39

please help.

Thank you

tereško
  • 58,060
  • 25
  • 98
  • 150
Mardino Ismail
  • 61
  • 1
  • 1
  • 9
  • You need to set a private variable inside the containing class e.g. private link and then set it inside your page function like $this->link = $data['link'] and then use $this->link to reference it in employee. – iSZ Dec 07 '17 at 01:03
  • can you give example from my code? – Mardino Ismail Dec 07 '17 at 01:34
  • I've added an example below. Is your code inside a CI class, something like below? – iSZ Dec 07 '17 at 01:53

2 Answers2

2

Update your method like the following

private function page() {
$page_id = $this->uri->segment(3);
$page_details = $this->m_module->submenu($page_id)->row_array();

return [
    'title' => $page_details['sub_title'];
    'menu' => $page_details['title'];
    'submenu' => $page_details['sub_title'];
    'link'=> $page_details['sub_target'];
    'page_id' => $page_id;
];

}

private function employee() {
  $data = $this->page();
  $data['employee']= $this->m_module->employee()->result_array();
 $this->template->display($data['link'],$data);
}

The reason you were not getting was because your method was not returning as value.

usrNotFound
  • 2,680
  • 3
  • 25
  • 41
  • thank you brother, but now i have problem, variable $data['employee'] can't call in view page. error this code A PHP Error was encountered Severity: Notice Message: Undefined variable: employee Filename: human_capital/employee.php Line Number: 24 i want to array $data['employee'] at view page like this – Mardino Ismail Dec 07 '17 at 01:36
  • check the updated code. – usrNotFound Dec 07 '17 at 01:37
  • thank you so much Bro. this very helped. – Mardino Ismail Dec 07 '17 at 01:40
0

You can set a class variable to pass the value of link between functions

<?php
    class MyController extends CI_Controller {
        private $link;

        public function page($data){
            $this->link = $data['link'];
        }

        public function page(){
            echo $this->link;
        }

    }
?>
iSZ
  • 682
  • 5
  • 10