php Codeigniter:foreach 方法或结果数组?[型号+查看]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12108237/
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
Codeigniter: foreach method or result array?? [Models +View]
提问by Shimsham84
I am currently following tutorials on viewing data from the database using the Framework Codeigniter. There are various ways in which I've learnt. Is there is a more realiable way- either displaying as an array or using 'foreach' in the view file? Any opinions would be helpful.
我目前正在学习使用 Framework Codeigniter 查看数据库中的数据的教程。我学到了各种各样的方法。有没有更可行的方法 - 显示为数组或在视图文件中使用“foreach”?任何意见都会有所帮助。
This is my code using the two methods:
这是我使用两种方法的代码:
Method 1 Model:
方法一模型:
function getArticle(){
$this->db->select('*');
$this->db->from('test');
$this->db->where('author','David');
$this->db->order_by('id', 'DESC');
$query=$this->db->get();
if($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}$query->free_result();
}
}
}
Method 1 View file:
方法一查看文件:
<?php foreach($article as $row){ ?>
<h3><?php echo $row->title; ?></h3>
<p><?php echo $row->content; ?></p>
<p><?php echo $row->author; ?></p>
<p><?php echo $row->date; ?></p>
<?php } ?>
Method 2 Model: class News_model extends CI_Model {
方法二模型:class News_model extends CI_Model {
function getArticle(){
$this->db->select('*');
$this->db->from('test');
$this->db->where('author', 'David');
$this->db->order_by('id', 'DESC');
$query=$this->db->get();
if ($query->num_rows()>0) {
return $query->row_array();
}
$query->free_result();
}
Method 2 View file:
方法二查看文件:
<?php echo '<h3>' .$article['title'].'</h3>' ?>
<?php echo '<p>' .$article['content']. '</p>' ?>
<?php echo '<p>' .$article['author']. '</p>' ?>
<?php echo '<p>'. $article['date']. '</p>' ?>
回答by Sergey Telshevsky
I would do it like that:
我会这样做:
Model
模型
function getArticle() {
$this->db->select('*');
$this->db->from('test');
$this->db->where('author','David');
$this->db->order_by('id', 'DESC');
return $this->db->get()->result();
}
}
}
Controller
控制器
function get_tests() {
$data = array(
'tests' => $this->mymodel->getArticle()
}
$this->load->view('myview', $data);
}
View
看法
<table>
<?php foreach($tests as $test) { ?>
<tr>
<td><?php echo $test->title;?></td>
<td><?php echo $test->content;?></td>
<td><?php echo $test->author;?></td>
<td><?php echo $test->date;?></td>
</tr>
</table>
If you wish to work with arrays rather than objects, in your model change line
如果您希望使用数组而不是对象,请在模型更改行中
return $this->db->get()->result();
to
到
return $this->db->get()->result_array();
and in views echo like
并在意见回声像
<td><?php echo $test['title'];?></td>
P.S.
聚苯乙烯
In your code you use $query->free_result();but it doesn't even run because when you use keyword returneverything after that is not even parsed. It's not necessary to free results anyway.
在您使用的代码中$query->free_result();,它甚至没有运行,因为当您使用关键字时,return之后的所有内容甚至都没有被解析。无论如何都没有必要释放结果。
P.S.2.
PS2。
You use if($query->num_rows() > 0) {but don't have the elsepart, it means that it's not necessary too. If you'll return no lines to your foreach statement in the view, you won't get any errors.
您使用if($query->num_rows() > 0) {但没有else零件,也意味着它没有必要。如果您不向视图中的 foreach 语句返回任何行,则不会出现任何错误。

