0

I’ve a questionnaire that I analyze with php. The data are supposed to be displayed in a Java-based polar-graph and a table. You can see how it looks with static data here.

However, the graph doesn’t show when I use the data from my database. The table works fine. A few days ago I asked about how to integrate php-data to java via json_encode because I thought this might be the problem, but I learned (thanks again) that this is the correct approach.

My web-hoster says that it doesn’t support php in html, so I modified the index.html to an index.php. I have no idea what to modify to make it work. Database-connection works, php-values in the table are correct, the js-graph works fine with static data.

Here are my php-arrays:

$arr1 = array(axis => "Gesundheitszustand", value => $X1P);
$arr2 = array(axis => "BMI", value => $X3);
$arr3 = array(axis => "Stress", value => $X10P);
$arr4 = array(axis => "Körperliche Aktivität", value => $X4P);
$arr5 = array(axis => "Nahrung: Gemüse/Obst", value => $X8d);
$arr6 = array(axis => "Nahrung: Fisch", value => $X8f);
$arr7 = array(axis => "Nahrung: Fleisch", value => $X8h);
$arr8 = array(axis => "Geistige Gesundheit", value => $X9P);
$arr9 = array(axis => "Zufriedenheit", value => $X2P);
$arr10 = array(axis => "Rauchen", value => $X9a);

The values are numbers between 0 and 1.

That’s how I try to include the js-file in my php-file:

<html>
<head>
  <script src="d3js.js"></script>
    <script src="radarchart.js"></script>
<link rel="stylesheet" href="style.css">
</head>
<body>
<?php ?>
       <script type="text/javascript" src="script.js"></script>
<?php ?>

</body>

And that’s how I include my php-data to the js-file:

var d = [
          [
            <?php echo json_encode($arr1); ?>,
            <?php echo json_encode($arr2); ?>,
            <?php echo json_encode($arr3); ?>,
            <?php echo json_encode($arr4); ?>,
            <?php echo json_encode($arr5); ?>,
            <?php echo json_encode($arr6); ?>,
            <?php echo json_encode($arr7); ?>,
            <?php echo json_encode($arr8); ?>,
            <?php echo json_encode($arr9); ?>,
            <?php echo json_encode($arr10); ?>,         
                  ],[
            {axis:"Gesundheitszustand",value:0.63},
            {axis:"BMI",value:0.58},
            {axis:"Stress",value:0.67},
            {axis:"Körperliche Aktivität",value:0.33},
            {axis:"Nahrung: Gemüse/Obst",value:0.66},
            {axis:"Nahrung: Fisch",value:0.25},
            {axis:"Nahrung: Fleisch",value:0.50},
            {axis:"Geistige Gesundheit",value:0.68},
            {axis:"Zufriedenheit",value:0.7},
            {axis:"Rauchen",value:0.91},
          ]
        ];

Although json_encode is the correct way, it has to be the data-integration, correct? Or am I missing something? Any suggestions? Many thanks for your comments/help in advance!

Community
  • 1
  • 1
Martin
  • 33
  • 1
  • 4
  • Note that JavaScript and Java are two separate languages. You mean JavaScript. – nnnnnn Apr 07 '17 at 04:25
  • Instead of composing your array manually using tons and tons and *tons* of `json_encode` calls and `echo` glue, make an array of arrays and put in one call. Also names like `$arr2` are a sign you need to learn more about nested arrays. They'll make your code a lot cleaner. – tadman Apr 07 '17 at 04:29

1 Answers1

0

Use echo to output a string directly