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
cheatboy00
Forum Contributor
Posts: 151 Joined: Sat Jun 29, 2002 10:36 am
Location: canada
Contact:
Post
by cheatboy00 » Wed Jul 03, 2002 3:34 am
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 ї $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 ї $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 ї $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
RandomEngy
Forum Contributor
Posts: 173 Joined: Wed Jun 26, 2002 3:24 pm
Contact:
Post
by RandomEngy » Wed Jul 03, 2002 9:22 am
Yeah, foreach expects more than one argument. Use foreach like this:
Code: Select all
foreach($array as $value) { statements; }
It will loop through the array giving $value an element of $array on each pass.
cheatboy00
Forum Contributor
Posts: 151 Joined: Sat Jun 29, 2002 10:36 am
Location: canada
Contact:
Post
by cheatboy00 » Wed Jul 03, 2002 12:11 pm
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
hob_goblin
Forum Regular
Posts: 978 Joined: Sun Apr 28, 2002 9:53 pm
Contact:
Post
by hob_goblin » Wed Jul 03, 2002 12:27 pm
once you get the totals, add them all up, and then divide the originals into them
cheatboy00
Forum Contributor
Posts: 151 Joined: Sat Jun 29, 2002 10:36 am
Location: canada
Contact:
Post
by cheatboy00 » Wed Jul 03, 2002 12:32 pm
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
hob_goblin
Forum Regular
Posts: 978 Joined: Sun Apr 28, 2002 9:53 pm
Contact:
Post
by hob_goblin » Wed Jul 03, 2002 12:38 pm
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 » Wed Jul 03, 2002 12:43 pm
I think I get what you are asking for. Instead of the foreach loops try to use something like this:
mysql_num_rows will return the number of rows in the result. Is this what you wanted?
cheatboy00
Forum Contributor
Posts: 151 Joined: Sat Jun 29, 2002 10:36 am
Location: canada
Contact:
Post
by cheatboy00 » Wed Jul 03, 2002 12:52 pm
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){
15 $onepre = 0;
16 } else {
17 $onepre = $len1 / ($len1 + $len2 + $len3)* 100;
18 }
19
20 if ($len2 = 0){
21 $twopre = 0;
22 } else {
23 $twopre = $len2 / ($len1 + $len2 + $len3) * 100;
24 }
25
26 if ($len3 = 0){
27 $threepre = 0;
28 } else {
29 $threepre = $len3 / ($len1 + $len2 + $len3) * 100;
30 }
arg this is a kick in the pants
protokol
Forum Contributor
Posts: 353 Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:
Post
by protokol » Wed Jul 03, 2002 12:54 pm
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
llimllib
Moderator
Posts: 466 Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD
Post
by llimllib » Wed Jul 03, 2002 1:01 pm
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 » Wed Jul 03, 2002 1:15 pm
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.
protokol
Forum Contributor
Posts: 353 Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:
Post
by protokol » Wed Jul 03, 2002 3:03 pm
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
llimllib
Moderator
Posts: 466 Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD
Post
by llimllib » Wed Jul 03, 2002 3:24 pm
i didn't miss it, just saw that you had already pointed it out
protokol
Forum Contributor
Posts: 353 Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:
Post
by protokol » Wed Jul 03, 2002 3:28 pm
ok, just thought i'd clarify .. thanks
Galahad
Forum Contributor
Posts: 111 Joined: Fri Jun 14, 2002 5:50 pm
Post
by Galahad » Wed Jul 03, 2002 3:45 pm
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.