Ren
Ren

Reputation: 775

Store data from php json into javascript dataset

I have a php codes that fetch the data from ms access and store it to $jsonStr somthing like:

$pivot_dataset = array();
   while(odbc_fetch_row($rs)) {
   // Push this data onto the end of the array
$pivot_dataset[] = array(
    'id' => odbc_result($rs,"id"),
    'year' => odbc_result($rs,"year"),
    'month' => odbc_result($rs,"month"),
    'empName' => odbc_result($rs,"empName"),
    'empPose' => odbc_result($rs,"empPos"),
    'numMc' => odbc_result($rs,"numMc"),
    'numLeave' => odbc_result($rs,"numLeave")
);
}

 odbc_close($conn);
 $jsonStr = json_encode($pivot_dataset); //[{"id": 1, "year": 2014 , "month": "JAN" ... }, { ... }];

But now i want to store the $jsonStr in javascript dataset. SO i do something like this:

var pivot_dataset = <?php 
$conn = odbc_connect('pivot_test','','') or die ("Error in connection");
$sql = "select * from empDetails"; 
$rs = odbc_exec($conn,$sql);
if (!$rs)
{
  exit ("Error in Sql");
}
$pivot_dataset = array();
while(odbc_fetch_row($rs)) {
// Push this data onto the end of the array
$pivot_dataset[] = array(
    'id' => odbc_result($rs,"id"),
    'year' => odbc_result($rs,"year"),
    'month' => odbc_result($rs,"month"),
    'empName' => odbc_result($rs,"empName"),
    'empPose' => odbc_result($rs,"empPos"),
    'numMc' => odbc_result($rs,"numMc"),
    'numLeave' => odbc_result($rs,"numLeave")
);
}
odbc_close($conn);
$jsonStr = json_encode($pivot_dataset);
echo $jsonStr;
?>;

But its not working,i want the javascript store the data set something like this:

var pivot_dataset = [{"id": 1, "year": 2014 , "month": "JAN", "empName": "David", "empPos": "engineer","numMc": 1, "numLeave": 2},];

Someone can please correct? I'm new to this. TQ

Upvotes: 0

Views: 606

Answers (1)

jogesh_pi
jogesh_pi

Reputation: 9782

just do like

var pivot_dataset = <?php echo $jsonStr;  ?>

not like you have done the whole logic:

var pivot_dataset = <?php 
$conn = odbc_connect('pivot_test','','') or die ("Error in connection");
$sql = "select * from empDetails"; 
$rs = odbc_exec($conn,$sql);
if (!$rs)
...........

First do the PHP Logic then store the data into a variable that you have done in $jsonStr; then next step is

<?php 
 $conn = odbc_connect('pivot_test','','') or die ("Error in connection");
 $sql = "select * from empDetails"; 
 $rs = odbc_exec($conn,$sql);
 if (!$rs)
    {
      exit ("Error in Sql");
    }
 $pivot_dataset = array();
 while(odbc_fetch_row($rs)) {
    // Push this data onto the end of the array
   $pivot_dataset[] = array(
    'id' => odbc_result($rs,"id"),
    'year' => odbc_result($rs,"year"),
    'month' => odbc_result($rs,"month"),
    'empName' => odbc_result($rs,"empName"),
    'empPose' => odbc_result($rs,"empPos"),
    'numMc' => odbc_result($rs,"numMc"),
    'numLeave' => odbc_result($rs,"numLeave")
  );
 }
 odbc_close($conn);
$jsonStr = json_encode($pivot_dataset);
?>

<script>
var pivot_dataset = <?php echo $jsonStr;  ?>
</script>

that it.

Upvotes: 2

Related Questions