Get chat message data from table, grouped by date
up vote
1
down vote
favorite
id sender_id receiver_id msg date_added
1 2 5 hii 2018-10-24 16:42:41
2 5 2 hello 2018-10-25 16:42:41
i want to extract data as:
[2018-10-24]=>[{
sender_id=>2,
receiver_id=>5,
msg=>hii}],
[2018-10-25]=>[{
sender_id=>5,
receiver_id=>2,
msg=>hello
}]
i have done this by first querying for the date first and applying group by to dates. then looping through dates and finding the data for those dates. but i think use of loops for that much of dates and that much of user will make the app slow in the coming future
The code i have used is:
//the dates is used to get the dates from table using group by
$dates = $this->get_date_list($checkArr);
////then i loop through each date to get the corresponding data
foreach($dates as $key=>$value){
$msg = $this->get_msg_by_date($value,$checkArr);
$data[$value] = $msg;
}
//here is the code to get the date from the date column
public function get_date_list($checkArr){
$sql = "Select date(date_added) as new_date from tb_chats where (sender_id = $checkArr[sender_id] AND receiver_id = $checkArr[receiver_id]) OR (sender_id = $checkArr[receiver_id] AND receiver_id = $checkArr[sender_id]) GROUP BY new_date";
$query = $this->db->query($sql);
$data = $query->result_array();
return array_column($data,'new_date');
}
//here is the code to get the data for the looped dates
public function get_msg_by_date($date,$checkArr){
$sql = "Select * from tb_chats where (sender_id = $checkArr[sender_id] AND receiver_id = $checkArr[receiver_id] AND date_added LIKE '$date%') OR (sender_id = $checkArr[receiver_id] AND receiver_id = $checkArr[sender_id] AND date_added LIKE '$date%')";
$query = $this->db->query($sql);
$data = $query->result_array();
return $data;
}
php sql mysql codeigniter
New contributor
add a comment |
up vote
1
down vote
favorite
id sender_id receiver_id msg date_added
1 2 5 hii 2018-10-24 16:42:41
2 5 2 hello 2018-10-25 16:42:41
i want to extract data as:
[2018-10-24]=>[{
sender_id=>2,
receiver_id=>5,
msg=>hii}],
[2018-10-25]=>[{
sender_id=>5,
receiver_id=>2,
msg=>hello
}]
i have done this by first querying for the date first and applying group by to dates. then looping through dates and finding the data for those dates. but i think use of loops for that much of dates and that much of user will make the app slow in the coming future
The code i have used is:
//the dates is used to get the dates from table using group by
$dates = $this->get_date_list($checkArr);
////then i loop through each date to get the corresponding data
foreach($dates as $key=>$value){
$msg = $this->get_msg_by_date($value,$checkArr);
$data[$value] = $msg;
}
//here is the code to get the date from the date column
public function get_date_list($checkArr){
$sql = "Select date(date_added) as new_date from tb_chats where (sender_id = $checkArr[sender_id] AND receiver_id = $checkArr[receiver_id]) OR (sender_id = $checkArr[receiver_id] AND receiver_id = $checkArr[sender_id]) GROUP BY new_date";
$query = $this->db->query($sql);
$data = $query->result_array();
return array_column($data,'new_date');
}
//here is the code to get the data for the looped dates
public function get_msg_by_date($date,$checkArr){
$sql = "Select * from tb_chats where (sender_id = $checkArr[sender_id] AND receiver_id = $checkArr[receiver_id] AND date_added LIKE '$date%') OR (sender_id = $checkArr[receiver_id] AND receiver_id = $checkArr[sender_id] AND date_added LIKE '$date%')";
$query = $this->db->query($sql);
$data = $query->result_array();
return $data;
}
php sql mysql codeigniter
New contributor
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
id sender_id receiver_id msg date_added
1 2 5 hii 2018-10-24 16:42:41
2 5 2 hello 2018-10-25 16:42:41
i want to extract data as:
[2018-10-24]=>[{
sender_id=>2,
receiver_id=>5,
msg=>hii}],
[2018-10-25]=>[{
sender_id=>5,
receiver_id=>2,
msg=>hello
}]
i have done this by first querying for the date first and applying group by to dates. then looping through dates and finding the data for those dates. but i think use of loops for that much of dates and that much of user will make the app slow in the coming future
The code i have used is:
//the dates is used to get the dates from table using group by
$dates = $this->get_date_list($checkArr);
////then i loop through each date to get the corresponding data
foreach($dates as $key=>$value){
$msg = $this->get_msg_by_date($value,$checkArr);
$data[$value] = $msg;
}
//here is the code to get the date from the date column
public function get_date_list($checkArr){
$sql = "Select date(date_added) as new_date from tb_chats where (sender_id = $checkArr[sender_id] AND receiver_id = $checkArr[receiver_id]) OR (sender_id = $checkArr[receiver_id] AND receiver_id = $checkArr[sender_id]) GROUP BY new_date";
$query = $this->db->query($sql);
$data = $query->result_array();
return array_column($data,'new_date');
}
//here is the code to get the data for the looped dates
public function get_msg_by_date($date,$checkArr){
$sql = "Select * from tb_chats where (sender_id = $checkArr[sender_id] AND receiver_id = $checkArr[receiver_id] AND date_added LIKE '$date%') OR (sender_id = $checkArr[receiver_id] AND receiver_id = $checkArr[sender_id] AND date_added LIKE '$date%')";
$query = $this->db->query($sql);
$data = $query->result_array();
return $data;
}
php sql mysql codeigniter
New contributor
id sender_id receiver_id msg date_added
1 2 5 hii 2018-10-24 16:42:41
2 5 2 hello 2018-10-25 16:42:41
i want to extract data as:
[2018-10-24]=>[{
sender_id=>2,
receiver_id=>5,
msg=>hii}],
[2018-10-25]=>[{
sender_id=>5,
receiver_id=>2,
msg=>hello
}]
i have done this by first querying for the date first and applying group by to dates. then looping through dates and finding the data for those dates. but i think use of loops for that much of dates and that much of user will make the app slow in the coming future
The code i have used is:
//the dates is used to get the dates from table using group by
$dates = $this->get_date_list($checkArr);
////then i loop through each date to get the corresponding data
foreach($dates as $key=>$value){
$msg = $this->get_msg_by_date($value,$checkArr);
$data[$value] = $msg;
}
//here is the code to get the date from the date column
public function get_date_list($checkArr){
$sql = "Select date(date_added) as new_date from tb_chats where (sender_id = $checkArr[sender_id] AND receiver_id = $checkArr[receiver_id]) OR (sender_id = $checkArr[receiver_id] AND receiver_id = $checkArr[sender_id]) GROUP BY new_date";
$query = $this->db->query($sql);
$data = $query->result_array();
return array_column($data,'new_date');
}
//here is the code to get the data for the looped dates
public function get_msg_by_date($date,$checkArr){
$sql = "Select * from tb_chats where (sender_id = $checkArr[sender_id] AND receiver_id = $checkArr[receiver_id] AND date_added LIKE '$date%') OR (sender_id = $checkArr[receiver_id] AND receiver_id = $checkArr[sender_id] AND date_added LIKE '$date%')";
$query = $this->db->query($sql);
$data = $query->result_array();
return $data;
}
php sql mysql codeigniter
php sql mysql codeigniter
New contributor
New contributor
edited 30 mins ago
200_success
127k15148412
127k15148412
New contributor
asked 37 mins ago
Arun Sharma
61
61
New contributor
New contributor
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Arun Sharma is a new contributor. Be nice, and check out our Code of Conduct.
Arun Sharma is a new contributor. Be nice, and check out our Code of Conduct.
Arun Sharma is a new contributor. Be nice, and check out our Code of Conduct.
Arun Sharma is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208984%2fget-chat-message-data-from-table-grouped-by-date%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown