php Codeigniter 在视图中使用 foreach

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29679003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:30:59  来源:igfitidea点击:

Codeigniter using foreach in view

phpdatabasecodeigniterforeachviews

提问by Tessa

I'm using Codeigniter on an Apache xampp set up and attempting to use foreach in a view for the first time and I just can't get it to work.

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

My Controller code:

我的控制器代码:

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');
    }
}

My Model:

我的型号:

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'];
        }
    }
}

My View:

我的看法:

<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>

At present I get the following warning message:

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

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

But the text from my database does appear at the top of the page outside of the headder so I know the database is connecting and the data from it is being collected. Can anyone tell me where I'm going wrong?

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

采纳答案by Imran Qamer

you just have issue in your model function foreach loop just replace that with:

您只是在模型函数 foreach 循环中遇到问题,只需将其替换为:

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

回答by Damien Pirsy

In your controller you're correctlyassigning the result of the model to a variable:

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

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

but in your model you're ECHOINGthe result, not RETURNINGthem. SO $data['news']is null(since the model's method doesn't return anything), and you can't iterate over null ofcourse -> and that's the error in your view. Change your model to return an array, for example:

但在模型中你呼应的结果,而不是RETURNING他们。SO$data['news']null(因为模型的方法不返回任何内容),并且您无法遍历null of课程 -> 这就是您认为的错误。更改模型以返回数组,例如:

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

我的数据库中的文本确实出现在页面顶部

because, indeed, you're echoing it (as pointed out above) before the views are outputted, that's why you see them beforethe rendered html

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

回答by Damien Pirsy

please try this:

请试试这个:

in your view page:

在您的视图页面中:

<?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 } ?>

回答by Uraj

Try this for your answer:

试试这个你的答案:

your 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');
}

model function :

模型功能:

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

in view file :

在视图文件中:

<?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 } ?>

回答by ryush00

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

Try foreach($news as $new)instead of foreach($news as $news).

尝试foreach($news as $new)代替foreach($news as $news).

回答by Omar El Don

use

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

instead

反而

回答by Gayan

you can use HTML compatible PHP version. that will help customize HTML code with PHP

您可以使用 HTML 兼容的 PHP 版本。这将有助于使用 PHP 自定义 HTML 代码

<?php echo $var; ?>is equal to <?= $var ?>

<?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; ?>