我在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
但我的数据库中的文本确实出现在headder之外的页面顶部,所以我知道数据库正在连接,并且正在收集来自它的数据。谁能告诉我哪里出错?
答案 0 :(得分:3)
在您的控制器中,您正确将模型的结果分配给变量:
$data['news'] = $this->testm->get_news();
但是在您的模型中,您 ECHOING 结果,而不是 RETURNING 。结果$data['news']
是null
(因为模型的方法不会返回任何内容),并且您无法迭代null of
课程 - &gt;那是您视图中的错误。更改模型以返回数组,例如:
Public function get_news()
{
$query = $this->db->query('SELECT id, date, text FROM news');
return $query->result_array();
}
我的数据库中的文字确实出现在页面顶部
因为确实,在输出视图之前,你会回复它(如上所述),这就是为什么你在之前看到渲染的html
的原因答案 1 :(得分:2)
请试试这个:
在您的视图页面中:
<?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 } ?>
答案 2 :(得分:1)
<?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)
。
答案 3 :(得分:1)
你的模型函数中只有问题foreach循环只需将其替换为:
$query = $this->db->query('SELECT id, date, text FROM news');
$result = $query->result_array();
return $result;
答案 4 :(得分:1)
使用
foreach($thing as $something): //it is important if not compulsory to use a different name for variables!!
some HTML
endforeach;
代替
答案 5 :(得分:1)
试试这个答案:
你的控制器:
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 } ?>
答案 6 :(得分:0)
您可以使用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; ?>