skip to Main Content

Currently I can view the list of my database and putting them as Sub-menus of my site, in the database they are ordered as smallest to highest (by ID)

this is the code I use to get the data on database:

   $database_service = get_data_of('service', 'id,slug,image,title');

the ‘service’ in the code is the specific table I get data.

then creating a foreach loop to get the data in order, but I want it to show in a HIGHEST TO LOWEST order based on ID number.

so here’s a full code on how I get the data:

            <ul class="classs class1 class2">
                <?php
   $database_service = get_data_of('service', 'id,slug,image,title');
                foreach ($database_service as $item) {
                ?>
                <li>
                    <a class="dropdown-item class lcass2" href="<?php echo $this->config->item('url') . 'service/' . $item->slug; ?>">
                        <img src="<?php echo $this->config->item('img').$item->image; ?>"
                                 class="clas1 class2" style="max-width:25px;"> 
                        <?php echo $item->title; ?>
                    </a>
                </li>
                <?php } ?>
            </ul>

how to do this?

2

Answers


  1. Use

    $this->db->order_by('ID', 'desc');
    

    in your model, before below line

    $this->db->get();

    to sort data in descending order.

    You can refer below link to know more about ordering in Codeigniter.

    Order By Clause in Codeigniter

    Login or Signup to reply.
  2. Use

    $this->db->select('id,slug,image,title')
    ->from('service')
    ->order_by('id','DESC')
    ->get();
    

    OR you can also use this

    $this->db->select('id,slug,image,title')
    ->order_by('id','DESC')
    ->get('service');
    

    DESC meaning descending order if want to show the result in ascending order just use

    $this->db->order_by('id','asc');
    

    Check out the documentation

    Check out the Tutorial

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search