Jump to content

PHP increment problem


Urbley

Recommended Posts

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. ";
?>
Link to comment
Share on other sites

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
Link to comment
Share on other sites

[!--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 again

Steve
[/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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.