How can I pass my data to the json format using the foreach ()?

How can I pass my data to the json format using the foreach ()?

cris19ncris19n Posts: 25Questions: 5Answers: 0

How can I pass my data to the json format using the foreach () loop to load it into the ajax option of the datatables?
and retrieve them with the data table data filter option:

something like that:

I just want to show the data from the db using server side,
but I have not managed to make it work for me, I would appreciate the help, some example.

as you can see I am using classes and methods to do the queries (MVC).

js datatables:

    $('#myTable').DataTable( {
        'processing': true,
        'serverSide': true,
        'ajax': {
            'url':'ajax/dtb.mostrar.dts.ajax.php',
            dataFilter: function(data){
               var json = jQuery.parseJSON( data );
                json.recordsTotal = json.total;
                json.recordsFiltered = json.total;
                json.data = json.list;

                console.log("datos devueltos de la bd: ",data);

                return JSON.stringify( json ); // return JSON string


            }
        }
    } );.

archivo ajax php; ajax/dtb.mostrar.dts.ajax.php

        <?php

         require_once "../modelos/showtables.modelo.php";
            require_once "../controladores/showtables.controlador.php";





                                   $item = null;
                                    $valor= null;

                                    $showTable = ControladorTables::ctrMostrarTables($item, $valor);
                                    $fila=-1;
                                    foreach ($showTable as $key => $value) {

                                        $arreglo["data"][]=$value["cod_materia_prima_mp"];
                    $arreglo["data"][]=$value["fecha_registro_mp"];
                    $arreglo["data"][]=$value["fk_persona_responsable_mp"];
                    $arreglo["data"][]=$value["lote_piku_mp"];
                    $arreglo["data"][]=$value["lote_materia_prima_mp"];
                    $arreglo["data"][]=$value["viaje_mp"];
                    $arreglo["data"][]=$value["fecha_ingreso_mp"];
                    $arreglo["data"][]=$value["granja_mp"];
                    $arreglo["data"][]=$value["galpon_mp"];
                    $arreglo["data"][]=$value["sexo_mp"];
                    $arreglo["data"][]=$value["linea_mp"];
                    $arreglo["data"][]=$value["edad_mp"];
                    $arreglo["data"][]=$value["cantidad_jaula_mp"];
                    $arreglo["data"][]=$value["cantidad_aves_por_jaula_mp"];
                    $arreglo["data"][]=$value["cantidad_aves_mp"];
                    $arreglo["data"][]=$value["peso_promedio_mp"];
                    $arreglo["data"][]=$value["placa_vehiculo_mp"];
                    $arreglo["data"][]=$value["conductor_vehiculo_mp"];
                    $arreglo["data"][]=$value["hora_incio_atrape_mp"];
                    $arreglo["data"][]=$value["hora_salida_granja_mp"];
                    $arreglo["data"][]=$value["hora_llegada_planta_mp"];
                    $arreglo["data"][]=$value["total_atrape_mp"];
                    $arreglo["data"][]=$value["observaciones_mp"];
                    $arreglo["data"][]=$value["numero_sello_seguridad_mp"];
                    $arreglo["data"][]=$value["mortalidad_porcentaje_mp"];
                    $arreglo["data"][]=$value["tipo_sacrificio_mp"];
                    $arreglo["data"][]=$value["estado_aves_apto_mp"];
                    $arreglo["data"][]=$value["veterinario_granja_mp"];
                    $arreglo["data"][]=$value["recibe_planta_mp"];
                    $arreglo["data"][]=$value["fecha_inspeccion_granja_mp"];
                    $arreglo["data"][]= '<td>

                                                        <div class="btn-group" role="group" aria-label="Third group">
                                                            <button class="btn btn-warning btnEditarMateriaP btnEditMateriaP  manito-clic" editMp="'.$value["cod_materia_prima_mp"].'" idUsuario="'.$usuarios2["cod_persona_responsable_pr"].'" User="'.$usuarios2["usuario_pr"].'" data-toggle="modal" data-target="#modalEditarMateriaPrima"><i class="fa fa-pencil"></i></button>
                                                        </div>
                                                        <div class="btn-group" role="group" aria-label="Third group">
                                                            <button class="btn btn-danger btnEliminarMateriaP manito-clic" deleteMp="'.$value["cod_materia_prima_mp"].'" lotepiku="'.$value["lote_piku_mp"].'" viajepiku="'.$value["viaje_mp"].'"><i class="fa fa-times"></i></button>
                                                        </div>
                                                    </td>';

                                            }



                                    }

        echo json_encode( $arreglo);


        ?>

Replies

  • allanallan Posts: 52,631Questions: 1Answers: 8,052 Site admin

    I might be wrong, but it doesn't appear you are using server-side processing there. But rather just loading the full data set from the server? With server-side processing, you'd need logic to handle the paging, sorting and filtering.

    How many rows are you looking at here? If less than 50'000, then I'd suggest just using client-side processing, reading your JSON data from the serer directly.

    Can you give me a sample of what the returned JSON from the server looks like?

    Allan

Sign In or Register to comment.