控制器"照片":
public function seePics(){
// in $res, i have all the pictures in my table 'photo' (id, id_photograph, title, size...)
$res = $this->Photo_model->readPhoto();
// with the session, i have all the data that i need on the connected user
$this->load->library('session');
$data = $this->session->userdata('data');
// user id
$idUser = $data['id'];
// here, i want to have only the picture of the connected user
if(!sizeof($res) == 0){
foreach($res as $item){
if($item->id_user == $idUser){
$values['photo'] = $item;
$this->load->view('photo/voirPhoto', $values);
}
}
}
else{
$this->load->view('photo/aucunePhoto',$data);
}
}
查看"voirPhoto.php":如何显示数据
<div class="row">
<div class="col-md-6">
<label>Title</label>
</div>
<div class="col-md-6">
<p><?php echo $photo->titre?></p>
</div>
</div>
<div class="row">
<div class="col-md-6">
<label>Name</label>
</div>
<div class="col-md-6">
<p><?php echo $photo->name?></p>
</div>
</div>
正如你所看到的,我的foreach在我的控制器中,但我想在我的视图中做foreach。我该怎么做?
因为我的问题是:如果连接的用户有3张图片,它将显示3倍的视图。
提前感谢
正如Viney所说,您可以在readPhoto函数中过滤查询中的用户。比如:$res = $this->Photo_model->readPhoto($idUser)
和
public function readPhoto($idUser = null)
{
$query = "select * from photos " . (($idUser)? "where id_user = {$idUser}" : "");
...
}
因此,您将拥有连接用户的所有照片。
如果只想捕获一行,可以在查询中首先使用$res[0]或(取决于数据库(。
控制器:
public function seePics($id=null){
$query['my_query'] = "Select * from photos where id _user =".$id;
}
视图:
<?php foreach($my_query as $key=>$val){
<?= $val['photo']?> //photo column name
}