doujia1163 2014-05-29 15:43
浏览 79
已采纳

循环遍历数组并为每个元素打印HTML dinamcly

I am trying to loop through my array which contains objects and print out the data I need into my HTML. This HTML needs to print my first element and afterwards "create" a new HTML showing my second element and so on...

This is my PHP function where I query the DB:

public function getCuponesUser(){
    $db = JFactory::getDbo();
    $user = JFactory::getUser()->id;
    $query = 'SELECT * FROM #__cuphoneo_subscripcion as cs LEFT JOIN #__k2_items as k2i ON k2i.id = cs.item_id WHERE cs.user_id='.$user.' GROUP BY cs.item_id';
    $db->setQuery($query);
    $resultado = $db->loadObjectList();

    return $resultado;
}

This is what $resultado returns: http://pastebin.com/PSX7KMBq

And this is my HTML/PHP code where I am trying to print the data retrieved:

<div class="panel-group" id="accordion">
    <div class="panel panel-info">
 <?php  
    $model = $this->getModel();
    $cuponesUsuario[] = $model->getCuponesUser();

    foreach($cuponesUsuario as $valor){
    ?>

 <div class="panel-heading" data-toggle="collapse" data-parent="#accordion" data-target="#collapse1">
    <div class="panel-heading-left">
        <h4 class="panel-title accordion-toggle">
            Data 1
        </h4>
     </div>
     <div class="panel-heading-right">
        <h4 class="panel-title">
            Data 2
        </h4>
     </div>
</div>
<div id="collapse1" class="panel-collapse collapse">
  <div class="panel-body">
    <div class="panel-left">
        <div class="panel-image">
            Data 3
        </div>
        <div class="panel-fecha">
            <span class="label label-info">Data 4</span>
        </div>
    </div>

    <div class="panel-right">
        <div class="panel-desc">
        Data 5
        </div>
        <div class="panel-boton-canjear">
            <input class="btn" type="button" value="Canjear"/>
        </div>
    </div>
  </div>
</div>
    <?php } ?>

Where it says Data 1, Data 2, Data 3...etc, is where I need to print out the different values the Query may return.

EDIT: Im pretty sure I also have to use a for but not very sure how to do this :P

  • 写回答

2条回答 默认 最新

  • dounue1965 2014-05-29 15:51
    关注

    If $resultado already represents the array you want to loop over, then this is wrong:

    $cuponesUsuario[] = $model->getCuponesUser();
    

    Just do this:

    $cuponesUsuario = $model->getCuponesUser();
    

    Inside your foreach loop, you can access the data like this:

    <?php echo $valor->title; ?> 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?