如何在codeigniter中以dscending顺序显示数据库中的数据?

时间:2017-05-12 16:24:38

标签: php database codeigniter phpmyadmin codeigniter-3

通常当我从我的数据库中获取数据时,我按照他们的id顺序排列。但是,我创建了一个名为'sales'的列,它也是一个整数 我希望我的数据按照销售按升序排列。这是我的代码:

我的模特

     public  function get_bestseller($id = FALSE) {
    if ($id === FALSE)
    {
        $query = $this->db->get('submit');
        return $query->result_array();
    }

    $query = $this->db->get_where('submit', array('id' => $id));
    $this->db->order_by('sales', 'DESC');
    return $query->result_array();
}

我的观点

    <div class="tab-pane fade" id="faq-cat-2">
            <div class="panel-group" id="accordion-cat-2">
                <?php foreach ($bestseller as $bestseller_item): ?>
                    <div class="col-sm-4 col-lg-4 col-md-4">
                        <div class="thumbnail">
                            <img class="lazy" src="<?php echo base_url() ?>Images/last.gif"
                                 data-original="<?php echo base_url() ?>uploads/<?php echo $bestseller_item['imgname']; ?>"
                                 style="width: 350px; height: 150px">
                            <div class="caption">
                                <?php if ($bestseller_item['Price'] == 0): ?>
                                    <h4 class="pull-right"><p class="text-success">Free</p></h4>
                                <?php else: ?>
                                    <h4 class="pull-right">$<?php echo $bestseller_item['Price']; ?></h4>
                                <?php endif; ?>
                                <h4>
                                    <a href="<?php echo site_url('submit/view/' . $bestseller_item['id']); ?>"
                                       target="_blank"><?php echo $bestseller_item['Title']; ?></a>
                                </h4>
                                <p><?php echo $bestseller_item['textarea']; ?></p>
                            </div>
                            <p class="text-right"><b
                                        class="author">By: <?php echo $bestseller_item['Author']; ?></b>
                            </p>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        </div>

我的控制器

   public function index()
   {
    $data['bestseller'] = $this->Submit_Database->get_bestseller();

    $this->load->view('templates/header');
    $this->load->view('pages/home', $data);
    $this->load->view('templates/footer');
}

1 个答案:

答案 0 :(得分:2)

$query = $this->db->order_by('sales', 'DESC')->get_where('submit', array('id' => $id));

您在查询已经运行后添加order by子句。