how do I use php to separate the information into a json_encode variable?

Asked

Viewed 168 times

0

I’m doing a query inside another in php until beauty works but the result of the query inside only returns me a line that would be the last line, plus the query had to return 3 lines so I decided to put the query result in an array and made it into json using php’s json_encode function, now I need to separate this information into strings someone knows how I can do it?

follows the code excerpt:

while (($row = oci_fetch_array($stid)) != false) {

        if($row['LOTE_SERIAL'] == ""){
            $buscaTotal = oci_parse($conn,"select di.docit_amt TOTAL, docit_qt QTD, di.docit_lot LOTE from DOCIT di, DOCHD dc where di.docit_doc_prc_id = dc.dochd_doc_prc_id and dc.dochd_doc_id in ('228') and di.docit_cd in ('BRR010C030')")or die("erro no select buscaTotal");


                oci_execute($buscaTotal);

                $total_prod = array();
                $qtd = array();
                $lote = array();
                while (($val = oci_fetch_array($buscaTotal)) != false){
                    $total_prod[] = $val['TOTAL'];
                    $qtd[] = $val['QTD'];
                    $lote[] = $val['LOTE'];


                }



                    $total = json_encode($total_prod)."<br>";
                    $qtd = json_encode($qtd)."<br>";
                    $lote = json_encode($lote)."<br>";




            echo $total;

        }else{
            $total = $row['TOTAL_PRODUTO'];
            $qtd = $row['QTD_ENTRADA'];
            $lote = $row['LOTE_SERIAL'];
        }

        echo "<tr class='filtro'>";
            echo "<td>".$row['CESV_ENTRADA']."</td>";
            echo "<td>".$row['MOTIVO_ACESSO']."</td>";
            echo "<td>".$row['CLIENTE']."</td>";
            echo "<td>".$row['PEDIDO']."</td>";
            echo "<td>".$row['DATA_ENTRADA']."</td>";
            echo "<td>".$row['DATA_SAIDA']."</td>";
            echo "<td>".$row['NF_ENTRADA']."</td>";
            echo "<td>".$row['NF_SAIDA']."</td>";
            echo "<td>".$row['PRODUTO']."</td>";
            echo "<td>".$row['UNIDADE']."</td>";
            echo "<td>".$lote."</td>";
            echo "<td>".$row['LOTE_COMP']."</td>";
            echo "<td>".$row['UZS_ENTRADA']."</td>";
            echo "<td>".$row['UZS_SAIDA']."</td>";
            echo "<td>".$qtd."</td>";
            echo "<td>".$total."</td>";
            echo "<td>".$row['PESO']."</td>";
        echo "</tr>";
    }

this code returns this: ["29070","3960","63000"]

and I need you to come back like this 29070 3960 63000

Does anyone have any idea how I can do this?

  • Edit your question, take the image and place the real code. Greetings.

  • Your echo $total, printa: ["29070","3960","63000"]? Have you tried using, json_encode($json, JSON_PRETTY_PRINT)

  • I tried it didn’t work

  • Got it. You’ve set up an array $total_prod[] = $val['TOTAL']; Why are you running a json_encode($total_prod) in it, if you at the end just want to take a print? Travels with foreach, you will have the result you are looking for.

  • is that you needed to do outside while the print

  • It would be like taking duplicate values from foreach, for for I’m doing one while inside another while it’s repeating?

  • Did you see my answer to your question? It’s just down. Did you try to apply it? If so, make your comments on the answer below. Greetings

Show 2 more comments

1 answer

0

You have assembled an array $total_prod[] = $val['TOTAL']; I just don’t know what your goal is json_encode($total_prod) in it, if you at the end just want to give a print? Travels with foreach, the builder foreach provides an easy way to iterate over arrays. This will give you the result you are looking for.

...
while (($val = oci_fetch_array($buscaTotal)) != false){
    $total_prod[] = $val['TOTAL'];
    $qtd[] = $val['QTD'];
    $lote[] = $val['LOTE'];
}


foreach($total_prod as $total) {
    echo $total . "\n";
}
...
  • I used your code however I am still having problem, is showing duplicate values, I would like to bring the unique value but only once, for you to understand my code I am making two results of two query and I need to show the two results together in the html table, until here get more is bringing duplicate think it is because of the first while

  • There is the first while and the second while. The foreach, it’s out of that second while, As in the code of my answer, right? If so. So when you run a print_r($total_prod), what does it print? I remember that you put an img instead of pure code. In the picture, your code was different. Look at your image here with the different code: https://i.stack.Imgur.com/sL7sH.png Anyway, show me the output of the print_r($total_prod)

  • Already able to do what I wanted I made a group Concat then broke by the delimiter and returned by for the separate result more thank you for the help

Browser other questions tagged

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