在php中的bootstrap模式中显示foreach循环变量值

时间:2015-03-18 06:25:12

标签: php twitter-bootstrap

这是我显示服务列表的PHP代码

<?php foreach($services as $service) { ?>       
                        <a href="" id="fetch" data-toggle="modal" data-target="#myModal" class="btn-block"><?php echo htmlentities($service->services_title); ?></a>
                    <?php }?>

现在我想在bootstrap模型中显示详细描述,但它只显示第一个循环值从不显示其他值。

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel"><?php echo htmlentities($service->services_title); ?></h4>
      </div>
      <div class="modal-body">
<?php echo htmlentities($service->services_description); ?>   
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

1 个答案:

答案 0 :(得分:1)

所以我提供的概述代码,您可能需要编辑以满足您的确切需求......

// Append a unique data-target to every anchor    
// I assume the $service has id attribute

    <?php foreach($services as $service) { ?>       
         <a href="" id="fetch" data-toggle="modal" data-target="#myModal-<?php echo $service->id;?>" class="btn-block"><?php echo htmlentities($service->services_title); ?></a>
    <?php }?>



// Now in your markup there should be multiple modals to be targeted by your anchor tags like 

<div class="modal fade" id="myModal-1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  ...
</div>
<div class="modal fade" id="myModal-2" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  ...
</div>
<div class="modal fade" id="myModal-3" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  ...
</div>

注意:可以有其他好的方法,比如保持相同的模态,但是根据点击的锚点通过ajax调用它。这样你的标记就会变得不那么混乱,并且会根据要求添加数据