skip to Main Content

I have a table within html. the data was being fetched from phpmyadmin using php. and I want to total the values in each row. Please Help.

<?php
// Connect to the database
$dbLink = new mysqli('localhost', 'root', '', 'ghsexam_database');
if(mysqli_connect_errno()) {
die("MySQL connection failed: ". mysqli_connect_error());
}

// Query for a list of all existing files
$sql = 'SELECT `last_name`, `first_name`, `middle_initial`, `educ_attain`, `logic1`, 
`logic2`,`logic3`,  `grammar1`, `grammar2`, `grammar3`, `grammar4`, `grammar5`, `ghs_essay`, 
`reading1`, `reading2`, `reading3`, `reading4`, `listening1`, `listening2` FROM `ghs_table` ';

$result = $dbLink->query($sql);


// Check if it was successfull
if($result) {
// Make sure there are some files in there
if($result->num_rows == 0) {

    echo '<p>There are no files in the database</p>';
}
else {
    // Print the top of a table
    echo '<center><table id=myTable table border=4 cellspacing=1 cellpadding=1 width="100%">
            <tr class="header">

                <th onclick="sortTable(0)" style="cursor:pointer; "nowrap="nowrap"><b>Last Name</b> 
<i class="fa fa-fw fa-sort"></i></th>

                <th onclick="sortTable(1)" style="cursor:pointer;" nowrap="nowrap"><b>First Name</b> 
<i class="fa fa-fw fa-sort"></i></th>
                <th onclick="sortTable(2)" style="cursor:pointer;" nowrap="nowrap"><b>Middle 
Initial</b><i class="fa fa-fw fa-sort"></i></th>
                <th onclick="sortTable(3)" style="cursor:pointer;" nowrap="nowrap"><b>Educational 
Attainment</b><i class="fa fa-fw fa-sort"></i></th>

                <th onclick="sortTable(4)" style="cursor:pointer;" nowrap="nowrap"><b>Logic:1:</b><i 
class="fa fa-fw fa-sort"></i></th>

                <th onclick="sortTable(5)" style="cursor:pointer;"><b>Logic:2</b><i class="fa fa-fw 
fa-sort"></i></th>
                <th onclick="sortTable(6)" style="cursor:pointer;"><b>Logic3</b><i class="fa fa-fw 
fa-sort"></i></th>
                <th onclick="sortTable(7)" style="cursor:pointer;"><b>Grammar:1</b><i class="fa fa- 
fw fa-sort"></i></th>

                <th onclick="sortTable(8)" style="cursor:pointer;" nowrap="nowrap"><b>Grammar:2</b> 
<i class="fa fa-fw fa-sort"></i></th>
                <th onclick="sortTable(9)" style="cursor:pointer;" nowrap="nowrap"><b>Grammar:3</b> 
<i class="fa fa-fw fa-sort"></i></th>
                <th onclick="sortTable(11)" style="cursor:pointer;" nowrap="nowrap"><b>Grammar:4</b> 
<i class="fa fa-fw fa-sort"></i></th>
                <th onclick="sortTable(12)" style="cursor:pointer;" nowrap="nowrap"><b>Grammar:5</b> 
<i class="fa fa-fw fa-sort"></i></th>
                <th onclick="sortTable(13)" style="cursor:pointer;" nowrap="nowrap"> 



 <b>Essay Writing</b><i class="fa fa-fw fa-sort"></i></th>
                <th onclick="sortTable(14)" style="cursor:pointer;" nowrap="nowrap"><b>Reading:1</b> 
<i class="fa fa-fw fa-sort"></i></th>
                <th onclick="sortTable(15)" style="cursor:pointer;" nowrap="nowrap"><b>Reading:2</b> 
<i class="fa fa-fw fa-sort"></i></th>
                <th onclick="sortTable(16)" style="cursor:pointer;" nowrap="nowrap"><b>Reading:3</b>                
<i class="fa fa-fw fa-sort"></i></th>
                <th onclick="sortTable(17)" style="cursor:pointer;" nowrap="nowrap"><b>Reading:4</b> 
<i class="fa fa-fw fa-sort"></i></th>
                <th onclick="sortTable(18)" style="cursor:pointer;" nowrap="nowrap"> 
<b>Listening:1</b><i class="fa fa-fw fa-sort"></i></th>
                <th onclick="sortTable(19)" style="cursor:pointer;" nowrap="nowrap"> 
<b>Listening:2</b><i class="fa fa-fw fa-sort"></i></th>
                <th onclick="sortTable(20)" style="cursor:pointer;" nowrap="nowrap"><b>Total</b><i 
class="fa fa-fw fa-sort"></i></th>

            </tr>';

    // Print each file
    while($row = $result->fetch_assoc()) {
        echo "
            <tr>
                <td>{$row['last_name']}</td>
                <td>{$row['first_name']}</td>
                <td>{$row['middle_initial']}</td>
                <td>{$row['educ_attain']}</td>
                <td class='count-me'>{$row['logic1']}</td>
                <td class='count-me'>{$row['logic2']}</td>
                <td class='count-me'>{$row['logic3']}</td>
                <td class='count-me'>{$row['grammar1']}</td>
                <td class='count-me'>{$row['grammar2']}</td>
                <td class='count-me'>{$row['grammar3']}</td>
                <td class='count-me'>{$row['grammar4']}</td>
                <td class='count-me'>{$row['grammar5']}</td>
                <td>{$row['ghs_essay']}</td>
                <td class='count-me'>{$row['reading1']}</td>
                <td class='count-me'>{$row['reading2']}</td>
                <td class='count-me'>{$row['reading3']}</td>
                <td class='count-me'>{$row['reading4']}</td>
                <td class='count-me'>{$row['listening1']}</td>
                <td class='count-me'>{$row['listening2']}</td>

I have a problem in this part. I want to total the limited values on each rows. like, 1 1 1 1 1 = 5.
Fill free to comment down below.[Click this to see the output][1]

    <script language='javascript' type='text/javascript'>

        var tds = document.getElementById('myTable').getElementsByTagName('td');
        var sum = 0;
        for(var i = 0; i < tds.length; i ++) {
            if(tds[i].className == 'count-me') {
                sum += isNaN(tds[i].innerHTML) ? 0 : parseInt(tds[i].innerHTML);
            }
        }
        document.getElementById('myTable').innerHTML += '<td>as<td>' + sum + '</td>';
    </script>



            </tr>";
    }

    // Close table
     echo '</table></center>';
}

  // Free the result
 $result->free();
}
 else
{
 echo 'Error! SQL query failed:';
 echo "<pre>{$dbLink->error}</pre>";
 }

 // Close the mysql connection
 $dbLink->close();
 ?>


[1]: https://i.stack.imgur.com/JpG2b.png

2

Answers


  1. I’m not sure I understand it right. Do you want to sum up columns in each row? Like logic1+logic2+logic3? If it is what You want try something like this:

    $sql = '
    SELECT 
        `last_name`,
        `first_name`,
        `middle_initial`,
        `educ_attain`,
        `logic1`,
        `logic2`,
        `logic3`,
        (`logic1`+`logic2`+`logic3`) AS logic_sum,
    FROM
        `ghs_table`
    ';
    

    In logic_sum you will get all logic columns summed up. And repeat it of course for grammar, reading and listening. Hope it helps.

    Login or Signup to reply.
  2. You should update your code with as below:

    <script language='javascript' type='text/javascript'>
      var sum = 0;
      $('td').each(function(){
        var tdTxt = $(this).text();
          if($(this).hasClass('count-me')) {
            $(this).text(sum);
            sum = 0;
          } else {
            sum+= parseFloat(tdTxt);    
          } 
      });
      document.getElementById('myTable').innerHTML += '<td>as<td>' + sum + '</td>';
        </script>

    I hope it would help you out.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search