I am using CodeIgniter 4. I want to do this Full Calendar, I followed evry step in the tutorial
**ErrorException Undefined variable $data **
This is my View File
<div class="container">
<h1>Codeigniter Fullcalendar</h1>
<div class="row" style="width:50%">
<div class="col-md-12">
<div id="calendar"></div>
</div>
</div>
</div>
<script type="text/javascript">
var events = <?php echo json_encode($data) ?>;
var date = new Date()
var d = date.getDate(),
m = date.getMonth(),
y = date.getFullYear()
$('#calendar').fullCalendar({
header : {
left : 'prev,next today',
center: 'title',
right : 'month,agendaWeek,agendaDay'
},
buttonText: {
today: 'today',
month: 'month',
week : 'week',
day : 'day'
},
events : events
})
</script>
This is my Controller
public function index() {
$db = \Config\Database::connect();
$builder = $db->table('calendar');
$query = $builder->select('*')
->limit(10)->get();
$data = $query->getResult();
foreach ($data as $key => $value) {
$data['data'][$key]['title'] = $value->title;
$data['data'][$key]['start'] = $value->start_date;
$data['data'][$key]['end'] = $value->end_date;
$data['data'][$key]['backgroundColor'] = "#00a65a";
}
return view('calendar', $data);
}
Iam using C14, and try to use full calendar in my project. But this happens when I try this code for Full Calendar. Can someone help
1条答案
按热度按时间2wnc66cl1#
问题是教程的作者没有考虑到
events
数据库表中没有 'events' 的情况。因此,在方法
\App\Controllers\FullCalendar::index()
中,如果数据库中不存在事件,则不会设置$data['data']
数组键,从而导致在视图文件(app/Views/home.php
)中未定义$data
。***快速修复***可以通过为
$data['data']
数组键提供默认值来满足这种情况。例如:虽然,我个人不喜欢作者的实现的一些事情。
1.他/她使用或多或少相同的现有数据修改同一结果集(
$data
)。只有键被重命名。这同样可以通过使用查询生成器的->select(...)
方法为表列名取别名来实现。1.他/她使用
->getResult()
返回一个对象数组,但根据用例,使用->getResultArray()
时,一个纯数组就足够了解决方案:
附录
您可以向数据库表中添加一些事件来测试它。例如: