I need to sum up data obtained by “foreach” function by using javascript

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



I need to sum up data obtained by “foreach” function by using javascript



I have tried to use Javascript to sum up data obtained by foreach function as described below but I have failed, anyone with support p'se..


Javascript


foreach



This is the code:


<tbody>
<?php
$running_year = 2018;
$count = 1;
$query = $this->db->get('teacher')->result_array();
foreach ($query as $row):
?>

<td class="count-me">
<?php
$teacher_id = $this->db->get_where('subject',
array('teacher_id' => $row['teacher_id'],'year'=>
$running_year))->result_array();
foreach ($teacher_id as $row1):?>

<?php
$subject_no = $this->db->get_where('class_routine',
array('subject_id' => $row1['subject_id']));

$test = $subject_no->num_rows();
echo $test;
?>
</br>
<?php endforeach;?>
</td>


<td id="countit">
<?php
$teacher_id = $this->db->get_where('subject',
array('teacher_id' =>$row['teacher_id'], 'year'=> $running_year))->result_array();
foreach ($teacher_id as $row1):?>


//I need the results (that is sum) to be here

</br>
<?php endforeach;;?>
</td>

<?php endforeach;?>

</tbody>



This below is the javascript


<script>
var tds = document.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('countit').innerHTML += '<tr><td>' + sum + '</td><td>total</td></tr>';

</script>



The results are these one



p'se click here to see the result




1 Answer
1



The basic logic of your code is correct. I believe PHP response came late and javascript executes first as being client side. You may try jquery and
$(document).ready(function()


<script
src="https://code.jquery.com/jquery-3.3.1.js"
integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="
crossorigin="anonymous"></script>


<script>

$(document).ready(function()
var tds = document.getElementsByTagName('td');
console.log(tds);
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);



alert(sum);
);
</script>





If I write number manually like <td>5</td> it works. But the number should be called from database by foreach as $row @azizsagi
– AbuuJurayj Swabur Buruhan
Aug 8 at 11:35





yes, can you please tell me what is the format of returning number?
– azizsagi
Aug 8 at 11:53





Ok, The results shown in a table (no. of periods per week) are the values of $test variable, but if i try to write a number manually may be 5 it works, but what i need is to get the sum by adding $test values obtained directly from a database, can you do that? That is why i mentioned "foreach" function @azizsagi
– AbuuJurayj Swabur Buruhan
Aug 8 at 11:59






I think problem is that javascript runs first and data from database loads later. You may try by adding $( document ).ready(); or $( document ).on( ); for that you need to add jquery in your page.
– azizsagi
Aug 8 at 12:04





P'se may you edit your answer so that I can get it more clearly
– AbuuJurayj Swabur Buruhan
Aug 8 at 12:21






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered