Codeigniter - 在每x行后插入广告

时间:2014-09-13 11:35:12

标签: codeigniter ads

我想在从数据库返回的每个X行之后插入广告。 我的控制器看起来像这样:

class Home extends CI_Controller {

    public function __construct(){
        parent::__construct();
        $this->load->database();
        $this->load->library('parser');
        $this->load->helper('url');
        $this->load->helper('download');
        $this->load->model('Categorii');
        $this->load->model('Comentarii');
    }


    public function index($pages = ""){ 

        $c_ar = $this->Categorii->iaCategorii();
        $sql = (!empty($pages)) ? "SELECT * FROM bancuri WHERE categorie = '" . $c_ar[$pages] . "'" : "SELECT * FROM bancuri";

        $dbQ = $this->db->query($sql);

        $renderData = array("bancuri" => $dbQ->result_array(), "ad" => "<img src='http://placehold.it/350x150'>"); //Se stocheaza datele intr-o matrice     

        $this->parser->parse('pages/bancuri',$renderData);
    }

视图看起来像这样:

<div class="col-lg-8 col-sm-12 col-xs-12">
        {bancuri}
        {ad}
          <div class="corgi_feed_well">
            <div class="individual_feed_item">
              <div class="feed_item">
                <div class="feed_body">
                    <div class="row">
                        <!--
                         <audio controls>
                          <source src="<?php echo $this->config->item('base_url'); ?>resources/mp3/{mp3}" type="audio/mpeg">
                        </audio> 
                        -->
                    </div>
                  <div class="row">
                    <div class="feed_profile_pic">
                        <span class="label label-info">Rating: {rating}</span>
                        <div class="clearfix">&nbsp;</div>
                        <button type="button" class="btn btn-success">Like</button>
                    </div>
                    <div class="feed_text">
                      <p class="well">{banc}</p>
                    </div>
                  </div>
                </div>
                <div class="comment_area">
                  <p>Adauga un comentariu</p>
                  <form>
                    <textarea rows="3" class="span6"></textarea><br/>
                    <input type="submit" class="btn" />
                  </form>
                </div>
                <hr class="feed_hr" />
                <div class="bottom_meta">
                  <div class="row">
                    <div class="bottom_left">
                      <div class="share_wrapper">
                        <div class="share"><a href="#" class="share_button"><i class="icon-heart"></i></a></div>
                        <div class="share_hidden">
                          <ul class="hover_heart">
                            <span class="internal_heart"><i class="icon-heart"></i> Trimite bancul pe</span> 
                            <div class="social_links">
                              <li><span><i class="icon-twitter"></i> Twitter</span></li>
                              <li><span><i class="icon-facebook"></i> Facebook</span></li>
                              <li><span><i class="icon-pinterest"></i> Pinterest</span></li>
                            </div>
                          </ul>
                        </div>
                      </div>
                    </div>

                    <div class="bottom_left">
                        <a href="/home/descarca/{mp3}" class="btn btn-small btn-primary" onClick="this.disabled=true">Descarca</a> <span class="label label-primary">{descarcari} descarcari</span>
                        <a href="#" class="label label-warning">{comentarii} comentarii</a><br>
                    </div>
                    <div class="bottom_right">
                      <a href="#">{autor}</a> <span>|</span> <a href="#" class="show_comment_link">Adauga comentariu</a> <span>|</span> {data}
                    </div> 
                  </div>
                </div>
              </div>
            </div>
          </div>
        {/bancuri}

问题在于,此时它会显示每个数据库行的广告(简单图像atm)。

1 个答案:

答案 0 :(得分:0)

无论您发布的代码如何,简而言之,您只需添加一个计数器并使用模数。

类似

$arr = range('a','z');
$n = 0;
foreach ($arr  as $item)  
{  
    if($n % 4 ==0) // 4 equals to the interval between ads
        echo 'print banner';  
$n++ ;
} 

这将打印一个广告,或任何你想要的,每4行,根据你认为合适的数字玩,如果你不想在第一行显示广告,则添加零检查。