Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,472 members, 7,808,702 topics. Date: Thursday, 25 April 2024 at 03:39 PM

Please Help Me Look At My Code - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Please Help Me Look At My Code (1211 Views)

Php Gurus Help Me Out: What Is Wrong With My Code?? / Please Help Me Look At My Code / Please Help Me Check Whats Wrong With My Code(c Language) (2) (3) (4)

(1) (Reply) (Go Down)

Please Help Me Look At My Code by nicolas247(m): 8:24am On Apr 20, 2015
public function GetSum($total_ded){
$db=new Database();
$conn=$db->Connect_DB();
if($conn){
$query = "SELECT SUM(total_ded) FROM february";

$result=mysqli_query($conn, $query);
if($result){
$sum=mysqli_fetch_array($result);
return $sum[0];
}else{
return false;
}
}else{
return false;
}
}


my question is how do i display this code in a table and connect it to database

I am trying to sum up a column....

Pls any help is appreciated
thank in advance
Re: Please Help Me Look At My Code by Adesege(m): 8:37am On Apr 20, 2015
Mysqli_query first parameter takes the connection handle while the second parameter takes your SQL query.

To answer your question, are you trying to display all the results from the query or just the sum?
Re: Please Help Me Look At My Code by Urine: 8:39am On Apr 20, 2015
nicolas247:
public function GetSum($total_ded){
$db=new Database();
$conn=$db->Connect_DB();
if($conn){
$query = "SELECT SUM(total_ded) FROM february";

$result=mysqli_query($conn, $query);
if($result){
$sum=mysqli_fetch_array($result);
return $sum[0];
}else{
return false;
}
}else{
return false;
}
}


my question is how do i display this code in a table and connect it to database

I am trying to sum up a column....

Pls any help is appreciated
thank in advance

Echo the results in a html table , Google it to see examples.

Modified: Check out this link, it should give you an idea.

http://stackoverflow.com/questions/14430174/use-php-to-display-mysql-results-in-html-table
Re: Please Help Me Look At My Code by Adesege(m): 8:44am On Apr 20, 2015
It's a bad practice to echo result from a function, rather use return.

Urine:


Echo the results in a html table , Google it to see examples.
Re: Please Help Me Look At My Code by Urine: 9:57am On Apr 20, 2015
Adesege:
It's a bad practice to echo result from a function, rather use return.


Baba, all I did was to give him an idea of how to go about it, did you see the "Google" part? It was for him to get his search right. If you feel you have a better answer, help the young man out.
Re: Please Help Me Look At My Code by Adesege(m): 10:03am On Apr 20, 2015
No p na. It'll be useful for any who views it later.

Urine:


Baba, all I did was to give him an idea of how to go about it, did you see the "Google" part? It was for him to get his search right. If you feel you have a better answer, help the young man out.
Re: Please Help Me Look At My Code by nicolas247(m): 11:05am On Apr 20, 2015
Thank you all for your earnest response but I have done all I could even Google all but still can't figure it.... Pls I will appreciate a code that can display this function..
Re: Please Help Me Look At My Code by kudaisi(m): 11:14am On Apr 20, 2015
I take it you've gotten all the database connections part right so I'm going to leave that part as is. Rather than using an SLQ SUM in your query it makes more sense do an SQL SELECT of the required row (in this case total_ded) and return the values as an array. You can then Loop through the array and display the necessary table. Finally sum the array and equally display the sum. Here's what the code will look like
<?php public funtion get_all_ded()
$db=new Database();
$conn=$db->Connect_DB();
if($conn){
$query = "SELECT total_ded FROM february";
$result=mysqli_query($conn, $query);
if($result){
$deds=mysqli_fetch_array($result);
return $deds;
}else{
return false;
}
}else{
return false;
}
}

//
$all_deds = get_all_deds();
?>
<table>
<tr><td>Deds</td></tr>
<?php
foreach($all_deds as $ded){
echo "<tr><td>".$ded."</td></tr>"
}
?>
</table>
<div>Total Deds: <?php echo array_sum($all_deds) ?></div>
Re: Please Help Me Look At My Code by jacob05(m): 5:24pm On Apr 20, 2015
kudaisi:
I take it you've gotten all the database connections part right so I'm going to leave that part as is. Rather than using an SLQ SUM in your query it makes more sense do an SQL SELECT of the required row (in this case total_ded) and return the values as an array. You can then Loop through the array and display the necessary table. Finally sum the array and equally display the sum. Here's what the code will look like
<?php public funtion get_all_ded()
$db=new Database();
$conn=$db->Connect_DB();
if($conn){
$query = "SELECT total_ded FROM february";
$result=mysqli_query($conn, $query);
if($result){
$deds=mysqli_fetch_array($result);
return $deds;
}else{
return false;
}
}else{
return false;
}
}

//
$all_deds = get_all_deds();
?>
<table>
<tr><td>Deds</td></tr>
<?php
foreach($all_deds as $ded){
echo "<tr><td>".$ded."</td></tr>"
}
?>
</table>
<div>Total Deds: <?php echo array_sum($all_deds) ?></div>


hmm..nice..but won't use a foreach on a large dataset (performance wise ) and would check falsity of $all_feds before displaying the table
Re: Please Help Me Look At My Code by kudaisi(m): 8:30pm On Apr 20, 2015
jacob05:



hmm..nice..but won't use a foreach on a large dataset (performance wise ) and would check falsity of $all_feds before displaying the table
Agreed, but I'm not trying to build his entire application for him. I only meant for the code to be used as guide. But it's worth mentioning.
Re: Please Help Me Look At My Code by Kingsonly: 10:17pm On May 07, 2015
Mysqli_fetch_array() requires two argument and not one
Re: Please Help Me Look At My Code by saviolamajic(m): 1:57am On May 08, 2015
Hello everyone, Please I need your
support so please go to >>> http:///votesaviour and
please vote for me in the Microsoft #YouthSpark Challenge for change. I'm one of the two Nigerian students among the 15 global finalists. Please help me make my dream of changing the world with science and technology become true. It won't take up to 10 seconds so please stop whatever your doing now and vote for me please. I really appreciate. It will go a long way in actualizing my career. Please vote>>> http:///votesaviour

#youthspark #youthsparkchallenge #microsoftafrica #microsoft #challengeforchange

(1) (Reply)

Programming For Kids & Teenagers. / I Need Your Help And Idea. / Urgent Need Of A Programmer

(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. 21
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.