doushijia5684 2015-11-28 12:05
浏览 39
已采纳

Yii:使用Cdbcriteria检索数据

I am yiibie. What I am trying to do is to retrieve data from my database, and for that i am using cDbCriteria. I want to get the last 4 images from my database table which is named Event. and then want to display those 4 images individually. I am using this method but not getting any result, please help me with this.

      <?php
 $Criteria = new CDbCriteria();
$Criteria->limit = 4;
$Criteria->order = "image DESC";
$Criteria->select = "id, image";
$Events = Event::model()->findAll($Criteria);
foreach ( (array)$Events as $Event)
{
    $Event[1]=$image1;
    $Event[2]=$image2;
    $Event[3]=$image3;
    $Event[4]=$image4;

}
  ?>
<div class="row">
<h3>Events</h3>
<div class="col-md-3">
<div class="thumbnail">

   <img src="<?php echo $image1  ?>">

<div class="caption">
    <a href="join.php"> <button class="btn btn-primary center-block">Join</button></a>
  </div>
    </div>
</div>
  • 写回答

2条回答 默认 最新

  • doucheng8471 2015-11-28 12:17
    关注

    findAll() method retrieves an array of ActiveRecords, so you need a foreach loop. Try:

    foreach ( (array)$Events as $Event)
    {
       echo "
          <div class='col-md-3'>
             <div class='thumbnail'>
                <img src='$Event->image' >
                <div class='caption'>
                   <a href='join.php'><button class='btn btn-primary center-block'>Join</button></a>
                </div>
             </div>
          </div>
       ";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据