mardi 30 juin 2015

useing php while loop json_encode

I am trying to use json_encode() in a while loop while getting database results. Here is my code:

 <?
    $sql_page = "select * from type";
    $rs_type=mysql_query($sql_page,$link);
    while ($row=mysql_fetch_array($rs_type)){
$jsonString =array( "n" => $row['tTitle'],
  "s" => array( array("n" => $row['tTitle1'],
  "s" => array( array("n" => $row['tTitle2'],
 "s" => array( array("n" => $row['tTitle3'],
 "s" => array( array("n" => $row['tTitle4'],
  ),
  ),
  ),
  ),
  ),
  ),
  ),
  ),
);


    $cart = json_encode( $jsonString );
    $file = '../application/js/tecair.json';
    file_put_contents($file, '['.$cart.']');
    ?>

The output following below is

[{"n":"1","s":[{"n":"1_1","s":[{"n":"1_2","s":[{"n":"1_3","s":[{"n":"1_4"}]}]}]}]}]

but i want it to be:

[ { "n":"1", "s":[ { "n":"1_1", "s":[{"n":"1_1_1"},{"n":"1_1_2"},{"n":"1_1_3"},{"n":"1_1_4"}] },{ "n":"1_2", "s":[{"n":"1_2_1"},{"n":"1_2_2"},{"n":"1_2_3"},{"n":"1_2_4"},{"n":"1_2_5"},{"n":"1_2_6"}] },{ "n":"1_3", "s":[ { "n":"1_3_1", "s":[ { "n":"1_3_1_1", "s":[{"n":"1_3_1_1_1"},{"n":"1_3_1_1_2"},{"n":"1_3_1_1_3"},{"n":"1_3_1_1_4"}] } ] } ] },{ "n":"1_4", "s":[{"n":"1_4_1"},{"n":"1_4_2"},{"n":"1_4_3"},{"n":"1_4_4"}] } ] },{ "n":"2" },{ "n":"2_1", "s":[{"n":"2_1_1"},{"n":"2_1_2"},{"n":"2_1_3"},{"n":"2_1_4"},{"n":"2_1_5"},{"n":"2_1_6"}] } ]

How do I use while loop to do it well? thanks!

Aucun commentaire:

Enregistrer un commentaire