Codeigniter 在视图中使用 foreach

Codeigniter using foreach in view

我在 Apache xampp 上使用 Codeigniter 设置并尝试在视图中使用 foreach,但我无法让它工作。

我的控制器代码:

class Test extends CI_Controller {

        public function index($page = 'testv')
    {
            if ( ! file_exists(APPPATH.'/views/'.$page.'.php'))
            {
                show_404();
            }
                $this->load->model('testm');
                $data['news'] = $this->testm->get_news();

                $this->load->view('headder');
                $this->load->view($page, $data);
                $this->load->view('footer');
    }
}

我的模特:

class Testm extends CI_Model {

    public function __construct()
    {
        parent::__construct();
    }

    Public function get_news()
    {
        $query = $this->db->query('SELECT id, date, text FROM news');

        foreach ($query->result_array() as $row)
        {
            echo $row['id'];
            echo $row['date'];
            echo $row['text'];
        }
    }
}

我的看法:

<main id='central_section'>
    <section id='logo_section'>
    <img src='<?php echo base_url(); ?>img/logo.png' id='small_logo' alt="Logo">
    </section>
    <section id='content'>
        <p class="large_headding">News</p>

            <?php foreach($news as $news) { ?>
                <article class="article_text">
                    <p class="segment_headding"><?php echo $news->date; ?></p>
                        <?php echo $news->text; ?>
                </article>
            <?php } ?>
        <div id="spacer"></div>
    </section>
</main>

目前我收到以下警告信息:

A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: views/testv.php

Line Number: 18

Backtrace:

File: C:\xampp\htdocs\tsh\CI\application\views\testv.php
Line: 18
Function: _error_handler

File: C:\xampp\htdocs\tsh\CI\application\controllers\test.php
Line: 16
Function: view

File: C:\xampp\htdocs\tsh\public_html\index.php
Line: 292
Function: require_once

但是我的数据库中的文本确实出现在页眉之外的页面顶部,所以我知道数据库正在连接并且正在收集其中的数据。谁能告诉我哪里出错了?

在您的控制器中,您正确地将模型的结果分配给变量:

$data['news'] = $this->testm->get_news();

但在您的模型中,您正在 ECHOING 结果,而不是 RETURNING 它们。所以 $data['news']null(因为模型的方法没有 return 任何东西),你不能迭代 null of 课程 -> 这就是你认为的错误.将您的模型更改为 return 数组,例如:

Public function get_news()
{
    $query = $this->db->query('SELECT id, date, text FROM news');
    return $query->result_array();
}

the text from my database does appear at the top of the page

因为,确实,您在输出视图之前回显了它(如上所述),这就是为什么您在 渲染之前看到它们的原因 html

        <?php foreach($news as $new) { ?>
            <article class="article_text">
                <p class="segment_headding"><?php echo $new->date; ?></p>
                    <?php echo $new->text; ?>
            </article>
        <?php } ?>

尝试 foreach($news as $new) 而不是 foreach($news as $news)

你的模型函数 foreach 循环有问题,只需将其替换为:

$query = $this->db->query('SELECT id, date, text FROM news');
$result = $query->result_array();
return $result;

使用

foreach($thing as $something): //it is important if not compulsory to use a different name for variables!!
some HTML
endforeach;

改为

请试试这个:

在您的视图页面中:

<?php foreach($news->result() as $new) { ?>
        <article class="article_text">
            <p class="segment_headding"><?php echo $new->date; ?></p>
                <?php echo $new->text; ?>
        </article>
    <?php } ?>

试试这个作为你的答案:

您的控制器:

public function index($page = 'testv')
{
        if ( ! file_exists(APPPATH.'/views/'.$page.'.php'))
        {
            show_404();
        }
            $this->load->model('testm');
            $data['news'] = $this->testm->get_news();

            $this->load->view('headder');
            $this->load->view($page, $data);
            $this->load->view('footer');
}

模型函数:

Public function get_news()
{
    $query = $this->db->query('SELECT id, date, text FROM news');
    return $query;
}

在查看文件中:

<?php foreach($news->result() as $new) { ?>
    <article class="article_text">
        <p class="segment_headding"><?php echo $new->date; ?></p>
            <?php echo $new->text; ?>
    </article>
<?php } ?>

您可以使用 HTML 兼容的 PHP 版本。这将有助于使用 PHP

自定义 HTML 代码

<?php echo $var; ?> 等于 <?= $var ?>

<?php foreach($allNews as $news): ?>
    <article class="article_text">
        <p class="segment_headding"><?= $news->date ?></p>
        <?= $news->text ?>
    </article>
<?php endforeach; ?>