php - Codeigniter,没有 $data 的未定义变量
问题描述
萨拉姆,我遇到了一个在视图中未定义的变量的问题。我之前没有$data
在控制器中使用变量。在我使用 之后$data
,它仍然没有出现在我的视图中(称为仪表板)。有人可以帮我解决这个代码吗?
对不起,我知道以前有这样的问题。但我想知道我的,有什么问题,除了我在另一个文件中的代码很好
home.php 作为控制器
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 作为视图
<?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; ?>"><</a><?php echo $html_title; ?> <a href="?ym=<?php echo $next; ?>">></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 }?>
我也很好奇$ym
。变量$prev
,$html_title
并且$next
没有被识别。我应该通过此代码吗?
谢谢你
解决方案
我认为您正在寻找这种结构:
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);
}
}
您希望在视图中可用的所有变量都需要定义为您传递的数组中的键,在这种情况下$data
:
$data['myVar'] = 'myValue'
将对应于视图中的以下内容:
var_dump($myVar); // result: myValue
您可以对要传递的数组执行相同操作:
$data['myArray'] = [1, 2, 3, 4];
并在视图中访问它,如下所示:
var_dump($myArray); // result: [1, 2, 3, 4]
推荐阅读
- flutter - Flutter:增加 appBar 图标高度
- hyperledger-fabric - 如何防止用户直接看到块文件
- excel - 在 Range 中查找一个单词并再次查找,直到 Range 结束
- php - 如何在 PHP 中合并表格数据
- mysql - 如何返回时间戳的日期集合
- javascript - 由于目标被视为被动,无法在被动事件侦听器中阻止默认值。Three.js 轨迹球控件
- android - 键盘打开时Android警报对话框不居中
- javascript - 当人加入昵称 IRC
- jquery - 将接收到的数据从 ajax 发送到另一个 ajax 作为文件
- reactjs - 测试使用函数构建的 React 组件