简体   繁体   中英

how to calculate sum of 3 subject and find percentage

i am used codeiginter fremwork and i have one question how to calculate subject mark in codeiginiter Let's explain my question: i am create student markshit in codeiginter. step-1: admin login in login page. step-2: after login display student Markshit in table format.this table formate (admin_list.php) is below.

step-3: admin_list.php page is display student First Name, Last Name, Subject (Maths,Physics,Chemistry).

step-4:how to calculate sum of 3 subject(Maths,Physics,Chemistry) and find percentage of each student ,and it display in admin_list.php page.

my code is below:

i am creating admin_list.php page in view layer this list is display subjectmark. ****admin_list.php****-------this is view layer

 <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>

i am creating Admin.php page in controller,this is below

**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');
    }

i am creating Admin_model.php page in Model ,this is below

**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);
    }

i am creating admin_edit.php page in View Layer,this is below

**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(); ?>
<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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