如何计算3个科目的总和并找到百分比

时间:2016-04-29 12:33:39

标签: php codeigniter

我使用的是codeiginter fremwork,我有一个问题 如何计算codeiginiter中的主题标记 让我们解释一下我的问题: 我在codeiginter中创建学生markshit。 步骤1:登录页面中的admin登录。 步骤2:登录后以表格格式显示学生Markshit。此表格式(admin_list.php)位于下方。

步骤3:admin_list.php页面显示学生名字,姓氏,科目(数学,物理,化学)。

步骤4:如何计算3个科目(数学,物理,化学)的总和,找到每个学生的百分比,并显示在admin_list.php页面。

我的代码如下:

我正在视图层创建admin_list.php页面 此列表是显示主题标记。     **** admin_list.php **** -------这是视图层

 <body>
        <table border="1">
            <thead>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Maths</th>
            <th>Physics</th>
            <th>Chemistry</th>
            <th colspan="6">Action</th>

        </thead>
        <?php foreach ($result as $value): ?>
            <tr>
                <td><?php echo $value['student_firstname'] ?></td>
                <td><?php echo $value['student_lastname'] ?></td>
                <td><?php echo $value['maths'] ?></td>
                <td><?php echo $value['physics'] ?></td>
                <td><?php echo $value['chemistry'] ?></td>
                <td><a href="<?php echo base_url() ?>Admin/edit/<?php echo $value['student_master_id'] ?>">Edit</a></td>
                <td><a href="<?php echo base_url() ?>Admin/delete/<?php echo $value['student_master_id'] ?>">Delete</a></td>
            </tr>
        <?php endforeach; ?>
    </table>

我正在控制器中创建Admin.php页面,这在

下面
**This is controller**

   public function subject_list() {
        $data['result'] = $this->admin_model->subject();
        $this->load->view('admin/admin_list', $data);
    }

    public function edit($id) {
        $data['result'] = $this->admin_model->get_student_id($id);
        //var_dump($id);
        $data['id'] = $id;

        if (!empty($_POST['submit'])) {
            $this->update($id);
        }

        $this->load->view('admin/admin_edit', $data);
    }

    public function update($id) {
        $this->admin_model->edit_mark($_POST, $id);
        redirect('Admin/subject_list');
    }

我正在模型中创建Admin_model.php页面,这位于

之下
**This is Model**

 public function subject() {

        return $this->db->get('student_info')->result_array();
    }

    public function get_student_id($id) {
        return $this->db->get_where('student_info', array('student_master_id' => $id))->row_array();
    }

    public function edit_mark($data, $id) {

        $data1 = array(
            'maths' => $data['maths'],
            'physics' => $data['physics'],
            'chemistry' => $data['chemistry']
        );

        $this->db->where('student_master_id', $id);
        $this->db->update('student_info', $data1);
    }

我正在View Layer中创建admin_edit.php页面,这位于

之下
**This is admin/admin_edit.php**

     <?php echo form_open('Admin/edit/' . $id); ?>
        <table border="1">

            <tr>
                <td>Maths:</td>
                <td><input type="text" name="maths" value="<?php echo $result['maths'] ?>"></td>
            </tr>
            <tr>
                <td>Physics:</td>
                <td><input type="text" name="physics" value="<?php echo $result['physics'] ?>"></td>
            </tr>
            <tr>
                <td>Chemistry:</td>
                <td><input type="text" name="chemistry" value="<?php echo $result['chemistry'] ?>"></td>
            </tr>

            <tr></tr>
            <tr>
            <input type="hidden" name="student_master_id" value="<?php echo $result['student_master_id'] ?>">
            <td colspan="2"><center><input type="submit" name="submit" value="Update"></center></td>
    </tr>
</table>
<?php echo form_close(); ?>

1 个答案:

答案 0 :(得分:0)

<body>
    <table border="1">
       <thead>
          <th>First Name</th>
          <th>Last Name</th>
          <th>Maths</th>
          <th>Physics</th>
          <th>Chemistry</th>
          <th>Total</th>
          <th>Percentage</th>  
          <th colspan="6">Action</th>
      </thead>
          <?php foreach ($result as $value): 
              $total=$value['maths']+$value['chemistry']+$value['physics'];
              $per= ($total/3);
          ?>
          <tr>
              <td><?php echo $value['student_firstname'] ?></td>
              <td><?php echo $value['student_lastname'] ?></td>
              <td><?php echo $value['maths'] ?></td>
              <td><?php echo $value['physics'] ?></td>
              <td><?php echo $value['chemistry'] ?></td>
              <td><?php echo $total; ?></td>
              <td><?php echo $per; ?></td> 
              <td><a href="<?php echo base_url() ?>Admin/edit/<?php echo $value['student_master_id'] ?>">Edit</a></td>
              <td><a href="<?php echo base_url() ?>Admin/delete/<?php echo $value['student_master_id'] ?>">Delete</a></td>
         </tr>
            <?php endforeach; ?>
</table>