0

这是我的模型

function get_one_news($limit = 2, $offset = null) {

            if ($limit) {

                $this->db->limit($limit, $offset);
            }

            $this->db->order_by('news.added_on', 'desc');

            return $this->db->get('news');


        }

在我的控制器中我已经分配了它

$data['news'] = $this->news_model->get_one_news();

在我看来,我已经实现了它

<div class="col-md-8 col-lg-8">
            <!-- artigo em destaque -->
            <?php if ($news->num_rows()):?>
            <?php foreach ($news->result() as $n):?>
            <?php if ($n->cat_id == 17):?>
            <div class="featured-article">
                <a href="#">
                <?php if ($n->image != ''): ?>
                    <img src="<?php echo base_url('uploads/'.$n->image);?>" alt="" class="thumb">
                <?php endif;?>
                </a>
                <div class="block-title">
                    <h4><?php echo $n->title = word_limiter($n->title, 7);?></h4>

                </div>
            </div>
            <!-- /.featured-article -->
            <?php endif;?>
            <?php endforeach;?>
            <?php endif;?>
        </div>

这个查询给了我结果,但是如果我输入限制 3,它给 2,如果我输入 2,它给 1,如果我输入 1,它不会给出任何结果,为什么?

4

1 回答 1

0

如果你回显 $news->num_rows() 会发生什么?我怀疑它可能与以下内容有关:

<?php if ($n->cat_id == 17):?>

您是否获得与限制相同的行数,但由于不是 cat_id == 17 而过滤掉了最新的行数?

于 2015-04-29T17:22:45.697 回答