Home>

If i am in the DB as shown below, I would like to obtain the subject name and score of the subject, and display it on the comparison bar graph. Some people don't have a subject, so I would like to put'0'in that subject. I would like to delete duplicate subjects.

DB
user Course Points
Tanaka 90
Makoto Sato 81
Yamazaki English 79
Tanaka Mathematics 84
Science Yamazaki 64
Sato Society 75
Sato English 87

The shape I want to realize
Example When Tanaka-kun and Sato-kun data are acquired

Two subjects [Japanese, mathematics, society, English]
Tanaka [90,84,0,0]
Sato [0,81,75,87]

I would like to have a shape like

, but I'd like to see a professor without knowing how to do it.

Error message

Because it becomes the following form, 0 cannot be entered, so it will be displayed clogged.
When displayed on the graph, the point of Sato-kun's "mathematics" is displayed in the graph of''s language'as a comparison with Tanaka's national language score.

Two subjects [Japanese, mathematics, society, English]
Tanaka [90,84]
Sato [81,75,87]

Applicable source code
$userdata = "";
  $kamoku_label_value = "";
  $user_name = array ();
  $user_kamoku = array ();
    for ($i = 0;$i<count ($user);$i ++) {
            $sql = "Enters the SELECT statement";
            $stmt = $pdo->query ($sql);
            $stmt->execute ();
            $user_tensu = array ();
            $user_tensu_value = "";
            $border_color = ('red', 'blue', 'green', 'yellow', 'rgba (75,192,192,0.4)', 'rgba (153,255,51,1)', 'rgba (255,153,0,1)' , '# 34495e'];
              while ($result = $stmt->fetch (PDO :: FETCH_ASSOC)) {

                  if ($deleted_at === NULL) {
                    $user_tensu [] = array ('tensu' =>$result ['master_tensu']);
                    $user_kamoku [] = array ('kamoku' =>$result ['kamoku_name']);
                    $user_name [] = array ('name' =>$user_name);
                  }
                }
                / * Check if the array is empty * /
                if (isset ($user_name [$i] ['name'])) {
                  / * Duplicate name check * /
                    $name_tmp = $name_result = array ();
                    foreach ($user_name as $num =>$value) {
                        if (! in_array ($value ['name'], $name_tmp)) {
                          $name_tmp [] = $value ['name'];
                          $name_result [] = $value;
                        }
                    }

                    / * Here is the form to put the associative array of 'tensu' into chart.js into the array * /
                    $user_tensu_array = array_column ($user_tensu, 'tensu');
                    foreach ($user_tensu_array as $val) {
                      $user_tensu_value. = "". $val. ",";
                    }
                    $userdata. = "{label: '". $name_tmp [count ($name_tmp)-1]. "', backgroundColor: '". $border_color [$i]. "', data: [". $user_tensu_value. " ]}, ";
                }

          }/ * Check for duplicate skill names * /
             $kamoku_tmp = $kamoku_result = array ();
               foreach ($user_kamoku as $num =>$value) {
                  if (! in_array ($value ['kamoku'], $kamoku_tmp)) {
                      $kamoku_tmp [] = $value ['kamoku'];
                      $kamoku_result [] = $value;
                  }
               }
            / * From an associative array to an array * /
            $kamoku_name_array = array_column ($kamoku_result, 'kamoku');
            foreach ($kamoku_name_array as $val) {
              $kamoku_label_value. = "'". $val. "',";
            }


The graph is output in here document
$view =<<<EOD
<! DOCTYPE html>
<html lang = "en">
<head>
<meta charset = "UTF-8">
<title>Skill Management</title>
<script type = "text/javascript" src = "../ commons/js/jquery/jquery-3.2.1.min.js"></script>
<script src = "https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.4/Chart.min.js"></script>
</head>
<body>
<script>
var ctx = document.getElementById ("myChart"). getContext ('2d');
var myChart = new Chart (ctx, {
  type: 'horizontalBar',
  data: {
    labels: [$kamoku_label_value],
    datasets: [
    $userdata
    ]
  },
  options: {
    scales: {
        xAxes: [{
            ticks: {
                min: 0,
                max: 150,
                stepSize: 10
            }
        }],
        yAxes: [{
        }]
    },
    title: {
      display: true,
      text: 'score'
    },
  }
});
</script>
</body>

</html>
EOD;
echo $view;
Supplemental information (language/FW/tool version etc.)

The chart uses chart.js

  • Answer # 1

    I'm not looking for that part, but
    Tanaka [90,84,0,0]
    The initial value of
    Tanaka [0,0,0,0]
    And then overwrite it.

Related articles