codeigniter 将sql表中的值显示到正确的html表列中

zphenhs4  于 2022-12-30  发布在  其他
关注(0)|答案(1)|浏览(101)

我想在html表格中显示数据库中的值。这些值是包含学生费用支付信息的数据。
我想把它们显示在一个表上,以创建某种收费报告或财务报表。
这就是我想要的表格外观:

adm no | class   | student name | Tuition | Transportation | Boarding | Total
------ |---------|--------------|---------|----------------|----------|--------
001    | class 1 | Mary Mordi   |  25000  |                | 5000     | 30000 
025    | class 3 | Daniel Ify   |  35000  |   12000        |          | 47000

我似乎不能像这样正确地得到值。它们只是分散在各处。我猜可能是我的查询或foreach循环。

--表格student_fees_master的表格结构

CREATE TABLE `student_fees_master` (
  `id` int(11) NOT NULL,
  `is_system` int(1) NOT NULL DEFAULT 0,
  `student_session_id` int(11) DEFAULT NULL,
  `fee_session_group_id` int(11) DEFAULT NULL,
  `amount` float(10,2) DEFAULT 0.00,
  `is_active` varchar(10) NOT NULL DEFAULT 'no',
  `created_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-表格fee_session_groups的表格结构

CREATE TABLE `fee_session_groups` (
  `id` int(11) NOT NULL,
  `fee_groups_id` int(11) DEFAULT NULL,
  `session_id` int(11) DEFAULT NULL,
  `is_active` varchar(10) NOT NULL DEFAULT 'no',
  `created_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-表格fee_groups的表格结构

CREATE TABLE `fee_groups` (
  `id` int(11) NOT NULL,
  `name` varchar(200) DEFAULT NULL,
  `is_system` int(1) NOT NULL DEFAULT 0,
  `description` text DEFAULT NULL,
  `is_active` varchar(10) NOT NULL DEFAULT 'no',
  `created_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

----表格fee_groups_feetype的表格结构

CREATE TABLE `fee_groups_feetype` (
  `id` int(11) NOT NULL,
  `fee_session_group_id` int(11) DEFAULT NULL,
  `fee_groups_id` int(11) DEFAULT NULL,
  `feetype_id` int(11) DEFAULT NULL,
  `session_id` int(11) DEFAULT NULL,
  `amount` decimal(10,2) DEFAULT NULL,
  `fine_type` varchar(50) NOT NULL DEFAULT 'none',
  `due_date` date DEFAULT NULL,
  `fine_percentage` float(10,2) NOT NULL DEFAULT 0.00,
  `fine_amount` float(10,2) NOT NULL DEFAULT 0.00,
  `is_active` varchar(10) NOT NULL DEFAULT 'no',
  `created_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-表格student_fees_deposite的表格结构

CREATE TABLE `student_fees_deposite` (
  `id` int(11) NOT NULL,
  `student_fees_master_id` int(11) DEFAULT NULL,
  `fee_groups_feetype_id` int(11) DEFAULT NULL,
  `amount_detail` text DEFAULT NULL,
  `is_active` varchar(10) NOT NULL DEFAULT 'no',
  `created_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

型号代码

$sql = "SELECT student_fees_master.id,
student_fees_master.student_session_id,
student_fees_master.fee_session_group_id,students.firstname,
students.admission_no,students.lastname,fee_groups.name,
IFNULL(student_fees_deposite.id,0) as `student_fees_deposite_id`,
JSON_VALUE(student_fees_deposite.amount_detail, '$.*.amount') AS `deposit`
FROM `student_fees_master` 
INNER JOIN fee_session_groups on fee_session_groups.id = student_fees_master.fee_session_group_id 
INNER JOIN fee_groups_feetype on  fee_groups_feetype.fee_session_group_id = fee_session_groups.id 
INNER JOIN fee_groups on fee_groups.id=fee_groups_feetype.fee_groups_id 
INNER JOIN feetype on feetype.id=fee_groups_feetype.feetype_id 
LEFT JOIN student_fees_deposite on student_fees_deposite.student_fees_master_id=student_fees_master.id and student_fees_deposite.fee_groups_feetype_id=fee_groups_feetype.id 
INNER JOIN student_session on student_session.id= student_fees_master.student_session_id  
INNER JOIN students on students.id=student_session.student_id 
GROUP BY student_fees_master.id";

 $query = $this->db->query($sql);
        return $query->result();
    }

x1c 0d1x控制器

function financialreport() {

        if (!$this->rbac->hasPrivilege('balance_fees_report', 'can_view')) {
            access_denied();
        }

        $this->session->set_userdata('top_menu', 'Reports');
        $this->session->set_userdata('sub_menu', 'Reports/finance');
        $this->session->set_userdata('subsub_menu', 'Reports/finance/financialreport');
        $data['title'] = 'student fee';
        $data['title'] = 'student fee';
        $data['sch_setting'] = $this->sch_setting_detail;
        $this->form_validation->set_rules('class_id', $this->lang->line('class'), 'trim|required|xss_clean');
        $this->form_validation->set_rules('section_id', $this->lang->line('section'), 'trim|required|xss_clean');
        
        $data['feedetails'] = $this->studentfeemaster_model->getFullFinancialRecord($fee_session_groups_id, $student_fees_master_id, $fee_groups_feetype_id);

       $data['feegroup'] = $this->studentfeemaster_model->getFeeGroupName();
       
        
        $this->load->view('layout/header', $data);
        $this->load->view('admin/transaction/financialreport', $data);
        $this->load->view('layout/footer', $data);

     }

查看

<table class="table table-striped table-hover" id="headerTable">
                                        <thead>
                                            <tr>
       <th class="text text-left" style="border: 1px solid black;"><?php echo $this->lang->line('admission_no'); ?></th>
        <th class="text-left" style="border: 1px solid black;"><?php echo $this->lang->line('class'); ?></th>
       <th class="text text-left" style="border: 1px solid black;"><?php echo $this->lang->line('student_name'); ?></th> 
            <?php
            foreach ($feedetails as $fee_key => $fee_value) {
                                                    ?>
        <th style="border: 1px solid black;"><?php echo $fee_value->name;?> </th>
                                                <?php
                                             ?>
      <th class="text text-left" style="border: 1px solid black;">Total</th>
                                           

</tr>
</thead>
<tbody>
<tr>
            <td style="border: 1px solid black;"><?php echo $fee_value->admission_no; ?></td>
            <td style="border: 1px solid black;"><?php echo "{$fee_value->class} {$fee_value->section}"; ?></td>
        <td style="border: 1px solid black;"><?php echo "{$fee_value->lastname} {$fee_value->firstname}"; ?></td>
       
          <td style="border: 1px solid black;"><?php echo $fee_value->deposit;?> </td>
               
             <td style="border: 1px solid black;"><?php echo $fee_value->amount;?>
                                                            </td>                                  

</tr>
 <?php 

}
?>   
</tbody>
</table>
dced5bon

dced5bon1#

在浏览器的代码查看器中检查HTML的输出,看起来for循环包含了表头的结尾和数据,所以在每个循环中回显了两个表行闭包</tr>,以及额外的<tbody>元素等。
您实际上需要两个数据集、一个字段列表和一个人员/费用列表。然后,您可以使用一个循环导出标题行,使用两个嵌套循环输出数据。
您需要确保数据按照与表字段相同的顺序排序,并在没有数据的地方导出空的<td></td>对。
您可以通过对主数据调用数组函数或通过单独的数据库查询来获取标题数据。
在数据库中使用透视查询将行转换为列,使其与表具有相同的格式,这可能更容易。

**编辑:**可能类似于以下内容

1.提取费用列名列表
1.将数据聚合到数组中
1.输出HTML表

  • (我在这里使用学生姓名对数据进行分组,但是您可能希望使用SQL中提到的主ID,我错过了它,因为它不在示例数据输出中)*
<?php
 // Sample data
$feedetails = [
    ['id' => 1, 'admissionnumber' => 123, 'firstname'=>'Fred', 'name' => 'tuition', 'deposit'=>1000],
    ['id' => 2, 'admissionnumber' => 123, 'firstname'=>'Fred', 'name' => 'room', 'deposit'=>2000],
    ['id' => 3, 'admissionnumber' => 111, 'firstname'=>'John', 'name' => 'tuition', 'deposit'=>1000],
    ['id' => 4, 'admissionnumber' => 111, 'firstname'=>'John', 'name' => 'car', 'deposit'=>3000],
    ];

    // Get a list of fee types (for columns in html table)
    $feeTypes= array_count_values(array_column($feedetails, 'name'));
// $feeTypes = array(3) { ["tuition"]=> int(2) ["room"]=> int(1) ["car"]=> int(1) } 

    // Aggregate the data against each person
    $fees = [];
    foreach($feedetails as $feedetail){
        $fees[$feedetail['firstname']]['admissionno'] = $feedetail['admissionnumber'];
        $fees[$feedetail['firstname']][$feedetail['name']] = $feedetail['deposit'];
    }

// $feedetail = array(2) {
// ["Fred"]=> array(3) { ["admissionno"]=> int(123) ["tuition"]=> int(1000) ["room"]=> int(2000) }
// ["John"]=> array(3) { ["admissionno"]=> int(111) ["tuition"]=> int(1000) ["car"]=> int(3000) }
//  } 

    // Print table
    ?>
<table class="table table-striped table-hover" id="headerTable">
<thead>
<tr>
  <th class="text text-left" style="border: 1px solid black;">admission_no</th>
    <th class="text text-left" style="border: 1px solid black;">student_name</th> 
  <?php     foreach ($feeTypes as $feeName => $count) {
        echo '<th style="border: 1px solid black;">' . $feeName . '</th>';
            }
?>
    <th class="text text-left" style="border: 1px solid black;">Total</th>
</tr>
</thead>
<tbody>

<?php
// Table Header Above

// Table Body Below
foreach ($fees as $firstname => $feedetail){
    $total = 0;
?>
<tr>
        <td style="border: 1px solid black;"><?php echo $feedetail['admissionno'] ?></td>
        <td style="border: 1px solid black;"><?php echo $firstname; ?></td>
        <?php
        // Go through each fee type printing data, or an empty table grid if no data
        foreach ($feeTypes as $feeName => $count) {
            if(array_key_exists($feeName, $feedetail)) {
                // fee type found for this student, echo data and add to total
                echo '<td style="border: 1px solid black;">' .  $feedetail[$feeName] . '</td>';
                $total += $feedetail[$feeName];
            } // otherwise echo empty table cell
            else echo '<td style="border: 1px solid black;"></td>';
        }
        ?>
        <td style="border: 1px solid black;"><?php echo $total;?>
    </td>
</tr>
<?php
}
?>
</tbody>
</table>

相关问题