将图像作为Codeigniter中的链接

时间:2016-12-08 21:19:29

标签: php html codeigniter

我正在尝试将图片作为Codeigniter中的链接,但<a>标记无效。我试图将图像作为链接,但在foreach循环<a>标记内部不起作用。

这是我的代码

 <?php if(count($trendpost)):?>
    <?php if(count($trendpost)<=3):?>
      <div class="row">
          <?php foreach($trendpost as $post): ?>
            <div class="col-lg-4 col-md-4">
              <a href=<?php echo site_url($post->url)?>> <img  class="imageborder trendimg" src=<?php echo site_url($post->link)?>></a>

                 <p><?= $post->news ?></p>
            </div>
            <?php endforeach; ?>
       </div>
       <?php else: ?>
       <div class="row">
           <?php foreach($trendpost as $post) :?>
             <div class="col-lg-4 col-md-4">
                 <img class="imageborder trendimg" src=<?php echo site_url($post->link)?>>
                 <p><?= $post->news ?></p>
             </div>
           <?php endforeach; ?>
        </div>
       <?php endif ?>
       <?php else: ?>
          <p> not found</p>
       <?php endif ?>
  </div>
</div>

2 个答案:

答案 0 :(得分:0)

将您的代码更改为此,请再次检查。

<?php if(count($trendpost)):?>
<?php if(count($trendpost)<=3):?>
  <div class="row">
      <?php foreach($trendpost as $post): ?>
        <div class="col-lg-4 col-md-4">
          <a href="<?php echo site_url($post->url)?>" style="display: block;"> <img  class="imageborder trendimg" src=<?php echo site_url($post->link)?>></a>

             <p><?= $post->news ?></p>
        </div>
        <?php endforeach; ?>
   </div>
   <?php else: ?>
   <div class="row">
       <?php foreach($trendpost as $post) :?>
         <div class="col-lg-4 col-md-4">
           <a href="<?php echo site_url($post->url)?>" style="display: block;">
             <img class="imageborder trendimg" src=<?php echo site_url($post->link)?>></a>
             <p><?= $post->news ?></p>
         </div>
       <?php endforeach; ?>
    </div>
   <?php endif ?>
   <?php else: ?>
      <p> not found</p>
   <?php endif ?>

答案 1 :(得分:0)

您可以使用

<?php echo anchor('your Controller_name/link','<img src="your path"  /> ','class="btn btn-default btn-flat"')?>

而不是锚标记