【发布时间】:2014-12-16 16:01:16
【问题描述】:
所以,我正在试验一个网络应用程序。我想要做的是通过将登录用户的 id 与“帖子”数据库上的 id 匹配,在时间轴中显示用户的帖子列表。
这是我的模型
public function load_posts($user_id)
{
$this->db->where("user_id",$user_id);
$query=$this->db->get('posts');
if ($query->num_rows()>0)
{
foreach($query->result() as $rows)
{
$haha = array(
'post_id' => $rows->id,
'title' => $rows->title,
'artist' => $rows->artist,
'album' => $rows->album,
'caption' => $rows->caption,
);
}
return $haha;
}
return array();
}
我的控制器
public function welcome()
{
$data['title']= 'Welcome';
$user_id=$this->session->userdata('user_id');
$haha['hah'] = $this->user_model->load_posts($user_id);
$this->load->view('header_view',$data);
$this->load->view('welcome_view.php', $haha);
$this->load->view('footer_view',$data);
}
还有风景
<div class="timeline">
<div class="boxes">
<table>
<?php
foreach($hah as $rows):
?>
<tr>
<td><?php echo $rows['title']; ?></td>
<td><?php echo $rows['artist']; ?></td>
<td><?php echo $rows['album']; ?></td>
<td><?php echo $rows['caption']; ?></td>
</tr>
<?php
endforeach;
?>
</table>
</div>
</div>
使用这些代码,我得到 4 行这些错误
A PHP Error was encountered
Severity: Warning
Message: Illegal string offset 'title'
Filename: views/welcome_view.php
Line Number: 25
2
A PHP Error was encountered
Severity: Warning
Message: Illegal string offset 'artist'
Filename: views/welcome_view.php
Line Number: 26
2
A PHP Error was encountered
Severity: Warning
Message: Illegal string offset 'album'
Filename: views/welcome_view.php
Line Number: 27
2
A PHP Error was encountered
Severity: Warning
Message: Illegal string offset 'caption'
Filename: views/welcome_view.php
Line Number: 28
2
以防万一,我的“帖子”表
CREATE TABLE IF NOT EXISTS `posts` (
`id` int(11) NOT NULL,
`title` varchar(255) NOT NULL,
`artist` varchar(255) NOT NULL,
`album` varchar(255) NOT NULL,
`cover` varchar(30) NOT NULL,
`caption` text NOT NULL,
`user_id` int(11) NOT NULL,
`postTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)
任何帮助将不胜感激!
【问题讨论】:
-
如果将数据库“where”放在模型中的 get 函数上方会发生什么?
-
您已将
$haha附加到您的视图中,并尝试在$hahforeach($hah as $rows):上循环。不是笔误吗?我在任何地方都没有看到$hah。 -
$haha与$hah? -
其实可以在 $haha['hah'] = $this->user_model->load_posts($user_id); (在控制器中。我只是在玩弄变量名。
-
@Craig: 哦,是的,我交换了它们只是想看看它是否对任何东西有影响,结果不是,我忘记把它放回原来的位置,然后再把它贴在这里。恢复到原来的状态
标签: php database codeigniter