Php 在视图中使用foreach的Codeigniter

Php 在视图中使用foreach的Codeigniter,php,database,codeigniter,foreach,views,Php,Database,Codeigniter,Foreach,Views,我正在ApacheXAMPP设置中使用Codeigniter,并首次尝试在视图中使用foreach,但我无法让它工作 我的控制器代码: class Test extends CI_Controller { public function index($page = 'testv') { if ( ! file_exists(APPPATH.'/views/'.$page.'.php')) {

我正在ApacheXAMPP设置中使用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>
但是我数据库中的文本确实出现在标题器之外的页面顶部,因此我知道数据库正在连接,并且正在收集其中的数据。有人能告诉我哪里出错了吗?

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

$data['news'] = $this->testm->get_news();
但在你的模型中,你是在重复结果,而不是返回结果。因此,
$data['news']
null
(因为模型的方法不返回任何内容),并且您不能迭代
null of
course->,这就是您视图中的错误。更改模型以返回数组,例如:

Public function get_news()
{
    $query = $this->db->query('SELECT id, date, text FROM news');
    return $query->result_array();
}
“我的数据库”中的文本确实显示在页面顶部

因为,事实上,您在输出视图之前会回显它(如上所述),这就是为什么您会在呈现的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作为$new)
而不是
foreach($news作为$news)
您的模型函数foreach循环中出现了问题,请将其替换为:

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

相反,请尝试以下方法:

在您的查看页面中:

<?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代码

等于



谢谢,只需对视图文件稍作调整,即可将调用从更改为此,效果非常理想!不是我,我支持你,因为我发现你的解释很有用。我没有将你的答案标记为正确答案的唯一原因是,伊姆兰首先回答了问题,他的答案虽然没有包含太多细节,但确实回答了我的问题。没有冒犯的意思。
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 } ?>
<?php foreach($allNews as $news): ?>
    <article class="article_text">
        <p class="segment_headding"><?= $news->date ?></p>
        <?= $news->text ?>
    </article>
<?php endforeach; ?>