如何将表转换为treeview?

toiithl6  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(344)

我想把我使用phpmysql得到的表转换成带有复选框的treeview,最好的方法是什么。



树视图

这是我的密码:

<h4>Select Students</h4>
<table class="table table-bordered" id="example">
 <thead>        
  <tr>
   <th>#</th>
   <th>Class</th>
   <th>Roll/SL No.</th>
   <th>Adm No.</th>
   <th>Student Name</th>
   <th>Father Name</th>
  </tr>
 </thead>
<?php
$result = mysqli_query($conn7,"SELECT * FROM student_info WHERE school_id='$school_id' order by st_class_name ASC, roll_no + 0 ASC, st_name ASC");
while($row = mysqli_fetch_assoc($result)){
?>
   <tr>
      <td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $row['st_id']; ?>"></td>
      <td><?php  echo $row["st_class_name"]; ?></td>
      <td><?php  echo $row["roll_no"]; ?></td>
      <td><?php  echo $row["admission_no"]; ?></td>
      <td><?php  echo $row["st_name"]; ?></td> 
      <td><?php  echo $row["father_name"]; ?></td>
    </tr>
    <?php } ?>
</table>

我尝试过的新代码-

<?php
$prev_class_id = 1;
$sql_query_stu = mysqli_query($conn7,"SELECT * FROM student_info where school_uni_id='$school_uni_id' order by st_class ASC, roll_no + 0 ASC, st_name ASC"); 
while($display_stu = mysqli_fetch_assoc($sql_query_stu)){
$class_id = $display_stu['st_class'];
$prev_class_id = prev($class_id);
$class_id = $display_stu['st_class_name'];

if($class_id != $prev_class_id){
?>
    <li>
        <i class="fa fa-plus fa-lg"></i>&nbsp;
        <label>
        <input  id="xnode-0-1" data-id="custom-0-1" type="checkbox" value="<?php echo "$class_id"; ?>"/>
        &nbsp;<?php echo $display_stu['st_class_name']; ?>
        </label>
    </li>
<?php }} ?>

结果-mytree
我的mysql表中的每个学生都有class\u id和class\u name

uttx8gqw

uttx8gqw1#

性能问题是由嵌套的数据库查询造成的。使用包含联接的单个查询可以更好地实现这一点。我必须猜测这个查询,因为您还没有显示表定义。

SELECT s.sub_cat_id,
       s.sub_cat_name.
       i.roll_no,
       i.st_name,
       i.st_mobile,
       i.st_dob 
  FROM all_services s 
 WHERE s.school_id='$school_id'
  JOIN student_info i ON s.sub_cat_id = i.st_class
 ORDER BY s.sub_cat_name,
          i.roll_no + 0 ASC, 
          st_name ASC;

然后您的php代码需要执行以下操作:

$previous_class_id = -10000;
$sql_query = mysqli_query($conn7,$the_query_above);
while($display = mysqli_fetch_assoc($sql_query))
{
   $class_id = $display['sub_cat_id'];

   if ($class_id != $previous_class_id) 
   {
      /* html for the checkboxes, etc for each class */
   }

   /* html for the student details */

   if ($class_id != $previous_class_id) 
   {
      $previous_class_id = $class_id;
      /* html for the ends of lists of students  </ul> </li> and the like */
   }
}

关于 $previous_class_id 用于按类分解结果的呈现。一些语言( PL/1 例如,查找-)有一个

ON CHANGE $class_id { do something }

但在php中,你必须伪造它。
供将来参考:很难从你的问题中猜出你真正的问题是什么。你花时间来更准确地提出问题是绝对值得的。

相关问题