Page 1 of 2

poll script problems

Posted: Wed Jul 03, 2002 3:34 am
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

Posted: Wed Jul 03, 2002 9:22 am
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.

Posted: Wed Jul 03, 2002 12:11 pm
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

Posted: Wed Jul 03, 2002 12:27 pm
by hob_goblin
once you get the totals, add them all up, and then divide the originals into them

Posted: Wed Jul 03, 2002 12:32 pm
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

Posted: Wed Jul 03, 2002 12:38 pm
by hob_goblin
so just have some if() statements and say "if" it's 0.. dont do the math?

Posted: Wed Jul 03, 2002 12:43 pm
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?

Posted: Wed Jul 03, 2002 12:52 pm
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

Posted: Wed Jul 03, 2002 12:54 pm
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

Posted: Wed Jul 03, 2002 1:01 pm
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");

Posted: Wed Jul 03, 2002 1:15 pm
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.

Posted: Wed Jul 03, 2002 3:03 pm
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

Posted: Wed Jul 03, 2002 3:24 pm
by llimllib
i didn't miss it, just saw that you had already pointed it out

Posted: Wed Jul 03, 2002 3:28 pm
by protokol
ok, just thought i'd clarify .. thanks

Posted: Wed Jul 03, 2002 3:45 pm
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.