0

salam, I'm having a problem with a variable that is undefined in view. I don't use a $data variable in the controller before. after I use the $data, it still remains doesn't appear in my view (called dashboard). Can someone help me to solve this code?

I'm sorry, I know there are questions like this before. but I want to know mine, what is the problem, besides my code in another file are fine

home.php as controller

class Home extends CI_Controller{
public function __construct(){
    parent::__construct();
    $this->loginstatus->check_login();
    $this->load->library('template');
    $this->load->model('day_off_model');
}

public function index(){
    redirect('home/info');
}

public function info(){
    $data = array();
    $data['ym'] = date('Y-m');
    $this->template->display('information/dashboard');

    date_default_timezone_set('Asia/Tokyo');

    //$data['harian'] = 'a';
    // Get prev & next month
    if (isset($_GET['ym'])) {
        $ym = $_GET['ym'];
    } else {
        // Bulan ini
        $ym = date('Y-m');
    }
    //echo $ym;

    // Check format
    $timestamp = strtotime($ym . '-01');
    if ($timestamp === false) {
        $timestamp = time();
    }

    // Today
    $today = date('Y-m-j', time());

    // H3 title
    $html_title = date('m Y', $timestamp);

    $parts =  explode(' ',$html_title);
    $date   = $parts[0].' '.$parts[1];

    // return $date;
    // print_r($html_title);
    // die();

    $html_title=$this->tanggal->tanggal_indo_month_yeartext($date);

    // print_r($html_title);
    // die();

    // link prev next     mktime(hour,minute,second,month,day,year)
    $prev = date('Y-m', mktime(0, 0, 0, date('m', $timestamp)-1, 1, date('Y', $timestamp)));
    $next = date('Y-m', mktime(0, 0, 0, date('m', $timestamp)+1, 1, date('Y', $timestamp)));

    // Jumlah hari seblan
    $day_count = date('t', $timestamp);

    // 0:Minggu 1:Senin 2:Selasa ...
    $str = date('w', mktime(0, 0, 0, date('m', $timestamp), 1, date('Y', $timestamp)));
    //$str = date('w', mktime(0, 0, 0, date('m', $timestamp), 1, date('Y', $timestamp))); //senin, selasa, rabu

    // Create Calendar
    $weeks = array();
    $week = '';

    // cell dengan tabel kalender
    $week .= str_repeat('<td></td>', $str);
    //$week .= str_repeat('<td></td>', $str-1);

    for ( $day = 1; $day <= $day_count; $day++, $str++) {

        $date = $ym.'-'.$day;
        $exist = $this->day_off_model->get_all(false)->num_rows();
        // print_r($exist);
        // die();
        if ($today == $date) {
            $week .= '<td class="today">'.$day;
        } else {
            $week .= '<td>'.$day;
        }
        $week .= '</td>';

        // End of the week OR End of the month
        if ($str % 7 == 6 || $day == $day_count) {
        //if ($str % 7 == 0 || $day == $day_count) {
            if($day == $day_count) {
                // empty cell
                $week .= str_repeat('<td class="column_custom"></td>', 6 - ($str % 7));
                //$week .= str_repeat('<td></td>', 7 - ($str % 7));
            }

            $weeks[] = '<tr>'.$week.'</tr>';

            // weeks
            $week = '';
        }
    }
    //redirect('home/info');
}

dashboard.php as view

<?php if($this->session->userdata('user_type_id')==TRUE){?>
<div class="row">
<div class="col-md-12">
    <h1>Sistem Informasi Absensi Kepegawaian</h1>
    <hr/>
    Informasi terbaru Absensi Kepegawaian<br/>
    Pascasarjana Universitas Diponegoro
    <?php echo validation_errors(); ?>
</div>
</div>
<div class="row">
<?php echo $ym; ?>
<div class="col-md-4">
    <h3><a href="?ym=<?php echo $prev; ?>">&lt;</a><?php echo $html_title; ?> <a href="?ym=<?php echo $next; ?>">&gt;</a></h3>
    <br>
    <table class="table table-bordered dashboard">
        <tr>
            <th class="th_custom" width="30px">Minggu</th>
            <th class="th_custom" width="30px">Senin</th>
            <th class="th_custom" width="30px">Selasa</th>
            <th class="th_custom" width="30px">Rabu</th>
            <th class="th_custom" width="30px">Kamis</th>
            <th class="th_custom" width="30px">Jum'at</th>
            <th class="th_custom" width="30px">Sabtu</th>
        </tr>
        <?php
            foreach ($weeks as $week) {
                echo $week;
            }   
        ?>
    </table>
</div>
</div>
<?php }
else{?>
<h3>Silahkan login sebagai admin terlebih dahulu</h3>
<?php }?>

I'm curious about $ym too. variable $prev, $html_title and $next are not identified. Should I pass this code?

thank you

6
  • What do your routes look like? Instead of redirecting, you should load a view and pass the variables in there. Have a look at this documentation for dynamically adding data to a view: codeigniter.com/user_guide/general/… Commented May 30, 2018 at 17:03
  • I'm using home as default $route['default_controller'] = "home"; $route['404_override'] = ''; $route['translate_url_dashes'] = true; Commented May 30, 2018 at 17:20
  • So it looks like from your info method you need to load a view instead of redirecting. Like this: $this->load->view('home/info', $data);. You need to group up all of the data you wish to pass to the view into a single array. Also, in your index method instead of redirecting, maybe just do return $this->info(); Commented May 30, 2018 at 17:37
  • it occurs an error An Error Was Encountered Unable to load the requested file: home/info.php Commented May 30, 2018 at 22:23
  • Make sure that your view file is in the correct directory: application/views/home/info.php. You would create a folder for your class, and a view file for each method. Commented May 30, 2018 at 23:04

2 Answers 2

1

I think you're looking for this sort of structure:

class Home extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        // load other stuff
    }

    public function index()
    {
        $this->info();
    }

    public function info()
    {
        $data = [];

        // group all data you wish to pass to the view inside the $data array in this format:
        // $data[<variableName>] = <variableValue>

        $this->load->view('myView', $data);
    }
}

All of your variables that you want available inside the view need to be defined as a key in the array that you pass, in this case $data:

$data['myVar'] = 'myValue'

Will correspond to the following in the view:

var_dump($myVar); // result: myValue

You can do the same with arrays that you want to pass:

$data['myArray'] = [1, 2, 3, 4];

And access it in the view as follows:

var_dump($myArray); // result: [1, 2, 3, 4]
Sign up to request clarification or add additional context in comments.

1 Comment

this is incredibly helping my work. thank you for the debug with var_dump, I found what I miss. thanks again
0

In the Controller Change these lines

public function info(){
    $data = array();
    $data['ym'] = date('Y-m');
    $this->template->display('information/dashboard');

To

public function info(){
    $data['ym'] = date('Y-m');
    $this->template->display('information/dashboard', $data);

In the view file, you can echo the ym's value like this;

echo $ym;

1 Comment

You're most welcome. Mark this as the answer, else others may bother for typing more answers

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.