Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,148,737 members, 7,802,219 topics. Date: Friday, 19 April 2024 at 11:15 AM

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

Nairaland Forum / Science/Technology / Programming / Please Help Look At My Code (what is wrong with my code ) (3523 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) (Go Down)

Please Help Look At My Code (what is wrong with my code ) by nicolas247(m): 8:56pm On Jan 10, 2018
This is my Code but i am clearly hooked


<?php
require 'config.php';

$january=mysqli_query($con,'SELECT staffid, saving_ded, total_ded
FROM january

UNION ALL

SELECT staffid, saving_ded, total_ded

FROM february

UNION ALL

SELECT staffid, saving_ded, total_ded

FROM march
');
while($row1=mysqli_fetch_array($january))
{
$jan1=$row1['staffid'];
$jan2=$row1['saving_ded'];
$jan3=$row1['total_ded'];
?>
<tr>
<td><?php echo $jan1 ?></td>
<td><?php echo $jan2 ?></td>
<td><?php echo $jan3 ?></td>
</tr>
<?php }
?>

Please i really need help i am stock for days now

Table1

StaffID loan_amt Saving_ded total_ded
122 10 15 25
235 30 20 50
Table2

StaffID loan_amt Saving_ded total_ded
122 10 15 25
235 30 20 50

Table3

StaffID loan_amt Saving_ded total_ded
122 10 15 25
235 30 20 50

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

This is the result i will like to archive

StaffID loan_amt Saving_ded total_ded
122 20 30 50
235 60 40 100
Re: Please Help Look At My Code (what is wrong with my code ) by nicolas247(m): 10:59pm On Jan 10, 2018
help you boy o!
Re: Please Help Look At My Code (what is wrong with my code ) by nicolas247(m): 11:31pm On Jan 10, 2018
people just they look dey pass?
Re: Please Help Look At My Code (what is wrong with my code ) by LegendsCoded(m): 2:29am On Jan 11, 2018
Whats the response/error u get?
Re: Please Help Look At My Code (what is wrong with my code ) by tollyboy5(m): 3:34am On Jan 11, 2018
nicolas247:
people just they look dey pass?
Lolz you think sey e easy. I no b guru but you 4 make easier by telling which language you use
Re: Please Help Look At My Code (what is wrong with my code ) by nicolas247(m): 5:16am On Jan 11, 2018
tollyboy5:

Lolz you think sey e easy. I no b guru but you 4 make easier by telling which language you use

It is PHP boss

I have been on this for a week now

it is returning wrong values
Re: Please Help Look At My Code (what is wrong with my code ) by nicolas247(m): 5:17am On Jan 11, 2018
LegendsCoded:
Whats the response/error u get?

it is not giving error but just that it is returning wrong value
Re: Please Help Look At My Code (what is wrong with my code ) by Softhands(m): 5:44am On Jan 11, 2018
Delete line by line and keep testing each line to know where the problem is... Or check stack overflow..
Re: Please Help Look At My Code (what is wrong with my code ) by nicolas247(m): 5:49am On Jan 11, 2018
Softhands:
Delete line by line and keep testing each line to know where the problem is... Or check stack overflow..
i have checked stack overflow i dont have reference i can relate with. i don tire
Re: Please Help Look At My Code (what is wrong with my code ) by Softhands(m): 7:33am On Jan 11, 2018
Then check line by line... That's how you resolve it o.
nicolas247:

i have checked stack overflow i dont have reference i can relate with. i don tire
Re: Please Help Look At My Code (what is wrong with my code ) by bet9ja(m): 8:09am On Jan 11, 2018
Your code is giving a logical error and not run-time error.

I don't know php but with my programming knowledge. Let me see if i can clearly understand what you are trying to achieve.

From this code snipet below

while($row1=mysqli_fetch_array($january))
{
$jan1=$row1['staffid'];
$jan2=$row1['saving_ded'];
$jan3=$row1['total_ded'];
?>
<tr>
<td><?php echo $jan1 ?></td>
<td><?php echo $jan2 ?></td>
<td><?php echo $jan3 ?></td>
</tr>
<?php }
?>

Do you want to echo out the value of staffid, 'saving_ded and total_ded from just January 1 to January 3 or you want to echo out all the variables from January 1. Please explain
Re: Please Help Look At My Code (what is wrong with my code ) by bet9ja(m): 8:18am On Jan 11, 2018
tollyboy5:

Lolz you think sey e easy. I no b guru but you 4 make easier by telling which language you use


Seriously, are you a programmer at all? Someone posted a source code and you can't even tell if it is a PHP code or whatever. That is really bad.
Re: Please Help Look At My Code (what is wrong with my code ) by tollyboy5(m): 11:32am On Jan 11, 2018
bet9ja:



Seriously, are you a programmer at all? Someone posted a source code and you can't even tell if it is a PHP code or whatever. That is really bad.
I'm nt guru na. I'm still learning but based on embedded design for microcontrollers. Na assembly c/c++ b my major
Re: Please Help Look At My Code (what is wrong with my code ) by RealWealth22: 3:22pm On Jan 11, 2018
bet9ja:



Seriously, are you a programmer at all? Someone posted a source code and you can't even tell if it is a PHP code or whatever. That is really bad.
i thought as much if he is a programmer he should know the language.
Re: Please Help Look At My Code (what is wrong with my code ) by yorex2011: 5:18pm On Jan 11, 2018
You have tables for January , February... Etc ??

Why not create one table and have a month column, then create you sql to search through the months you need.

What exactly are you trying to achieve, cos i don't seem to understand..

Who taught you php?
Re: Please Help Look At My Code (what is wrong with my code ) by wane01(m): 5:37pm On Jan 11, 2018
tollyboy5:

I'm nt guru na. I'm still learning but based on embedded design for microcontrollers. Na assembly c/c++ b my major
u don't need to b a guru to identify lang from the syntax structure .... the lang was even declared in the source code except if u didn't take note and if u didn't ... it means you can't debug a code

1 Like

Re: Please Help Look At My Code (what is wrong with my code ) by Singlecliq: 8:26pm On Jan 11, 2018
let me see the result you are getting.
You should be getting this with the code I am seeing

StaffID loan_amt Saving_ded total_ded
122 20 30 50
122 20 30 50
122 20 30 50
235 60 40 100
235 60 40 100
235 60 40 100
Re: Please Help Look At My Code (what is wrong with my code ) by Infinity7(m): 9:25pm On Jan 12, 2018
//Try This


<?php
require 'config.php';

$january=mysqli_query($con,'SELECT staffid, saving_ded, total_ded
FROM january

UNION ALL

SELECT staffid, saving_ded, total_ded

FROM february

UNION ALL

SELECT staffid, saving_ded, total_ded

FROM march
');

while($row1=mysqli_fetch_array($january))
{
$jan1=$row1['staffid'];
$jan2=$row1['saving_ded'];
$jan3=$row1['total_ded'];

<tr>
<td><?php echo $jan1 ?></td>
<td><?php echo $jan2 ?></td>
<td><?php echo $jan3 ?></td>
</tr>
}

?>


//where is from March?
//i rearranged the syntax. Try Now inshaAllah.
Re: Please Help Look At My Code (what is wrong with my code ) by LegendsCoded(m): 12:13am On Jan 15, 2018
nicolas247:


it is not giving error but just that it is returning wrong value

Have you fixed it?
Re: Please Help Look At My Code (what is wrong with my code ) by nicolas247(m): 5:31pm On Jan 15, 2018
not at all
LegendsCoded:


Have you fixed it?

Re: Please Help Look At My Code (what is wrong with my code ) by nicolas247(m): 5:49pm On Jan 15, 2018
lol...

this app was my first app and the company are still using it. i will definitely rewrite it but i need this solution in the mean time

yorex2011:
You have tables for January , February... Etc ??

Why not create one table and have a month column, then create you sql to search through the months you need.

What exactly are you trying to achieve, cos i don't seem to understand..

Who taught you php?
Re: Please Help Look At My Code (what is wrong with my code ) by nicolas247(m): 6:16pm On Jan 15, 2018
Singlecliq:
let me see the result you are getting.
You should be getting this with the code I am seeing

StaffID loan_amt Saving_ded total_ded
122 20 30 50
122 20 30 50
122 20 30 50
235 60 40 100
235 60 40 100
235 60 40 100

Yes but the result i want is this
staff id loan_amt Saving_ded total_ded
112 60 90 150
235 180 120 300


Re: Please Help Look At My Code (what is wrong with my code ) by Singlecliq: 3:37pm On Jan 16, 2018
[quote author=nicolas247 post=64225767][/quote]

<?php
require 'config.php';
$january=mysqli_query($con,'SELECT staffid, saving_ded, total_ded FROM january UNION ALL SELECT staffid, saving_ded, total_ded FROM february UNION ALL SELECT staffid, saving_ded, total_ded FROM march ');
//setting up as an array $month = array();
//looping through and saving as an array while ($row = mysql_fetch_assoc($january)) $month[] = $row;
//setup a new array after loop
$result = array();
foreach ($month as $val) {
if (!isset($result[$val['staffid']]))
$result[$val['staffid']] = $val;
else
$result[$val['staffid']]['saving_ded'] += $val['saving_ded']; }
$result = array_values($result);
// reindex array
Re: Please Help Look At My Code (what is wrong with my code ) by nicolas247(m): 8:17am On Jan 18, 2018
Thanks but i still got this error

Warning: A non-numeric value encountered in D:\xampp\htdocs\2017\test.php on line 12

Warning: A non-numeric value encountered in D:\xampp\htdocs\2017\test.php on line 12

Warning: A non-numeric value encountered in D:\xampp\htdocs\2017\test.php on line 12

Warning: A non-numeric value encountered in D:\xampp\htdocs\2017\test.php on line 12

Warning: A non-numeric value encountered in D:\xampp\htdocs\2017\test.php on line 12

Warning: A non-numeric value encountered in D:\xampp\htdocs\2017\test.php on line 12

Warning: A non-numeric value encountered in D:\xampp\htdocs\2017\test.php on line 12

Warning: A non-numeric value encountered in D:\xampp\htdocs\2017\test.php on line 12

Warning: A non-numeric value encountered in D:\xampp\htdocs\2017\test.php on line 12

Warning: A non-numeric value encountered in D:\xampp\htdocs\2017\test.php on line 12

Warning: A non-numeric value encountered in D:\xampp\htdocs\2017\test.php on line 12

Warning: A non-numeric value encountered in D:\xampp\htdocs\2017\test.php on line 12

Warning: A non-numeric value encountered in D:\xampp\htdocs\2017\test.php on line 12

Warning: A non-numeric value encountered in D:\xampp\htdocs\2017\test.php on line 12

Warning: A non-numeric value encountered in D:\xampp\htdocs\2017\test.php on line 12

Warning: A non-numeric value encountered in D:\xampp\htdocs\2017\test.php on line 12

Warning: A non-numeric value encountered in D:\xampp\htdocs\2017\test.php on line 12

Warning: A non-numeric value encountered in D:\xampp\htdocs\2017\test.php on line 12

Warning: A non-numeric value encountered in D:\xampp\htdocs\2017\test.php on line 12

Warning: A non-numeric value encountered in D:\xampp\htdocs\2017\test.php on line 12

Warning: A non-numeric value encountered in D:\xampp\htdocs\2017\test.php on line 12

Warning: A non-numeric value encountered in D:\xampp\htdocs\2017\test.php on line 12

Warning: A non-numeric value encountered in D:\xampp\htdocs\2017\test.php on line 12

Warning: A non-numeric value encountered in D:\xampp\htdocs\2017\test.php on line 12

Warning: A non-numeric value encountered in D:\xampp\htdocs\2017\test.php on line 12

Warning: A non-numeric value encountered in D:\xampp\htdocs\2017\test.php on line 12

Singlecliq:



<?php

require 'config.php';

$january=mysqli_query($con,'SELECT staffid, saving_ded, total_ded
FROM january
UNION ALL
SELECT staffid, saving_ded, total_ded
FROM february
UNION ALL
SELECT staffid, saving_ded, total_ded
FROM march
');

//setting up as an array
$month = array();

//looping through and saving as an array
while ($row = mysql_fetch_assoc($january)) $month[] = $row;

//setup a new array after loop

$result = array();

foreach ($month as $val) {

if (!isset($result[$val['staffid']]))

$result[$val['staffid']] = $val;

else

$result[$val['staffid']]['saving_ded'] += $val['saving_ded'];
}

$result = array_values($result);

// reindex array

Re: Please Help Look At My Code (what is wrong with my code ) by Singlecliq: 8:59am On Jan 18, 2018
nicolas247:
Thanks but i still got this error

Warning: A non-numeric value encountered in D:\xampp\htdocs\2017\test.php on line 12

Warning: A non-numeric value encountered in D:\xampp\htdocs\2017\test.php on line 12

Warning: A non-numeric value encountered in D:\xampp\htdocs\2017\test.php on line 12

Warning: A non-numeric value encountered in D:\xampp\htdocs\2017\test.php on line 12

Warning: A non-numeric value encountered in D:\xampp\htdocs\2017\test.php on line 12

Warning: A non-numeric value encountered in D:\xampp\htdocs\2017\test.php on line 12

Warning: A non-numeric value encountered in D:\xampp\htdocs\2017\test.php on line 12

Warning: A non-numeric value encountered in D:\xampp\htdocs\2017\test.php on line 12

Warning: A non-numeric value encountered in D:\xampp\htdocs\2017\test.php on line 12

Warning: A non-numeric value encountered in D:\xampp\htdocs\2017\test.php on line 12

Warning: A non-numeric value encountered in D:\xampp\htdocs\2017\test.php on line 12

Warning: A non-numeric value encountered in D:\xampp\htdocs\2017\test.php on line 12

Warning: A non-numeric value encountered in D:\xampp\htdocs\2017\test.php on line 12

Warning: A non-numeric value encountered in D:\xampp\htdocs\2017\test.php on line 12

Warning: A non-numeric value encountered in D:\xampp\htdocs\2017\test.php on line 12

Warning: A non-numeric value encountered in D:\xampp\htdocs\2017\test.php on line 12

Warning: A non-numeric value encountered in D:\xampp\htdocs\2017\test.php on line 12

Warning: A non-numeric value encountered in D:\xampp\htdocs\2017\test.php on line 12

Warning: A non-numeric value encountered in D:\xampp\htdocs\2017\test.php on line 12

Warning: A non-numeric value encountered in D:\xampp\htdocs\2017\test.php on line 12

Warning: A non-numeric value encountered in D:\xampp\htdocs\2017\test.php on line 12

Warning: A non-numeric value encountered in D:\xampp\htdocs\2017\test.php on line 12

Warning: A non-numeric value encountered in D:\xampp\htdocs\2017\test.php on line 12

Warning: A non-numeric value encountered in D:\xampp\htdocs\2017\test.php on line 12

Warning: A non-numeric value encountered in D:\xampp\htdocs\2017\test.php on line 12

Warning: A non-numeric value encountered in D:\xampp\htdocs\2017\test.php on line 12


This is an error on php 7.

Paste code on line 12?
Re: Please Help Look At My Code (what is wrong with my code ) by benjibabs(m): 6:19am On Jan 19, 2018
Have you resolved it?

You should also provide details of what your code should achieve and not the output only.

From the code you posted, I think the result you are expecting is the sum of saving_ded, total_ded from Jan, feb & mar tables for each staffid. Is this accurate? If it is, your code is not complete yet. What your code is returning as it is is a 2 level array with all rows from each table as arrays. You need to find a way to select each staffid and corresponding xyz_ded values and add up the values, save them in another array and them echo them as needed. You may also be able to use MySQL statements to sum up the values before retrieving with php. I’ll try my hands on it later today and post my codes.

Happy coding ! cheesy
Re: Please Help Look At My Code (what is wrong with my code ) by benjibabs(m): 6:05pm On Jan 19, 2018
Here you go:

Note:
I used PDO instead of mysqli to connect to mysql database. However, the logic will still work so far you have the data in a multilevel array after using mysqli.

// 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';
$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
$sql1 = 'select staffid FROM january';
$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;
$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'];
$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>";
}



Depending on the number of columns and tables you are dealing with, the solution might not be efficient.

Happy coding! grin









benjibabs:
Have you resolved it?

You should also provide details of what your code should achieve and not the output only.

From the code you posted, I think the result you are expecting is the sum of saving_ded, total_ded from Jan, feb & mar tables for each staffid. Is this accurate? If it is, your code is not complete yet. What your code is returning as it is is a 2 level array with all rows from each table as arrays. You need to find a way to select each staffid and corresponding xyz_ded values and add up the values, save them in another array and them echo them as needed. You may also be able to use MySQL statements to sum up the values before retrieving with php. I’ll try my hands on it later today and post my codes.

Happy coding ! cheesy

Re: Please Help Look At My Code (what is wrong with my code ) by LegendsCoded(m): 7:34pm On Jan 19, 2018
benjibabs:
Here you go:

Note:
I used PDO instead of mysqli to connect to mysql database. However, the logic will still work so far you have the data in a multilevel array after using mysqli.

// 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';
$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
$sql1 = 'select staffid FROM january';
$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;
$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'];
$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>";
}



Depending on the number of columns and tables you are dealing with, the solution might not be efficient.

Happy coding! grin










Best answer ever
Re: Please Help Look At My Code (what is wrong with my code ) by nicolas247(m): 3:38pm On Jan 22, 2018
You are the bomb
thanks so much
problem solved though i am finding it difficult to add a column with staff's name.

benjibabs:
Here you go:

Note:
I used PDO instead of mysqli to connect to mysql database. However, the logic will still work so far you have the data in a multilevel array after using mysqli.

// 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';
$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
$sql1 = 'select staffid FROM january';
$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;
$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'];
$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>";
}



Depending on the number of columns and tables you are dealing with, the solution might not be efficient.

Happy coding! grin









Re: Please Help Look At My Code (what is wrong with my code ) by benjibabs(m): 7:44pm On Jan 22, 2018
nicolas247:
You are the bomb
thanks so much
problem solved though i am finding it difficult to add a column with staff's name.


You are welcome! I'm glad that got you off the hook. grin

You can easily add the column for the staff name by pulling the column with the names along with the staffid as below:

. . .

// Pull a list of all the staffid & Staff_name from one of the table assuming we have the same number of IDs in all tables
$sql1 = 'select staffid, staff_name FROM january';
$query = $pdo->prepare($sql1);
$query->execute();
$staffIdList = $query->fetchAll();

and then echo it out as below with the other columns:
. . .

// print out the values after each staffid and staff_name iteration
echo $staffIdList['staffid']." ".$staffIdList['staff_name']." ".$loanamt." ".$saving." ".$total."</br>";

change staff_name to the name of the column where you have the staff names.

Happy coding! smiley
Re: Please Help Look At My Code (what is wrong with my code ) by nicolas247(m): 3:28am On Jan 23, 2018
Beautiful. it work perfect
thanks real good. i made some little adjustment i will post my final code below

benjibabs:


You are welcome! I'm glad that got you off the hook. grin

You can easily add the column for the staff name by pulling the column with the names along with the staffid as below:

. . .

// Pull a list of all the staffid & Staff_name from one of the table assuming we have the same number of IDs in all tables
$sql1 = 'select staffid, staff_name FROM january';
$query = $pdo->prepare($sql1);
$query->execute();
$staffIdList = $query->fetchAll();

and then echo it out as below with the other columns:
. . .

// print out the values after each staffid and staff_name iteration
echo $staffIdList['staffid']." ".$staffIdList['staff_name']." ".$loanamt." ".$saving." ".$total."</br>";

change staff_name to the name of the column where you have the staff names.

Happy coding! smiley

(1) (2) (Reply)

I Got Scammed!!! Devs Shine Your Eyes... / Create A Website Like Nairaland In 3minutes & Start Making Tons Of Money Online / 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. 74
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.