2

I'm starting with DataTables (https://datatables.net/)

And I'm trying to reload my table with json source.

From PHP code: I want to create an json array:

$fh = fopen("pv1_consultafolioTorre".".json", 'w');
while($row = mssql_fetch_array($query_result))
  {
     $a = array(
       array(
         'folio' => $row['folio'],
         'MATNR' => $row['MATNR'],
         'sucursalSolcitante' => $sucursalSolcitante,
         'sucursalResponsable' => $sucursalResponsable,
         'fechaSolicitud' => $row['fechaSolicitud'],
         'cantidadSolicitada' => $row['cantidadSolicitada'],
         'MATNR' => $row['MATNR'],
         'fechaConfirmacion' => $row['fechaConfirmacion'],
         'cantidadConfirmada' => $row['cantidadConfirmada'],
         'tiempoConfirmacion' => $result,
         'estatus' => 'Pendiente'
       ));

       $jsonencoded = json_encode($a, 128);
       print json_encode($a);
       fwrite($fh, $jsonencoded);
  }

  fclose($fh);

and I obtain this:

[
    {
        "folio": "99001091347",
        "MATNR": "033999900         ",
        "sucursalSolcitante": " CDG1 MATRIZ",
        "sucursalResponsable": " SC13 TIJUANA CENTRO",
        "fechaSolicitud": "2015-05-23 09:13",
        "cantidadSolicitada": 1,
        "fechaConfirmacion": "2015-05-23 09:14:47",
        "cantidadConfirmada": 1,
        "tiempoConfirmacion": 4,
        "estatus": "Aprobado"
    }
]

But I DataTables need the next structure:

{
    "data": [
        [
            "99001091347",
            "033999900         ",
            " CDG1 MATRIZ",
            " SC13 TIJUANA CENTRO",
            "2015-05-23 09:13",
            "1",
            "2015-05-23 09:14:47",
            "1",
            "4",
            "Aprobado"
        ]
    ]
}

How can I fix this?

4 Answers 4

1

If you need to keep the associative array for writing it to your .json-file and echo the data for DataTables, then you can fetch the array values from your data array and create a new one for the JSON output:

$json_data = array();

while($row = mssql_fetch_array($query_result))
{
    $data = array(
        'folio' => $row['folio'],
        'MATNR' => $row['MATNR'],
        'sucursalSolcitante' => $sucursalSolcitante,
        'sucursalResponsable' => $sucursalResponsable,
        'fechaSolicitud' => $row['fechaSolicitud'],
        'cantidadSolicitada' => $row['cantidadSolicitada'],
        'MATNR' => $row['MATNR'],
        'fechaConfirmacion' => $row['fechaConfirmacion'],
        'cantidadConfirmada' => $row['cantidadConfirmada'],
        'tiempoConfirmacion' => $result,
        'estatus' => 'Pendiente'
    );

    // write the array to a file if needed

    $json_data[] = array_values($data); // Transforming for DataTables
}

// Build the final DataTables array
$data_tables_array = array(
    'data' => $json_data
);

$jsonencoded = json_encode($data_tables_array);
echo $jsonencoded;
Sign up to request clarification or add additional context in comments.

Comments

0

try this code

$output['data'] = [];

$fh = fopen("pv1_consultafolioTorre".".json", 'w');
while($row = mssql_fetch_array($query_result))
{
  $output['data'][] = array(
                    $row['folio'],
                    $row['MATNR'],
                    $sucursalSolcitante,
                    $sucursalResponsable,
                    $row['fechaSolicitud'],
                    $row['cantidadSolicitada'],
                    $row['MATNR'],
                    $row['fechaConfirmacion'],
                    $row['cantidadConfirmada'],
                    $result,
                    'Pendiente'
                );
}


print $jsonencoded = json_encode($output, 128);
fwrite($fh, $jsonencoded);

6 Comments

enough $jsonencoded = json_encode(array('data' =>array($a)), 128);
@splash58: Then you have the keys in the array. I do not know it that works with DataTables.
@Richard Reiber here is the output : {"data":[[{"folio":"99001091347"... Whatch there eval.in/369125
@splash58: In this case, if an object was returned must be in an array.
I see that output is that autor of the question wanted, isn't it?
|
0

Use this instead:

$fh = fopen("pv1_consultafolioTorre".".json", 'w');
$arr = Array();
while($row = mssql_fetch_array($query_result))
  {
     array_push($arr, array(
         $row['folio'],
         $row['MATNR'],
         $sucursalSolcitante,
         $sucursalResponsable,
         $row['fechaSolicitud'],
         $row['cantidadSolicitada'],
         $row['MATNR'],
         $row['fechaConfirmacion'],
         $row['cantidadConfirmada'],
         $result,
         'Pendiente'
       ));
  }

  print $jsonencoded = json_encode(Array("data" => $arr), 128);
  fwrite($fh, $jsonencoded);

  fclose($fh);

Comments

0

Please try below code. I have inserted $data = array('data' => $a); and changed $row[] in array as well. You will get desired output.

$fh = fopen("pv1_consultafolioTorre".".json", 'w');
        while($row = mssql_fetch_array($query_result))
          {
           $a = array(
                   array(
            $row['folio'],
            $row['MATNR'],
            $sucursalSolcitante,
            $sucursalResponsable,
            $row['fechaSolicitud'],
            $row['cantidadSolicitada'],
            $row['MATNR'],
            $row['fechaConfirmacion'],
            $row['cantidadConfirmada'],
            $result,
            'Pendiente'
           )
        );

    $data = array('data' => $a); // added by me.
    $jsonencoded = json_encode($data, 128);
    print json_encode($jsonencoded);
    fwrite($fh, $jsonencoded);

      }

      fclose($fh);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.