rjcode
rjcode

Reputation: 1349

Adding two PHP array and echo Json

I have below code for controller,

 public function user_ajaxtest()
 {
    $userlistingx = $this->model_usermanage->viewuserlisting();
    $userlistingy = array(
            array('select' => '<input type="checkbox" name="id[]" value="">')
            );

    $userlisting1['data'] = array_merge($userlistingx, $userlistingy);
    echo json_encode($userlisting1);
}

With this AJAX i am sending user list to DataTables. $userlistx giving me users array and i could able to see everything, But i would like to add checkbox for each row of datatable, For that i had created one more array $userlistingy, and then merge this two array,

With this, i am getting one more row instead of column in datatable,

See below ajax response which i am getting normally without merging array means, only echo json $userlistingx.

data: [{test_id: "8", user_id: "28", usernote: "1 image 1 attachment", department_id: "21",…},…] 0: {test_id: "9", user_id: "29", usernote: "1 image 1 attachment" 1: {test_id: "10", user_id: "30", usernote: "1 image 1 attachment"

But when i merget and send Json i get below,

data: [{test_id: "8", user_id: "28", usernote: "1 image 1 attachment", department_id: "21",…},…] 0: {test_id: "9", user_id: "29", usernote: "1 image 1 attachment" 1: {test_id: "10", user_id: "30", usernote: "1 image 1 attachment" 2: {select: "<input type="checkbox" name="id[]" value="">"}

In short question, How do i add select checkbox array to all my datarows?

Upvotes: 1

Views: 56

Answers (2)

Sougata Bose
Sougata Bose

Reputation: 31739

There is no need to pass the checkbox in the response. Add them to the table you are using.

Check this out

HTML

<table cellpadding="0" cellspacing="0" border="0" class="display" id="example" width="100%">
    <thead>
        <tr>
            <th>name</th>
            <th>option</th>
        </tr>
    </thead>
</table>

Script

$('#example').dataTable( {
    ajax: "yourpage.php",
    columns: [
        { data: "name" },
        {
            data:   "your_data",
            render: function ( data, type, row ) {
                return '<input type="checkbox" name="name_check" value="">';
            },
        }
    ],

} );

Upvotes: 0

Dinesh
Dinesh

Reputation: 4110

you can try this

 $userlistingx = $this->model_usermanage->viewuserlisting();
 for($i=0;$i<count($userlistingx);$i++)
  {
      $userlistingy[]='<input type="checkbox" name="id[]" value="">';
  }
$userlisting1['data'] = array_merge($userlistingx, $userlistingy);
echo json_encode($userlisting1);

Upvotes: 0

Related Questions