Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9088413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:54:14+00:00 2026-06-16T21:54:14+00:00

I have a somewhat-social-network logging project which let users input any activity they have

  • 0

I have a somewhat-social-network logging project which let users input any activity they have done the whole day.

On my db I have a ‘logs’ table which have the following fields:

| log_id | log_content | log_date | log_author |

My problem is, how can I do a query which would fetch the first n days then display its result in a grid view like this:

| Monday, December 7, 2013
| log_content, log_author, 6 AM | log_content, log_author, 8:30 AM | ...|

| Tuesday, December 8, 2013
| log_content, log_author, 7:10 AM | log_content, log_author, 12:00 NN | log...|

| Wednesday, December 9, 2013
| log_content, log_author, 6:30 PM |

| Thursday, December 10, 2013
| log_content, log_author, 9:53 AM | log_content, log_author, 4:02 PM | ...|

| Friday, December 11, 2013
| log_content, log_author, 1:34 PM |

I know I can’t do a query for the whole ‘logs’ table since it will lead to bottleneck when the logs grow. I was thinking of doing a query in bits, and when the user scrolls down, another background query would run for the next set of data to display(using limits and offsets).

My query as of the moment looks like this:

function fetch_logs($group_id, $limit, $offset){
        $this->db->select('l.log_id, l.log_date, l.log_content, u.username');
        $this->db->from('logs AS l');
        $this->db->join('log_group_ref AS r', 'l.log_id = r.log_id');
        $this->db->join('users AS u', 'l.log_author = u.user_id');
        $this->db->where('r.group_id', $group_id);
        $this->db->limit($limit, $offset);
        return $this->db->get()->result_array();
    }

But I have no idea how to group the queried data by days and lay them into grid.

I’m using Twitter’s Bootstrap with grid so I can lay down each log into a grid nicely then just add scroll bars if there are more logs.

Update:

So I updated my code based on raheel shan’s second method but I can’t seem to get the data that I wanted until I added:

$this->db->group_by(l.log_date);

basing on Exander’s code which fetch the correct set of data although not formatted same as raheel shan’s.

So out of those two, here’s my updated query:

$select =   array(
                            "DATE_FORMAT(l.log_date,'%a , %b %d , %Y') AS LogDate",
                            "GROUP_CONCAT(l.log_id) AS LogIDs",
                            "GROUP_CONCAT(l.log_content) AS LogContents",
                            "GROUP_CONCAT(u.username) AS LogAuthors",
                            "GROUP_CONCAT(DATE_FORMAT(l.log_date,'%h:%i %p')) AS LogTimes"
                        );
            $this->db->select($select);
            $this->db->from('logs AS l');
            $this->db->join('log_group_ref AS r', 'l.log_id = r.log_id');
            $this->db->join('users AS u', 'l.log_author = u.user_id');
            $this->db->where('r.group_id', $group_id);
            $this->db->group_by('l.log_date');
            $this->db->limit($limit, $offset);
            return $this->db->get()->result_array();

Now, I’ll have to deal with exploding each LogContents, LogAuthors, LogTimes and group them back in array. I need to study raheel shan’s code again.

Update:
I finally got all the things I need to achieve this. I’ll mark raheel shan’s as accepted since I can only accept one. Though Exander’s is also correct plus lesser code which make it great.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T21:54:15+00:00Added an answer on June 16, 2026 at 9:54 pm

    Here is how you can do it.
    View Demo

    Here

    SELECT
        DATE_FORMAT(l.log_date,'%a , %b %d , %Y') as LogDate,
        DATE(l.log_date) as LDate,
        GROUP_CONCAT(r.data) as LogData
    FROM logs as l
    INNER JOIN  (
                SELECT 
                        logs.log_date,
                        GROUP_CONCAT(other) as data     
                FROM logs
                LEFT JOIN (
                            SELECT 
                                log_date,
                                CONCAT(log_content, ' | ', log_author , ' | ' , DATE_FORMAT(log_date,'%h:%i %p')) as other 
                            FROM logs
                            GROUP BY log_date
                ) as t ON t.log_date = logs.log_date
                GROUP BY log_date
    ) as r ON r.log_date = l.log_date
    GROUP BY LDate
    

    And in Codeigniter

    $query  ="  SELECT
                    DATE_FORMAT(l.log_date,'%a , %b %d , %Y') as LogDate,
                    DATE(l.log_date) as LDate,
                    GROUP_CONCAT(r.data) as LogData
                FROM logs as l
                INNER JOIN  (
                            SELECT 
                                    logs.log_date,
                                    GROUP_CONCAT(other) as data     
                            FROM logs
                            LEFT JOIN (
                                        SELECT 
                                            log_date,
                                            CONCAT(log_content, ' | ', log_author , ' | ' , DATE_FORMAT(log_date,'%h:%i %p')) as other 
                                        FROM logs
                                        GROUP BY log_date
                            ) as t ON t.log_date = logs.log_date
                            GROUP BY log_date
                ) as r ON r.log_date = l.log_date
                GROUP BY LDate";
    
    $result =   $this->db->query($query);
    return $result->result();
    

    Learning Points

    Left Joins
    Group_Concat
    Concat
    DATE_FORMAT

    EDITS :
    2nd Method

    if you want to do it with php it is easy. Here is how you can do it. First Model function

    function fetch_logs($group_id, $limit, $offset)
    {
            $select =   array(
                            "DATE_FORMAT(l.log_date,'%a , %b %d , %Y') as LogDate",
                            "GROUP_CONCAT(l.log_id) as LogIDs",
                            "GROUP_CONCAT(l.log_content) as LogContents",
                            "GROUP_CONCAT(l.log_author) as LogAuthors",
                            "GROUP_CONCAT(DATE_FORMAT(l.log_date,'%h:%i %p')) LogTimes"
                        );
            $this->db->select($select);
            $this->db->from('logs AS l');
            $this->db->join('log_group_ref AS r', 'l.log_id = r.log_id');
            $this->db->join('users AS u', 'l.log_author = u.user_id');
            $this->db->where('r.group_id', $group_id);
            $this->db->group_by(l.log_date);
            $this->db->limit($limit, $offset);
            return $this->db->get()->result_array();
    }   
    

    And Now in Controller function

    function getLogs()
    {
        $result =   $this->yourmodel->fetch_logs();
        $data   =   array();
        $i  =   0;
    
        foreach($result as $row)
        {
            $logids     =   explode(',',$row['LogIDs']);
            $logcontents    =   explode(',',$row['LogContents']);
            $logauthors =   explode(',',$row['LogAuthors']);
            $logtimes       =   explode(',',$row['LogTimes']);
    
            $counter    =   count($logids);
            $test   =   array();
    
                for($j=0;$j<$counter;$j++)
                {
                    $test[] =   $logcontents[$j] .','. $logauthors[$j] .','.$logtimes[$j];
                }
            $data[$i]['logdata']    =   implode('|',$test);
            $data[$i]['logdate']    =   $row['LogDate'];
            unset($test);
            $i++;
        }
    
        $viewdata['data']   =   $data;
        $this->load->view('my_view',$viewdata);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Setting up a project structure; I am thinking is it better to have somewhat
I have following somewhat complex sql query which has horrible performance, 'certainly' due to
Let's say I have a somewhat large (several millions of items, or so) list
I am working on a project called association rule discovery from social network data:
I have a somewhat complex query with multiple (nested) sub-queries, which I want to
I have a somewhat grouping system which all the elements between two divs are
I am making a project posting system that will have somewhat some markup, similar
I have an application where users have somewhat of a settings page, and some
Connecting to another computer via sockets (which I have somewhat succeeded at, yay me)
I'm putting together somewhat of a social website. It's not another social network, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.