cuboidgraphix Posted August 24, 2007 Share Posted August 24, 2007 Hi guys, I'm new to php and mysql and I have a lil project. My project is that I need to have a php page that will read off a mysql database and output two individual values.. For example... I will have tables that say.. Date | Hour | Tel | Amount 07.08.21 | 20:01:02 | 6701064 | 5 07.08.21 | 20:01:04 | 6701065 | 10 and so on... Now my php page needs to say simply as follows.. Thanks for Caring So far there has been 2 (# on database) donations... We have gathered an amount of $15 (amount on database) Those in bold are the two things that are to be read off the mysql database. Now I have to admit I don't have a clue on how to start... so if anyone can write me the codes I would really appreciate it... I'll fill the server stuff later. I know it must be fries to ya'll but it's like rocket science to me. Thanks again. Link to comment https://forums.phpfreaks.com/topic/66433-beginners-project-but-still-clueless-on-what-to-do/ Share on other sites More sharing options...
Fadion Posted August 24, 2007 Share Posted August 24, 2007 $query = mysql_query("SELECT * FROM table"); $rows = mysql_num_rows($query); while($values = mysql_fetch_array($query)){ $sum += $values['amount']; } echo "So far there has been $rows donations" . "<br/>"; echo "We have gathered an amount of \$$sum"; There may be easier methos with mysql commands like COUNT and SUM, but as im not very good with those and ure a beginner, try with this code. Link to comment https://forums.phpfreaks.com/topic/66433-beginners-project-but-still-clueless-on-what-to-do/#findComment-332567 Share on other sites More sharing options...
jvrothjr Posted August 24, 2007 Share Posted August 24, 2007 The other way would work but you would get a miss count if the amount was 0 or less you basicly do two queries (sure you could do with one) 1 - Count Row's with a value greater the Zero $query = "select count(*) from table where Amount > 0"; $CntRecordQuery = mysql_query($query); $Cntvar=mysql_result($CntRecordQuery,0,"count(*)"); 2 - Sum up the Amount $query = "select sum(Amount) as TotalAmount from table"; $result = mysql_query($query); $Totalamount = $result['TotalAmount']; echo "So far there has been $Cntvar donations<br>"; echo "We have gathered an amount of $Totalamount"; Link to comment https://forums.phpfreaks.com/topic/66433-beginners-project-but-still-clueless-on-what-to-do/#findComment-332572 Share on other sites More sharing options...
cuboidgraphix Posted August 24, 2007 Author Share Posted August 24, 2007 Hehehe... no offense guys, but I mean I'm like a sperm beginner. lol how do I integrate that into a php page? where I could just like copy and paste.... I mean.. Including the php codes for the page.. and the mysql codes to connect and stuff... Let me explain.. I've seen some php&mysql codes.. but don't exactly understand it. So if you could write me the whole php and mysql codes that would really help me out. Link to comment https://forums.phpfreaks.com/topic/66433-beginners-project-but-still-clueless-on-what-to-do/#findComment-332586 Share on other sites More sharing options...
Fadion Posted August 24, 2007 Share Posted August 24, 2007 Or the two codes by jvrothjr coupled in one: $query = mysql_query("SELECT COUNT(amount), SUM(amount) FROM table WHERE amount>0"); $sum = mysql_result($query, 0, "SUM(amount)"); $count = mysql_result($query, 0, "COUNT(amount)"); echo "So far there has been $count donations" . "<br/>"; echo "We have gathered an amount of \$$sum"; This kind of code may be a bit complicated for a new starter. Anyway its u chosing Link to comment https://forums.phpfreaks.com/topic/66433-beginners-project-but-still-clueless-on-what-to-do/#findComment-332591 Share on other sites More sharing options...
jvrothjr Posted August 24, 2007 Share Posted August 24, 2007 Thats not what this is for its for you to learn from there is a forum for asking some one to right the code for you. The general user don't have the time to do that for you. You posted in the help forum and this is what you got. sry maybe in the moring its bed time Link to comment https://forums.phpfreaks.com/topic/66433-beginners-project-but-still-clueless-on-what-to-do/#findComment-332593 Share on other sites More sharing options...
Fadion Posted August 24, 2007 Share Posted August 24, 2007 lol. U need some tutorials to start off, but this is a help forum for specific stuff. Try google and ull find a lot of tutorials. Im just writing the full code also with the connect: $connect = @mysql_connect('mysql server host', 'username', password') or die('cant connect); mysql_select_db('database_name'); $query = mysql_query("SELECT COUNT(amount), SUM(amount) FROM table WHERE amount>0"); $sum = mysql_result($query, 0, "SUM(amount)"); $count = mysql_result($query, 0, "COUNT(amount)"); echo "So far there has been $count donations" . "<br/>"; echo "We have gathered an amount of \$$sum"; mysql_close($connect); Adjusting the host, user, pass, database name, tables and columns to fit your data, u will end up with what u first wanted. Link to comment https://forums.phpfreaks.com/topic/66433-beginners-project-but-still-clueless-on-what-to-do/#findComment-332596 Share on other sites More sharing options...
cuboidgraphix Posted August 24, 2007 Author Share Posted August 24, 2007 Sorry I'm a lil bit too demanding guys... I do apologize. And I do appreciate all of your help. But this project was given to me at work.. and I have like a day to find out how to do it... Not part of my job description, but I guess they felt as they could count on me to get it done.. so I was imploring you guys for help.. Again, sorry for being too pushy... and thanks for all your help. Now that I've got this... they've told me to try it on PostgreSQL ... this really sucks.. I'll have to go bother some poor souls in the other sub forum.. Thanks again.. you're a life saver. Link to comment https://forums.phpfreaks.com/topic/66433-beginners-project-but-still-clueless-on-what-to-do/#findComment-333059 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.