Jump to content


Photo

PHP increment problem


  • Please log in to reply
4 replies to this topic

#1 Urbley

Urbley
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 23 April 2006 - 11:29 AM

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. ";
?>

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 23 April 2006 - 02:40 PM

You code can be greatly simplified:

<?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. ";
?>

Ken

#3 Urbley

Urbley
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 23 April 2006 - 05:29 PM

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

#4 mb81

mb81
  • Members
  • PipPipPip
  • Advanced Member
  • 120 posts

Posted 23 April 2006 - 06:03 PM

[!--quoteo(post=367726:date=Apr 23 2006, 01:29 PM:name=Urbley)--][div class=\'quotetop\']QUOTE(Urbley @ Apr 23 2006, 01:29 PM) View Post[/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.

#5 Urbley

Urbley
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 23 April 2006 - 06:13 PM

LMFAO.... its been a long easter of nothing but coursework. Thanks for the heads up and quick reply.

*KICKING MYSELF*

Steve




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users