homer.favenir Posted October 21, 2008 Share Posted October 21, 2008 hi, i have a script that loops and one of the field stores number 1 only everyloop. i want to sum the field after each loop. how can i sum them? could anyone please help me. thanks Link to comment https://forums.phpfreaks.com/topic/129363-how-to-sum-integers-after-a-loop/ Share on other sites More sharing options...
Zane Posted October 21, 2008 Share Posted October 21, 2008 $theSum += $row['thefield']; put that in your loop then $theSum will be..........The Sum unless there's more to this question Link to comment https://forums.phpfreaks.com/topic/129363-how-to-sum-integers-after-a-loop/#findComment-670643 Share on other sites More sharing options...
homer.favenir Posted October 21, 2008 Author Share Posted October 21, 2008 it didnt work, my script is like this <?php for($i = 2; $i <= $count_files; $i++) { if(isset($qcfiles[$i])) { ?> <?php echo "<tr><td>"; ?><h4><?php echo $qcfiles[$i];?></h4><?php echo "</td></tr>"; ?> <?php $file1 = file($kedir . "/" . $kefiles[$i]); $file2 = file($qcdir . "/" . $qcfiles[$i]); $file1_count = count($file1); $file2_count = count($file2); $count = 1; for($x = 0; $x <= $file1_count; $x++) { if(isset($file1[$x]) && isset($file2[$x])) { $vtag1 = substr($file1[$x],178,1); $vtag2 = substr($file2[$x],178,1); $page1 = substr($file1[$x],0,4); $page2 = substr($file2[$x],0,4); $type1 = substr($file1[$x],5,1); $type2 = substr($file2[$x],5,1); $name1 = substr($file1[$x],7,78); $name2 = substr($file2[$x],7,78); $ind1 = substr($file1[$x],0,1); $ind2 = substr($file2[$x],0,1); $addr1 = substr($file1[$x], 1, 78); $addr2 = substr($file2[$x], 1, 78); $city1 = substr($file1[$x], 79, 30); $city2 = substr($file2[$x], 79, 30); $state1 = substr($file1[$x], 110, 2); $state2 = substr($file2[$x], 110, 2); $zc1 = substr($file1[$x], 112, 5); $zc2 = substr($file2[$x], 112, 5); $tf1 = substr($file1[$x], 123, 40); $tf2 = substr($file2[$x], 123, 40); $ac1 = substr($file1[$x], 164, 3); $ac2 = substr($file2[$x], 164, 3); $phone1 = substr($file1[$x], 168, 7); $phone2 = substr($file2[$x], 168, 7); if($vtag2 == 1) { $page_comp = strcmp($page1, $page2); $type_comp = strcmp($type1, $type2); $name_comp = strcmp($name1, $name2); if($page_comp != 0) { $err_stat = 1; $error = TRUE; $err_line = $count; $err_page1 = $page1; $err_page2 = $page2;?> <?php echo "<tr><td>"; ?> <?php echo "Line" . " " . $err_line;?><?php echo "</td>"; ?> <?php echo "<td>";?><?php echo "KE Page: " . " " . $err_page1 . "<br>" . "QC Page: " . " " . $err_page2; ?> <?php echo "<td>";?><?php if(isset($error)){$pageerror = 1;echo 1;}?><?php echo "</td>"; ?> <?php echo "<td>";?><?php if(!isset($error)){echo 1;}?><?php echo "</td>"; ?> <?php echo "</td></tr>"; ?> <?php } } } }?> i need my $pageerror to be sum up after the loop. any idea guys? thanks Link to comment https://forums.phpfreaks.com/topic/129363-how-to-sum-integers-after-a-loop/#findComment-670724 Share on other sites More sharing options...
GKWelding Posted October 21, 2008 Share Posted October 21, 2008 array_sum($pageerror); Link to comment https://forums.phpfreaks.com/topic/129363-how-to-sum-integers-after-a-loop/#findComment-670728 Share on other sites More sharing options...
homer.favenir Posted October 21, 2008 Author Share Posted October 21, 2008 $pageerror is not an array Link to comment https://forums.phpfreaks.com/topic/129363-how-to-sum-integers-after-a-loop/#findComment-670736 Share on other sites More sharing options...
thebadbad Posted October 21, 2008 Share Posted October 21, 2008 Set $pageerror to 0 before the loop, and use $pageerror += 1; in the loop. Link to comment https://forums.phpfreaks.com/topic/129363-how-to-sum-integers-after-a-loop/#findComment-670757 Share on other sites More sharing options...
homer.favenir Posted October 22, 2008 Author Share Posted October 22, 2008 where will i put the $pageerror in the script? Up Link to comment https://forums.phpfreaks.com/topic/129363-how-to-sum-integers-after-a-loop/#findComment-671402 Share on other sites More sharing options...
thebadbad Posted October 22, 2008 Share Posted October 22, 2008 Just before the loop, and then add a plus to the equal sign where it already says $pageerror = 1; Don't know if it'll work, since your code is pretty messy. <?php $pageerror = 0; for($i = 2; $i <= $count_files; $i++) { if(isset($qcfiles[$i])) { ?> <?php echo "<tr><td>"; ?><h4><?php echo $qcfiles[$i];?></h4><?php echo "</td></tr>"; ?> <?php $file1 = file($kedir . "/" . $kefiles[$i]); $file2 = file($qcdir . "/" . $qcfiles[$i]); $file1_count = count($file1); $file2_count = count($file2); $count = 1; for($x = 0; $x <= $file1_count; $x++) { if(isset($file1[$x]) && isset($file2[$x])) { $vtag1 = substr($file1[$x],178,1); $vtag2 = substr($file2[$x],178,1); $page1 = substr($file1[$x],0,4); $page2 = substr($file2[$x],0,4); $type1 = substr($file1[$x],5,1); $type2 = substr($file2[$x],5,1); $name1 = substr($file1[$x],7,78); $name2 = substr($file2[$x],7,78); $ind1 = substr($file1[$x],0,1); $ind2 = substr($file2[$x],0,1); $addr1 = substr($file1[$x], 1, 78); $addr2 = substr($file2[$x], 1, 78); $city1 = substr($file1[$x], 79, 30); $city2 = substr($file2[$x], 79, 30); $state1 = substr($file1[$x], 110, 2); $state2 = substr($file2[$x], 110, 2); $zc1 = substr($file1[$x], 112, 5); $zc2 = substr($file2[$x], 112, 5); $tf1 = substr($file1[$x], 123, 40); $tf2 = substr($file2[$x], 123, 40); $ac1 = substr($file1[$x], 164, 3); $ac2 = substr($file2[$x], 164, 3); $phone1 = substr($file1[$x], 168, 7); $phone2 = substr($file2[$x], 168, 7); if($vtag2 == 1) { $page_comp = strcmp($page1, $page2); $type_comp = strcmp($type1, $type2); $name_comp = strcmp($name1, $name2); if($page_comp != 0) { $err_stat = 1; $error = TRUE; $err_line = $count; $err_page1 = $page1; $err_page2 = $page2;?> <?php echo "<tr><td>"; ?> <?php echo "Line" . " " . $err_line;?><?php echo "</td>"; ?> <?php echo "<td>";?><?php echo "KE Page: " . " " . $err_page1 . "<br>" . "QC Page: " . " " . $err_page2; ?> <?php echo "<td>";?><?php if(isset($error)){$pageerror += 1;echo 1;}?><?php echo "</td>"; ?> <?php echo "<td>";?><?php if(!isset($error)){echo 1;}?><?php echo "</td>"; ?> <?php echo "</td></tr>"; ?> <?php } } } }?> Link to comment https://forums.phpfreaks.com/topic/129363-how-to-sum-integers-after-a-loop/#findComment-671587 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.