Urbley Posted April 23, 2006 Share Posted April 23, 2006 Ok basically i'm doing an assignment for Uni. A survey website which records votes for books and displays some results. Statistics, graphs etc. I'm not new to PHP so this is really breaking my heart because its probably something very very simple. Here is the code.Ok you can see very simply what is going on here. Nothing too complicated. It is outputting the correct value for the first pass where it collects all the votes in the table. But when I move along to collecting the votes for the separate genres it's giving me 0. I tried using an echo after the "$votes=$arrayVar["votes"]; and it echo'd the correct value but when I placed the echo after the $totalHorrorVotes=$totalHorrorVotes+$votes line it didnt echo anything. I hope someone can help because i've been staring at this for 1.5 hours now! Thanks in advance to anyone how has a minute.Steve<?php$db = mysql_connect("********", "********", "*******");mysql_select_db("********",$db);$dbQuery="SELECT * FROM survey";$result = mysql_query($dbQuery,$db);$totalVotes = 0; while ($arrayVar=mysql_fetch_array($result)) { $votes=$arrayVar["votes"]; $totalVotes=$totalVotes+$votes; } echo "There were a total of <strong>". $totalVotes ."</strong> votes. ";$dbQuery="SELECT * FROM survey WHERE title='Horror'";$result = mysql_query($dbQuery,$db);$totalHorrorVotes=0; while ($arrayVar=mysql_fetch_array($result)) { $votes=$arrayVar["votes"]; $totalHorrorVotes = $totalHorrorVotes + $votes;}echo "Out of all of these votes <strong>". $totalHorrorVotes ."</strong> were votes for books in the Horror genre. ";$dbQuery="SELECT * FROM survey WHERE title='Computers & internet'";$result = mysql_query($dbQuery,$db);$totalComputerVotes = 0; while ($arrayVar=mysql_fetch_array($result)) { $votes=$arrayVar["votes"]; $totalComputerVotes + $votes; }echo "There were a total of <strong>". $totalComputerVotes ."</strong> votes for books in the Computers & Internet genre. ";?> Quote Link to comment https://forums.phpfreaks.com/topic/8170-php-increment-problem/ Share on other sites More sharing options...
kenrbnsn Posted April 23, 2006 Share Posted April 23, 2006 You code can be greatly simplified:[code]<?php$db = mysql_connect("********", "********", "*******");mysql_select_db("********",$db);$dbQuery="SELECT sum(votes) as totalvotes FROM survey";$result = mysql_query($dbQuery,$db);$rw = mysql_fetch_assoc($result);$totalvotes = $rw['totalvotes'];echo "There were a total of <strong>". $totalVotes ."</strong> votes. ";$dbQuery="SELECT sum(votes) as totalvotes FROM survey WHERE title='Horror'";$result = mysql_query($dbQuery,$db);$rw = mysql_fetch_assoc($result);$totalHorrorVotes = $rw['totalvotes'];echo "Out of all of these votes <strong>". $totalHorrorVotes ."</strong> were votes for books in the Horror genre. ";$dbQuery="SELECT sum(votes) as totalvotes FROM survey WHERE title='Computers & internet'";$result = mysql_query($dbQuery,$db);$result = mysql_query($dbQuery,$db);$rw = mysql_fetch_assoc($result);$totalComputerVotes = $rw['totalvotes'];echo "There were a total of ". $totalComputerVotes ." votes for books in the Computers & Internet genre. ";?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/8170-php-increment-problem/#findComment-29818 Share on other sites More sharing options...
Urbley Posted April 23, 2006 Author Share Posted April 23, 2006 Thanks for the quick reply, This is simpler but i'm having the exact same problem. It's only returning the first value for total votes and not the individual genre vote counts. This is very weird..thanks againSteve Quote Link to comment https://forums.phpfreaks.com/topic/8170-php-increment-problem/#findComment-29853 Share on other sites More sharing options...
mb81 Posted April 23, 2006 Share Posted April 23, 2006 [!--quoteo(post=367726:date=Apr 23 2006, 01:29 PM:name=Urbley)--][div class=\'quotetop\']QUOTE(Urbley @ Apr 23 2006, 01:29 PM) [snapback]367726[/snapback][/div][div class=\'quotemain\'][!--quotec--]Thanks for the quick reply, This is simpler but i'm having the exact same problem. It's only returning the first value for total votes and not the individual genre vote counts. This is very weird..thanks againSteve[/quote]OK, I know this may be a silly question, but wouldn't title be the title of the book and there be another column like category or genre?That aside, I'm not going mention the fact that neither the title or the genre should be in the votes table, it shold be in the books table. Quote Link to comment https://forums.phpfreaks.com/topic/8170-php-increment-problem/#findComment-29864 Share on other sites More sharing options...
Urbley Posted April 23, 2006 Author Share Posted April 23, 2006 LMFAO.... its been a long easter of nothing but coursework. Thanks for the heads up and quick reply.*KICKING MYSELF*Steve Quote Link to comment https://forums.phpfreaks.com/topic/8170-php-increment-problem/#findComment-29867 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.