Creating Json File with PHP. Taking out last comma

Asked

Viewed 179 times

1

I am creating a Json file, but I need to remove the last comma from the pad referring to the last record.

This json will be used in Datatable

inserir a descrição da imagem aqui

{
  "data": [
    <?php foreach ($rs as $row): ?>
      [
        "<?= $row['ID_Receita']; ?>",
        "<?= $row['nome']; ?>",
        "<?= $row['valor']; ?>",
        "<?= $row['dataVenci']; ?>",
        "<?= $row['formaEntrada']; ?>"
      ],
    <?php endforeach; ?>
  ]
}

How can I do that?

  • @Angelosoares as would be the correct way to assemble this json?

  • The correct way is to mount an array in PHP and pass one json_encode in it. What you are doing can go wrong in several different ways...

1 answer

3


This looks like a JS generated by PHP, right? To boot Datatables, if I understand correctly.

The simplest and correct is to use the function json_encode PHP to generate a valid JSON from an array. So:

<?php
// monte essa array a partir do banco de dados
$dados = [
    ["1", "Tiago", "300.00", "etc"],
    // demais linhas
];
?>

<script>
// seu código js
// ...
{
    "data": <?php echo json_encode($dados); ?>
}
// ...
</script>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.