How to export all my data in to a json array

armasmike
armasmike used Ask the Experts™
on
How to export all my data in to a json array

Here is my code

<?php
ini_set('display_errors', TRUE);
error_reporting(E_ALL);


if(isset($_POST['submit']))
{


$UsrWorkOrder = $_POST['Work_Order'];


//db connect info
include 'conn.php';
//Where the query are stored 
include 'Query.php';




$result_Vis = odbc_exec($conn_Vis,$sql_Vis);  // Get Data From Result
    while($row2 = odbc_fetch_array($result_Vis))
    {
//Store the data in the new array

 

            $result_cor1db01_2 = odbc_exec($conn_cor1db01_2,$sql_cor1db01_2);  // Get Data From Result

            while($row3 = odbc_fetch_array($result_cor1db01_2))
            {







//This stores the data 
            	$Barcode = $row3["Work Order No"]."-". $row3["Serial No"];
       			$part = $row2["PART_X"];
        		$DESCR = $row2["DESCR_X"];
            	$Cable_Length = $row3["ValueInches"];
            	$MFG = $row3["Work Order No"]."-". $row3["Serial No"];
            	$Test_A = number_format($row3["Value1"],2);
            	$Test_B = number_format($row3["Value2"],2);



                    Echo "<br><br><img src=barcode.php?codetype=Code128B&size=80&text=".$row3["Work Order No"]."-". $row3["Serial No"] ." />";
                    Echo "<br>".$row3["Work Order No"]."-". $row3["Serial No"]."<br>";
                    Echo "Part Number: ".$part."<br>";
                    Echo "Description: " .$DESCR."<br>";
                    Echo "Mfg Lot Number: " .$row3["Work Order No"]."-". $row3["Serial No"]."<br>";
                    Echo "Cable Length: ". $row3["ValueInches"]."<br>";
                    Echo "Test Results: ".substr($row3["SS1"], -1)." " .number_format($row3["Value1"],2)."<br>";
                    Echo "Test Results: ".substr($row3["SS2"], -1)." " .number_format($row3["Value2"],2)."<br>";




            }

    }

    
odbc_close($conn_cor1db01_2);
odbc_close($conn_Vis);

}

?>

Open in new window


How can I put all the data it gets in to a json array. I have attached a file of the output data. What-i-want-it-to-be.xlsx

How can I put this in a json array ?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
I think I got it working with this

$posts = array();
$J_Barcode = $row3["Work Order No"]."-". $row3["Serial No"];
    $posts []= array (
        'Barcode ' => $J_Barcode );

Open in new window

Commented:
You use json_encode() to turn an array into JSON. If you want an array containing all the rows, you would first add a container to store your rows BEFORE your loop:
while($row3 = odbc_fetch_array($result_cor1db01_2))
{

Open in new window

to:
$rows = array(); // <-- Container
while($row3 = odbc_fetch_array($result_cor1db01_2))
{
$rows[] = $row3; // <-- Add row to container

Open in new window


Then after your loop:
            }
    echo json_encode($rows); // Convert your $rows to a JSON string and display it
    }
    
odbc_close($conn_cor1db01_2);
odbc_close($conn_Vis);
}
?>

Open in new window


This also presumes the inner loop is the one where you want the container.

Author

Commented:
That worked great !
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
Well that's only getting the info from 1 query I have two query's
I have a row3 and row2

Author

Commented:
this is all the info I need to go in the json array

$Barcode = $row3["Work Order No"]."-". $row3["Serial No"];
       			$part = $row2["PART_X"];
        		$DESCR = $row2["DESCR_X"];
            	$Cable_Length = $row3["ValueInches"];
            	$MFG = $row3["Work Order No"]."-". $row3["Serial No"];
            	$Test_A = number_format($row3["Value1"],2);
            	$Test_B = number_format($row3["Value2"],2);

Open in new window




$part = $row2["PART_X"];
$DESCR = $row2["DESCR_X"];

Open in new window



are the only 2 from other db server

Author

Commented:
it doesn't like it if I do

$json []= "barcode" => $Barcode = $row3["Work Order No"]."-". $row3["Serial No"]

Open in new window

Author

Commented:
I got it working

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial