Yii: Not getting images from database using CDbCri

2019-09-09 11:12发布

I am a yiibie.I am trying to get images from my database table named event and i am using CDbCriteria. I have a little bit knowledge of how using it but right now i am unable to retrieve the images from my database, might be i am writing my code wrong or mixing few things. By using this code i am not getting any error, i am not just getting the image, please help me with this thing, thank you.

<?php
 $Criteria = new CDbCriteria();
$Criteria->limit = 4;
$Criteria->order = "id DESC";
$Criteria->select = "id, image";
$Events = Event::model()->findAll($Criteria);
  ?>
<div class="row">
<h3>Events</h3>
<?php
foreach ( (array)$Events as $Event)
{
   echo "
      <div class='col-md-3'>
         <div class='thumbnail'>
            <img src='<? php echo Yii::app()->request->baseurl;?>$Event->image' >
            <div class='caption'>
               <a href='join.php'><button class='btn btn-primary center-block'>Join</button></a>
            </div>
         </div>
      </div>
   ";
}
?>
<a href="events.php"> <p class="view">View all</p></a>

</div><!--row ending here-->

标签: php yii
1条回答
Ridiculous、
2楼-- · 2019-09-09 11:54

First and most obvious bug is in loop. Your code should look like this:

<?php
foreach ( (array)$Events as $Event)
{
   echo "
      <div class='col-md-3'>
         <div class='thumbnail'>
            <img src="'.Yii::app()->request->baseurl.'/img folder path/'.$Event->image.'">                <div class='caption'>
               <a href='join.php'><button class='btn btn-primary center-block'>Join</button></a>
            </div>
         </div>
      </div>
   ";
}
?>

It should be enough to make your code work. The way you were concatenating strings (by using inside outer one) is incorrect. In PHP strings are concatenated by . sing.

查看更多
登录 后发表回答