php 如何将FullCalendar“v3”库集成到CodeIgnite 4中,引发错误“Undefined variable $data”

wgeznvg7  于 2022-12-02  发布在  PHP
关注(0)|答案(1)|浏览(111)

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

2wnc66cl

2wnc66cl1#

问题是教程的作者没有考虑到events数据库表中没有 'events' 的情况。
因此,在方法\App\Controllers\FullCalendar::index()中,如果数据库中不存在事件,则不会设置$data['data']数组键,从而导致在视图文件(app/Views/home.php)中未定义$data

***快速修复***可以通过为$data['data']数组键提供默认值来满足这种情况。例如:

  • \应用程序\控制器\完整日历::index()*
// ...
return view('home', array_key_exists('data', $data) ? $data : ['data' => []]);
// ...

虽然,我个人不喜欢作者的实现的一些事情。
1.他/她使用或多或少相同的现有数据修改同一结果集($data)。只有键被重命名。这同样可以通过使用查询生成器的->select(...)方法为表列名取别名来实现。
1.他/她使用->getResult()返回一个对象数组,但根据用例,使用->getResultArray()时,一个纯数组就足够了

解决方案:
  • \应用程序\控制器\完整日历::index()*
<?php

namespace App\Controllers;
use CodeIgniter\Controller;

class FullCalendar extends Controller
{

    /**
     * Write code on Method
     *
     * @return string
     */
    public function index()
    {
        $result = db_connect()
            ->table('calendar')
            ->select(['title', 'start_date AS start', 'end_date AS end'])
            ->limit(10)
            ->get()
            ->getResultArray();

        array_walk($result, function (&$value) {
            $value['backgroundColor'] = '#00a65a';
        });

        return view('home', ['data' => $result]);
    }

}
附录

您可以向数据库表中添加一些事件来测试它。例如:

INSERT INTO `calendar` (`id`, `title`, `start_date`, `end_date`)
VALUES (NULL, 'Ballet Dance', '2022-11-01', '2022-11-03'),
       (NULL, 'San Antonio Health & Fitness Expo', '2022-11-15', '2022-11-18')

相关问题