poll script problems

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
cheatboy00
Forum Contributor
Posts: 151
Joined: Sat Jun 29, 2002 10:36 am
Location: canada
Contact:

poll script problems

Post by cheatboy00 »

i'm having a damn hard time trying to get my numbers working on my poll. and also my divders wont show up either... the way i was doing it was probably the worst way possible but any help would be greatly thanked............

(note: i already have the connected part done)

Code: Select all

$vq1 = mysql_query("SELECT * FROM poll WHERE vote = 1");
$vq2 = mysql_query("SELECT * FROM poll WHERE vote = 2");
$vq3 = mysql_query("SELECT * FROM poll WHERE vote = 3");

foreach ($vq1){
   $len1++;}

foreach ($vq2){
   $len2++;}

foreach ($vq3){
   $len3++;}

if ($len1 = 0){
   $onepre = 0;} 
else {
   $onepre = $len1 / ($len1 + $len2 + $len3)* 100;}

if ($len2 = 0){
   $twopre = 0;} 
else {
   $twopre = $len2 / ($len1 + $len2 + $len3) * 100;}

if ($len3 = 0){
   $threepre = 0;} 
else {
   $threepre = $len3 / ($len1 + $len2 + $len3) * 100;}

echo "<table border=1  bordercolor=#444444 cellspacing=0 cellpadding=0 cols=2 width=100% >";
   echo "<tr><td align=center colspan=2>";
      echo "<font class=main>What do you think of the Layout?</font>";
   echo "</td></tr>";

   echo "<tr><td>";
      echo "<font class=main>It is nice &#1111; $onepre% ]</font>";
   echo "</td></tr>";
   echo "<tr><td align=left>";
      echo "<img src=pics/divder1.gif width=$onepre%>";
   echo "</td></tr>";

   echo "<tr><td>";
      echo "<font class=main>It sucks I have seen it on other sites &#1111; $twopre% ]</font>";
   echo "</td></tr>";
   echo "<tr><td align=center>";
      echo "<img src=pics/divder2.gif width=$twopre%>";
   echo "</td></tr>";

   echo "<tr><td>";
      echo "<font class=main>I don't know what I think &#1111; $threepre% ]</font>";
   echo "</TD></tr>";
   echo "<tr><td align=center>";
      echo "<img src=pics/divder3.gif width=$threepre3%>";
   echo "</td></tr>";

echo "</table>";
and the error i get is unexpected ) on line 5 (or this code i am shoing you)

help please i'm deaspreate
User avatar
RandomEngy
Forum Contributor
Posts: 173
Joined: Wed Jun 26, 2002 3:24 pm
Contact:

Post by RandomEngy »

Yeah, foreach expects more than one argument. Use foreach like this:

Code: Select all

foreach($array as $value) &#123; statements; &#125;
It will loop through the array giving $value an element of $array on each pass.
User avatar
cheatboy00
Forum Contributor
Posts: 151
Joined: Sat Jun 29, 2002 10:36 am
Location: canada
Contact:

Post by cheatboy00 »

arg.... but thats not what i wanted to do....

i want to get the number of 1's in the vote colum of the table called poll (same with 2's and 3's), then the second part was to get the % of the 1's out of all the numbers...... but it doesnt work but also i have to have that an if statement (i neeed an if statement for getting the precentage, becasue it'll just give me an error if i try and divide by zero)

this sucks i cant do anythign right
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

once you get the totals, add them all up, and then divide the originals into them
User avatar
cheatboy00
Forum Contributor
Posts: 151
Joined: Sat Jun 29, 2002 10:36 am
Location: canada
Contact:

Post by cheatboy00 »

arg. your not listening... i can do that i just need the numbers first thats whats not working.... and if the number i get is 0 its create an error saying you cant divid by zero.... so what i need again is the numbers to begin with... the stuff you gave me last night didnt work
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

so just have some if() statements and say "if" it's 0.. dont do the math?
Galahad
Forum Contributor
Posts: 111
Joined: Fri Jun 14, 2002 5:50 pm

Post by Galahad »

I think I get what you are asking for. Instead of the foreach loops try to use something like this:

Code: Select all

$len = mysql_num_rows($vq1);
mysql_num_rows will return the number of rows in the result. Is this what you wanted?
User avatar
cheatboy00
Forum Contributor
Posts: 151
Joined: Sat Jun 29, 2002 10:36 am
Location: canada
Contact:

Post by cheatboy00 »

well i think that works but.... i'm getting this error

Warning: Division by zero in /home/dragonsb/public_html/cbaa/showresults.php on line 17

Warning: Division by zero in /home/dragonsb/public_html/cbaa/showresults.php on line 23

Warning: Division by zero in /home/dragonsb/public_html/cbaa/showresults.php on line 29

Code: Select all

14   if ($len1 = 0)&#123;
15       $onepre = 0;
16    &#125; else &#123;
17       $onepre = $len1 / ($len1 + $len2 + $len3)* 100;
18    &#125;
19
20   if ($len2 = 0)&#123;
21      $twopre = 0;
22   &#125; else &#123;
23      $twopre = $len2 / ($len1 + $len2 + $len3) * 100;
24   &#125;
25
26   if ($len3 = 0)&#123;
27      $threepre = 0;
28   &#125; else &#123;
29      $threepre = $len3 / ($len1 + $len2 + $len3) * 100;
30   &#125;
arg this is a kick in the pants
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

instead of:

if ($len = 0) {}
if ($len2 = 0) {}
if ($len3 = 0) {}

Use this:

if ($len == 0) {}
if ($len2 == 0) {}
if ($len3 == 0) {}

Basically, use the comparison operator '==' as opposed to the assignment operator '=' that you are using
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

why not just let mySQL count for you:

Code: Select all

$vq1 = mysql_query("select count(*) from poll where vote = 1");
$vq2 = mysql_query("select count(*) from poll where vote = 2");
$vq3 = mysql_query("select count(*) from poll where vote = 3");
Galahad
Forum Contributor
Posts: 111
Joined: Fri Jun 14, 2002 5:50 pm

Post by Galahad »

llimllib has a good idea for the counting.

For the divide by zero, you could also try something like:

Code: Select all

$sum = $len1 + $len2 + $len3;
$onepre = (@($len1 / $sum) * 100);
The '@' makes it ignore any warnings (like divide by zero). The result of a divide by zero is null. That might be cleaner than all of the if statements. You can fiddle with the parenthesis in the divide line, if you want, this way makes it more readable for me. Just a thought though.
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

you are all missing the point that his logic is incorrect

if ($len = 0) is NOT the same thing as if ($len == 0)

He needs to change those statements to the second form using the '=='. Once he fixes this problem, THEN he can worry about counting his results
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

i didn't miss it, just saw that you had already pointed it out
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

ok, just thought i'd clarify .. thanks
Galahad
Forum Contributor
Posts: 111
Joined: Fri Jun 14, 2002 5:50 pm

Post by Galahad »

I also didn't miss it. I just thought I'd suggest an alternate method where he didn't have to worry about the '='/'==' difference (which is important to know, don't get me wrong). Take it easy, protokol.
Post Reply