Advice on handling multidimensional array with Codeigniter or possible other solution? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Advice on handling multidimensional array with Codeigniter or possible other solution? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I have a list of users and checkboxes, I need to get the id’s of each check box:checked as well as a subject field and a message field and pass to my controller. The only way I could think to do this was to grab each id and duplicate the subject and message fields and pass to a multidimensional array to my controller. My array looks like this:

[{id: "46", subject: "sample subject1", message: "test message1"}{id: "46", subject: "sample subject1", message: "test message1"}]

This seems really redundant as I only need the list of user ids and 1 copy of the message and subject fields in the array but not sure if there is a better way to do this or not. Is it possible to send two different arrays via ajax to my codeigniter controller? If so how do I access them in the controller?

Here is an example of my code to collect ids and create the array. If this is the best method I am still not sure how to handle a multidimensional array in my controller. Normally I would access the array contents using $this->input->post(‘key’) but, that wont work: I think I would need to loop through the array first in the controller but not sure how to do.

Here is my code:

$(document).on('click', '.send_btn', function(e) { 

//get ids of selected users
var subject = 'test subject';
var message = 'test message';
var data = [];

$(':checkbox[name="people_checked[]"]:checked').each (function () {
  data.push({
    id: this.id,
    subject: subject,
    message: message
  })
  
});

  $.ajax({
      url: "/message/send_message",
      method: "POST",
      success: function (data) {
        console.log('Success');
        
     }
 }) 

});

Any help advice is much appreciated.

Answer

If you want to DRY up the array, convert it to an object which has the subject and message properties in the root. Then you can provide the ids property as an array. It would look something like this:

$(document).on('click', '.send_btn', function(e) {
  var data = {
    subject: 'test subject',
    message: 'test message',
    ids: $(':checkbox[name="people_checked[]"]:checked').map((i, el) => el.id).get()
  };

  $.ajax({
    url: "/message/send_message",
    method: "POST",
    data: data, // note this was added, you didn't send a body in the original
    success: function(data) {
      console.log('Success');
    }
  })
});

The output would be:

{
  subject: 'test subject',
  message: 'test message',
  ids: [ 123, 987 ]
}

You would obviously need to amend your /message/send_message endpoint to work with a request body in that format.

We are here to answer your question about Advice on handling multidimensional array with Codeigniter or possible other solution? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji