无法访问字符串 Codeigniter 4 上字符串类型的偏移量

Cannot access offset of type string on string Codeigniter 4

提问人:Sony Haryono 提问时间:11/7/2023 最后编辑:Sony Haryono 更新时间:11/8/2023 访问量:58

问:

我收到此错误:

无法访问字符串上字符串类型的偏移量

尝试使用foreach()

在这一行:

<li><a href="javascript:void(0);"><?= $tags['tag_berita'] ?></a></li>

错误日志:

CRITICAL - 2023-11-08 15:42:50 --> Cannot access offset of type string on string
in APPPATH\Views\front\detail_berita.php on line 253.
 1 SYSTEMPATH\View\View.php(226): include()
 2 SYSTEMPATH\View\View.php(229): CodeIgniter\View\View->CodeIgniter\View\{closure}()
 3 SYSTEMPATH\Common.php(1178): CodeIgniter\View\View->render('front/detail_berita', [], true)
 4 APPPATH\Controllers\Berita.php(73): view('front/detail_berita', [...])
 5 SYSTEMPATH\CodeIgniter.php(915): App\Controllers\Berita->detailBerita('wali-kota-bersama-kajari-pekalongan-teken-mou-bidang-perdata-dan-tun')
 6 SYSTEMPATH\CodeIgniter.php(492): CodeIgniter\CodeIgniter->runController(Object(App\Controllers\Berita))
 7 SYSTEMPATH\CodeIgniter.php(353): CodeIgniter\CodeIgniter->handleRequest(null, Object(Config\Cache), false)
 8 FCPATH\index.php(79): CodeIgniter\CodeIgniter->run()
 9 SYSTEMPATH\Commands\Server\rewrite.php(47): require_once('C:\\xampp\\htdocs\\admin\\public\\index.php')

detail_berita.php:

<div class="sidebar-item mb-4">
    <h4 class="">All Categories</h4>
    <ul class="sidebar-category">
        <li class="border-danger-subtle"><a class="stretched-link" href="javascript:void(0);">All</a></li>
        <?php foreach ($ktg as $ktgs) : ?>
            <li class="border-danger-subtle"><a class="stretched-link" href="javascript:void(0);"><?= $ktgs['kategori'] ?></a></li>
        <?php endforeach; ?>
    </ul>
</div>

<div class="sidebar-item mb-4">
    <h4 class="">Tags</h4>
    <ul class="sidebar-tags">
    <?php foreach ($tag as $tags) : ?>
        <li><a href="javascript:void(0);"><?= $tags['tag_berita'] ?></a></li>
    <?php endforeach; ?>
    </ul>
</div>

控制器:

public function detailBerita($slug_berita)
    {
        $data = [
            'judul' => 'Detail Berita'
        ];

        $data['beritaid'] = $this->b->select('*')
            ->select('tb_berita.updated_at as tgl_berita')
            ->join('tb_kategori', 'id_kategori = kategori_id', 'left')
            ->join('users', 'tb_berita.username = users.username', 'left')
            ->join('tb_user', 'tb_user.user_id = users.id', 'left')
            ->where('slug_berita', $slug_berita, 'after')
            ->find();

        $data['tag'] = $this->b->select('tag_berita')->findAll();

        $data['ktg'] = $this->k->select('*')->orderBy('kategori asc')->findAll();

        // dd($data);

        return view('front/detail_berita', $data);
    }

我想像对类别一样循环标签数据,类别可以正常显示,但是当我添加标签数据时出现错误

PHP 代码点火器-4

评论

0赞 Álvaro González 11/7/2023
我不使用 CodeIgniter,但错误消息应该说失败的行,您可能在粘贴到此处时错误地裁剪了它。
0赞 Sony Haryono 11/7/2023
@ÁlvaroGonzález 我添加了错误行
0赞 Álvaro González 11/7/2023
所以你认为这是一个数组,但实际上它是一段文本。$tags
0赞 steven7mwesigwa 11/8/2023
有关错误具体原因的详细信息,请参阅服务器的错误日志。如果不查看日志,就无法准确回答这个问题。尝试在writable/logs/log-2023-11-07.log
0赞 Sony Haryono 11/8/2023
添加了错误日志,标签不应该和 KTGS 一样吗?怎么可能是文本?

答: 暂无答案