简体   繁体   English

在 while 中显示来自 SQL 的单独结果

[英]Displaying a separate result from SQL in while

Hello i'm a beginner in these matters.您好,我是这些问题的初学者。

Yesterday i got acquainted with something like a group_concat() function of sql.昨天我熟悉了 sql 的 group_concat() function 之类的东西。 I've got tables called "subjects" and "grades".我有名为“科目”和“成绩”的表格。 In subjects there is stored "id, name, subject_type" and in grades is "id, grade, subject_type".在科目中存储“id、name、subject_type”,在成绩中存储“id、grade、subject_type”。

So if subjects got subject_type = 1 it will shows all of grades for that type.因此,如果科目得到了 subject_type = 1,它将显示该类型的所有成绩。

I've created code like this:我创建了这样的代码:

<?php 
$sqltest1 = "SELECT s.id AS id,  s.name AS name, group_concat(g.grade SEPARATOR ',') as grades, s.teacher_1 as teacher_1
FROM subjects s 
INNER JOIN grades g ON (g.subject_type = s.subject_type)
GROUP BY s.id"; 
?>
<table class="table table-bordered">
      <thead>
        <tr>
          <th class="col-1">#</th>
          <th class="col-3 text-center">Subject</th>
          <th class="col-6 text-center">Grade</th>
          <th class="col-1 text-center">Options</th>
        </tr>
      </thead>
      <tbody>
    <!-- ###################################################################### -->
      <?php
      $result = $conn->query($sqltest1);
      if ($result->num_rows > 0) {
        $i = 1;

        // output data of each row
        while($row = $result->fetch_assoc()) {
          //$id = $row['id'];
          echo "<tr>";
          echo "<td scope='row'>". $i ."</td>";
          echo "<td>". $row['name'] ."</td>";
          echo "<td><span class='bg-primary'>". $row["grades"] ."</span></td>";
          echo "<td class='text-center'>". $row['teacher_1']."</td>";
          echo "</tr>";
          $i++;
      }
 
    
    } else {
    echo "";
    }
    
    ?>
    
    <!-- ###################################################################### -->
    </tbody>
    </table>

So my problem is that i can't retrive separate grades in one column.所以我的问题是我无法在一列中检索单独的成绩。 I would like to get all grades of a given type but to shows as separate grade.我想获得给定类型的所有成绩,但要显示为单独的成绩。

The while should be like this (check the apart inside the while) You get an array by splitting the value by every char you use to separate it: a comma. while 应该是这样的(检查 while 内部的分隔符)您可以通过将值按用于分隔它的每个字符来拆分值来获得一个数组:逗号。 Then you foreach all the elements and write a span inside.然后你foreach所有元素并在里面写一个span。

    // output data of each row
    while($row = $result->fetch_assoc()) {
      //$id = $row['id'];
      echo "<tr>";
      echo "<td scope='row'>". $i ."</td>";
      echo "<td>". $row['name'] ."</td>"; 

      echo "<td>";
      $arrayGrades = explode(",",  $row["grades"]);
      foreach ($arrayGrades as $grade) {
      echo "<span class='bg-primary'>". $grade ."</span>";
      }
      echo "</td>";
      
      echo "<td class='text-center'>". $row['teacher_1']."</td>";
      echo "</tr>";
      $i++;
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM