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;
}









share|improve this question









New contributor




Arun Sharma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    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;
    }









    share|improve this question









    New contributor




    Arun Sharma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      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;
      }









      share|improve this question









      New contributor




      Arun Sharma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      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






      share|improve this question









      New contributor




      Arun Sharma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Arun Sharma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 30 mins ago









      200_success

      127k15148412




      127k15148412






      New contributor




      Arun Sharma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 37 mins ago









      Arun Sharma

      61




      61




      New contributor




      Arun Sharma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Arun Sharma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Arun Sharma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.



























          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          Arun Sharma is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          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






























          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.










          draft saved

          draft discarded


















          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Costa Masnaga

          Fotorealismo

          Sidney Franklin