Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,308 members, 7,808,044 topics. Date: Thursday, 25 April 2024 at 05:34 AM

Please Help Look At My Code (what is wrong with my code ) - Programming (2) - Nairaland

Nairaland Forum / Science/Technology / Programming / Please Help Look At My Code (what is wrong with my code ) (3531 Views)

Why Do My Children Need To Learn To Code? What’s All The Fuss? / Da Fuk Is Wrong With This Board? Useless Threads Everywhere!! / Open Souce Or Source Code, What's It Really? (2) (3) (4)

(1) (2) (Reply)

Re: Please Help Look At My Code (what is wrong with my code ) by nicolas247(m): 3:31am On Jan 23, 2018
<?php
require_once('config.php');
error_reporting(1);
// Pull all the required data from the three tables into one 2 level array
$sql = 'select * from january UNION ALL select * from february UNION ALL select * from march UNION ALL select * from april UNION ALL select * from may UNION ALL select * from june UNION ALL select * from july UNION ALL select * from august UNION ALL select * from september UNION ALL select * from october UNION ALL select * from november UNION ALL select * from december';
$query = $pdo->prepare($sql);
$query->execute();
$row = $query->fetchAll();

// Pull a list of all the memberId from one of the table assuming we have the same number of IDs in all tables
// I added UNION to the select statement because of some record not in all of the table and that works very fine
$sql1 = 'select memberId, name FROM january UNION select memberId, name FROM february UNION select memberId, name FROM march UNION select memberId, name FROM april UNION select memberId, name FROM may UNION select memberId, name FROM june UNION select memberId, name FROM july UNION select memberId, name FROM august UNION select memberId, name FROM september UNION select memberId, name FROM october UNION select memberId, name FROM november UNION select memberId, name FROM december';
$query = $pdo->prepare($sql1);
$query->execute();
$memberIdList = $query->fetchAll();
// start a loop to loop through the list of the memberIds
foreach ($memberIdList as $memberIdList) {


//initialize the variable that will store the sum of each column
$loanamt = 0;
$loan_int = 0;
$saving = 0;
$total = 0;



//start a loop to loop through the 2 level array
foreach ($row as $key => $value){



// use an IF statement to select each of the memberIds and corresponding columns (loan_amt etc) and add them together as the loop runs
if ($row[$key]['memberId']== $memberIdList['memberId']){
$loanamt += $row[$key]['loan_amt'];
$loan_int += $row[$key]['loan_int'];
$saving += $row[$key]['saving_ded'];
$total += $row[$key]['total_ded'];
}
}
// print out the values after each memberId iteration
//echo $memberIdList['memberId']." ".$loanamt." ".$saving." ".$total."</br>";
echo "<tr>";
echo "<td>".$memberIdList['memberId']."</td>";
echo "<td>".$memberIdList['name']."</td>";
echo "<td>".$loanamt += $row[$key]['loan_amt']."</td>";
echo "<td>".$loan_int += $row[$key]['loan_int']."</td>";
echo "<td>".$saving += $row[$key]['saving_ded']."</td>";
echo "<td>".$total += $row[$key]['total_ded']."</td>";
echo "</tr>";
}


?>
Re: Please Help Look At My Code (what is wrong with my code ) by nicolas247(m): 9:49am On Jan 23, 2018
my final Code

<?php
require_once('config.php');
error_reporting(1);
// Pull all the required data from the three tables into one 2 level array
$sql = 'select * from january UNION ALL select * from february UNION ALL select * from march UNION ALL select * from april UNION ALL select * from may UNION ALL select * from june UNION ALL select * from july UNION ALL select * from august UNION ALL select * from september UNION ALL select * from october UNION ALL select * from november UNION ALL select * from december';
$query = $pdo->prepare($sql);
$query->execute();
$row = $query->fetchAll();

// Pull a list of all the staffId from one of the table assuming we have the same number of IDs in all tables
// I added UNION to the select statement because of some record not in all of the table and that works very fine
$sql1 = 'select staffId, name FROM january UNION select staffId, name FROM february UNION select staffId, name FROM march UNION select staffId, name FROM april UNION select staffId, name FROM may UNION select staffId, name FROM june UNION select staffId, name FROM july UNION select staffId, name FROM august UNION select staffId, name FROM september UNION select staffId, name FROM october UNION select staffId, name FROM november UNION select staffId, name FROM december';
$query = $pdo->prepare($sql1);
$query->execute();
$staffIdList = $query->fetchAll();
// start a loop to loop through the list of the staffIds
foreach ($staffIdList as $staffIdList) {


//initialize the variable that will store the sum of each column
$loanamt = 0;
$loan_int = 0;
$saving = 0;
$total = 0;



//start a loop to loop through the 2 level array
foreach ($row as $key => $value){



// use an IF statement to select each of the staffIds and corresponding columns (loan_amt etc) and add them together as the loop runs
if ($row[$key]['staffId']== $staffIdList['staffId']){
$loanamt += $row[$key]['loan_amt'];
$loan_int += $row[$key]['loan_int'];
$saving += $row[$key]['saving_ded'];
$total += $row[$key]['total_ded'];
}
}
// print out the values after each staffId iteration
//echo $staffIdList['staffId']." ".$loanamt." ".$saving." ".$total."</br>";
echo "<tr>";
echo "<td>".$staffIdList['staffId']."</td>";
echo "<td>".$staffIdList['name']."</td>";
echo "<td>".$loanamt += $row[$key]['loan_amt']."</td>";
echo "<td>".$loan_int += $row[$key]['loan_int']."</td>";
echo "<td>".$saving += $row[$key]['saving_ded']."</td>";
echo "<td>".$total += $row[$key]['total_ded']."</td>";
echo "</tr>";
}


?>
Re: Please Help Look At My Code (what is wrong with my code ) by benjibabs(m): 4:35am On Jan 24, 2018
Looks good!

nicolas247:

my final Code

<?php
require_once('config.php');
error_reporting(1);
// Pull all the required data from the three tables into one 2 level array
$sql = 'select * from january UNION ALL select * from february UNION ALL select * from march UNION ALL select * from april UNION ALL select * from may UNION ALL select * from june UNION ALL select * from july UNION ALL select * from august UNION ALL select * from september UNION ALL select * from october UNION ALL select * from november UNION ALL select * from december';
$query = $pdo->prepare($sql);
$query->execute();
$row = $query->fetchAll();

// Pull a list of all the staffId from one of the table assuming we have the same number of IDs in all tables
// I added UNION to the select statement because of some record not in all of the table and that works very fine
$sql1 = 'select staffId, name FROM january UNION select staffId, name FROM february UNION select staffId, name FROM march UNION select staffId, name FROM april UNION select staffId, name FROM may UNION select staffId, name FROM june UNION select staffId, name FROM july UNION select staffId, name FROM august UNION select staffId, name FROM september UNION select staffId, name FROM october UNION select staffId, name FROM november UNION select staffId, name FROM december';
$query = $pdo->prepare($sql1);
$query->execute();
$staffIdList = $query->fetchAll();
// start a loop to loop through the list of the staffIds
foreach ($staffIdList as $staffIdList) {


//initialize the variable that will store the sum of each column
$loanamt = 0;
$loan_int = 0;
$saving = 0;
$total = 0;



//start a loop to loop through the 2 level array
foreach ($row as $key => $value){



// use an IF statement to select each of the staffIds and corresponding columns (loan_amt etc) and add them together as the loop runs
if ($row[$key]['staffId']== $staffIdList['staffId']){
$loanamt += $row[$key]['loan_amt'];
$loan_int += $row[$key]['loan_int'];
$saving += $row[$key]['saving_ded'];
$total += $row[$key]['total_ded'];
}
}
// print out the values after each staffId iteration
//echo $staffIdList['staffId']." ".$loanamt." ".$saving." ".$total."</br>";
echo "<tr>";
echo "<td>".$staffIdList['staffId']."</td>";
echo "<td>".$staffIdList['name']."</td>";
echo "<td>".$loanamt += $row[$key]['loan_amt']."</td>";
echo "<td>".$loan_int += $row[$key]['loan_int']."</td>";
echo "<td>".$saving += $row[$key]['saving_ded']."</td>";
echo "<td>".$total += $row[$key]['total_ded']."</td>";
echo "</tr>";
}


?>

(1) (2) (Reply)

IOS Mobile App Developer Wanted / SQL And MySQL / Complete COMPUTER SCIENCE Tutorial Course

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 31
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.