Group by id from the substring queries on CodeIgniter

Question!

I have data from the database, and get the two characters from the id, I do not understand how to calculate data from the substring with the same id?

Example:

id = AA001, AA002, AA003, AB005, AB006, AC009, AC010, AC012, AC015

Result substring = AA, AA, AA, AB, AB, AC, AC, AC, AC,

I expected = AA:3, AB:2, AC:4

This my controllers

function mySubstr() { 
        $data=array('section'   => $this->mSubstr->get_substr(),
                    'isi'       =>'home/substr');
        $this->load->view('layout/wrapper', $data); 
 }

Models

function get_substr() {
    $query = $this->db
            ->select('LEFT(id, 2) as id', FALSE)
            ->from('tbSubstr')
            ->group_by('id')
            ->get();
    return $query->result();   
}

Views

<?php
    foreach ($section as $row) {
        echo json_encode($row->id); 
    }
?>
By : dwiyanto


Answers

Try this will may help you,

function get_substr() {
   $query = $this->db
        ->select('LEFT(id, 2) as sub_id , COUNT(*)', FALSE)
        ->from('tbSubstr')
        ->group_by('sub_id')
        ->get();
   return $query->result();   
}       
By : kc1994


You can debug your mongo queries in multiple ways.

Start with your index usage using below command :
db.images.aggregate( [ { $indexStats: { } } ] )

If this don't give you any useful information, then check the execution plan of the slow queries using : db.setProfilingLevel(2)
db.system.profile.find().pretty()

db.system.profile will give you complete profile of your queries.



C++11 solution:

#include <type_traits>

template<typename From, typename To>
To map(From e) {
    return static_cast<To>(
        static_cast<typename std::underlying_type<To>::type>(
        static_cast<typename std::underlying_type<From>::type>(e)));
}

This casting cascade is very explicit and supports enum classes.

For older C++ versions and for enums without class, static_cast<Enum2>(e) would suffice.

Edit:

With template specialization, you can use map without specifying any types explicitly:

enum class Enum1: int {A, B, C, D};
enum class Enum2: char {A1, B1, C1, D1};

template<typename T>
struct target_enum {
};

template<>
struct target_enum<Enum1> {
    typedef Enum2 type;
};

template<typename From>
typename target_enum<From>::type map(From e) {
    typedef typename target_enum<From>::type To;
    return static_cast<To>(
        static_cast<typename std::underlying_type<To>::type>(
        static_cast<typename std::underlying_type<From>::type>(e)));
}

You can then call map(Enum1::A). Also works with simple, non-class enums.

By : flyx


This video can help you solving your question :)
By: admin