hbalagh Posted August 13, 2007 Share Posted August 13, 2007 I've been searching here but I'm not sure i can do this w/o someone's help so im going to go ahead and ask AGAIN... I'm sure that this is the last time lol I just realized that not only do i want the total amount of #'s of votes to show up in my results for my poll but I want a percentage to display as well here is my code <?php include('database.php'); if(mysql_num_rows(mysql_query("SELECT * FROM ips WHERE ip='".$_SERVER['REMOTE_ADDR']."' LIMIT 1"))) { $query="SELECT * FROM results2 ORDER BY id DESC LIMIT 1"; $rez=mysql_query($query); $row=mysql_fetch_array($rez); print " <h3 style='text-align: center'>Museum Park Opinion Poll</h3> <p style='text-align: center'>Results up to date</p><font size='-2'> <ul style='list-style-type: none; margin:5px; padding:5px;font-size:12px;'> <li>Swimming Pool - {$row['a']}</li> <li>Fenced Dog Run - {$row['b']}</li> <li>Batting Cages - {$row['c']}</li> <li>Driving Range Cages - {$row['d']}</li> <li>Putting Green - {$row['e']}</li> <li>Putt Putt Golf - {$row['f']}</li> <li>Splash Gardens & Dancing Fountains - {$row['g']}</li> <li>Street Performers - {$row['h']}</li> <li>Soccer Field (includes touch football & rugby use) - {$row['i']}</li> <li>Cycling Track - {$row['j']}</li> <li>BMX Bicycle Course - {$row['k']}</li> <li>Skate Board Park - {$row['l']}</li> <li>Community Center - {$row['m']}</li> <li>Checkers & Dominos Tables - {$row['n']}</li> <li>Duck Pond - {$row['o']}</li> <li>Gazebo - {$row['p']}</li> <li>Music Stand - {$row['q']}</li> <li>Bar-B-Que Grills & Picnic Areas (at waters edge) - {$row['r']}</li> <li>Horse Shoe Pits - {$row['s']}</li> <li>Volley Ball Nets - {$row['t']}</li> <li>Swing Sets, Monkey Bars, etc.{$row['u']}</li> <li>Police / First Aid / Hydration Station - {$row['v']}</li> <li>Snack & Ice Cream Vendors - {$row['w']}</li></ul> ";} else{ ?> <SCRIPT LANGUAGE="javascript"> function KeepCount() { var NewCount = 0 if (document.form1.a.checked) {NewCount = NewCount + 1} if (document.form1.b.checked) {NewCount = NewCount + 1} if (document.form1.c.checked) {NewCount = NewCount + 1} if (document.form1.d.checked) {NewCount = NewCount + 1} if (document.form1.e.checked) {NewCount = NewCount + 1} if (document.form1.f.checked) {NewCount = NewCount + 1} if (document.form1.g.checked) {NewCount = NewCount + 1} if (document.form1.h.checked) {NewCount = NewCount + 1} if (document.form1.i.checked) {NewCount = NewCount + 1} if (document.form1.j.checked) {NewCount = NewCount + 1} if (document.form1.k.checked) {NewCount = NewCount + 1} if (document.form1.l.checked) {NewCount = NewCount + 1} if (document.form1.m.checked) {NewCount = NewCount + 1} if (document.form1.n.checked) {NewCount = NewCount + 1} if (document.form1.o.checked) {NewCount = NewCount + 1} if (document.form1.p.checked) {NewCount = NewCount + 1} if (document.form1.q.checked) {NewCount = NewCount + 1} if (document.form1.r.checked) {NewCount = NewCount + 1} if (document.form1.s.checked) {NewCount = NewCount + 1} if (document.form1.t.checked) {NewCount = NewCount + 1} if (document.form1.u.checked) {NewCount = NewCount + 1} if (document.form1.v.checked) {NewCount = NewCount + 1} if (document.form1.w.checked) {NewCount = NewCount + 1} if (NewCount == 11) { alert('Please select up to 10 only!') document.form1; return false; } } </SCRIPT> <script type="text/javascript" language="JavaScript"> <!-- function checkCheckBoxes(form1) { if ( form1.a.checked == false && form1.b.checked == false && form1.c.checked == false && form1.d.checked == false && form1.e.checked == false && form1.f.checked == false && form1.g.checked == false && form1.h.checked == false && form1.i.checked == false && form1.j.checked == false && form1.k.checked == false && form1.l.checked == false && form1.m.checked == false && form1.n.checked == false && form1.o.checked == false && form1.p.checked == false && form1.q.checked == false && form1.r.checked == false && form1.s.checked == false && form1.t.checked == false && form1.u.checked == false && form1.v.checked == false && form1.w.checked == false) { alert ('You didn\'t choose any of the options!'); return false; } else { return true; } } //--> </script> <?php //overview $query = "SELECT * FROM polls1 ORDER BY id DESC LIMIT 1"; $rez = mysql_query($query); $row = mysql_fetch_array($rez); $id=$row['id']; print(' <h3 style="text-align: center">Museum Park Opinion Poll</h3> <p style="text-align: center">Select up to 10 amenities</p><font size="-2"> <ul style="list-style-type: none; margin:0; padding:0;font-size:12px;"> <form name="form1" id="form1" action="record.php" method="POST" onsubmit="return checkCheckBoxes(this);"> <li><input type="checkbox" name="a" onClick="return KeepCount()">'.$row['a'].'</li> <li><input type="checkbox" name="b" onClick="return KeepCount()">'.$row['b'].'</li> <li><input type="checkbox" name="c" onClick="return KeepCount()">'.$row['c'].'</li> <li><input type="checkbox" name="d" onClick="return KeepCount()">'.$row['d'].'</li> <li><input type="checkbox" name="e" onClick="return KeepCount()">'.$row['e'].'</li> <li><input type="checkbox" name="f" onClick="return KeepCount()">'.$row['f'].'</li> <li><input type="checkbox" name="g" onClick="return KeepCount()">'.$row['g'].'</li> <li><input type="checkbox" name="h" onClick="return KeepCount()">'.$row['h'].'</li> <li><input type="checkbox" name="i" onClick="return KeepCount()">'.$row['i'].'</li> <li><input type="checkbox" name="j" onClick="return KeepCount()">'.$row['j'].'</li> <li><input type="checkbox" name="k" onClick="return KeepCount()">'.$row['k'].'</li> <li><input type="checkbox" name="l" onClick="return KeepCount()">'.$row['l'].'</li> <li><input type="checkbox" name="m" onClick="return KeepCount()">'.$row['m'].'</li> <li><input type="checkbox" name="n" onClick="return KeepCount()">'.$row['n'].'</li> <li><input type="checkbox" name="o" onClick="return KeepCount()">'.$row['o'].'</li> <li><input type="checkbox" name="p" onClick="return KeepCount()">'.$row['p'].'</li> <li><input type="checkbox" name="q" onClick="return KeepCount()">'.$row['q'].'</li> <li><input type="checkbox" name="r" onClick="return KeepCount()">'.$row['r'].'</li> <li><input type="checkbox" name="s" onClick="return KeepCount()">'.$row['s'].'</li> <li><input type="checkbox" name="t" onClick="return KeepCount()">'.$row['t'].'</li> <li><input type="checkbox" name="u" onClick="return KeepCount()">'.$row['u'].'</li> <li><input type="checkbox" name="v" onClick="return KeepCount()"> Police / First Aid / Hydration Station</li> <li><input type="checkbox" name="w" onClick="return KeepCount()">'.$row['w'].'</li> </ul> <br /><center> <input type="submit" name="action" value="VOTE!"> </center> </form><br /></font> '); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/64704-solved-percentage/ Share on other sites More sharing options...
Barand Posted August 13, 2007 Share Posted August 13, 2007 Get the total of all the values first in, say, $totalvotes. Then swimming pool pcent is $row['a'] * 100 / $totalvotes. And so on. Quote Link to comment https://forums.phpfreaks.com/topic/64704-solved-percentage/#findComment-322671 Share on other sites More sharing options...
hbalagh Posted August 13, 2007 Author Share Posted August 13, 2007 thanks, i just realized too that i forgot to add the other part of the script (the record.php) can you assist me with how to go about getting the total of all the results include('database.php'); $query = "SELECT * FROM polls1 ORDER BY id DESC LIMIT 1"; $rez = mysql_query($query); $row = mysql_fetch_array($rez); $id=$row['id']; if(isset($_POST['action'])) { $a = empty($_POST['a']) ? 0 : 1; $b = empty($_POST['b']) ? 0 : 1; $c = empty($_POST['c']) ? 0 : 1; $d = empty($_POST['d']) ? 0 : 1; $e = empty($_POST['e']) ? 0 : 1; $f = empty($_POST['f']) ? 0 : 1; $g = empty($_POST['g']) ? 0 : 1; $h = empty($_POST['h']) ? 0 : 1; $i = empty($_POST['i']) ? 0 : 1; $j = empty($_POST['j']) ? 0 : 1; $k = empty($_POST['k']) ? 0 : 1; $l = empty($_POST['l']) ? 0 : 1; $m = empty($_POST['m']) ? 0 : 1; $n = empty($_POST['n']) ? 0 : 1; $o = empty($_POST['o']) ? 0 : 1; $p = empty($_POST['p']) ? 0 : 1; $q = empty($_POST['q']) ? 0 : 1; $r = empty($_POST['r']) ? 0 : 1; $s = empty($_POST['s']) ? 0 : 1; $t = empty($_POST['t']) ? 0 : 1; $u = empty($_POST['u']) ? 0 : 1; $v = empty($_POST['v']) ? 0 : 1; $w = empty($_POST['w']) ? 0 : 1; $x = empty($_POST['x']) ? 0 : 1; if ($a + $b + $c + $d + $e + $f + $g + $h + $i + $j + $k + $l + $m + $n + $o + $p + $q + $r + $s + $t + $u + $v + $w + $x > 0) { $query = "UPDATE results2 SET a = a + %x, b = b + %x, c = c + %x, d = d + %x, e = e + %x, f = f + %x, g = g + %x, h = h + %x, i = i + %x, j = j + %x, k = k + %x, l = l + %x, m = m + %x, n = n + %x, o = o + %x, p = p + %x, q = q + %x, r = r + %x, s = s + %x, t = t + %x, u = u + %x, v = v + %x, w = w + %x, x = x + %x WHERE id = %x"; mysql_query(sprintf($query, $a, $b, $c, $d, $e, $f, $g, $h, $i, $j, $k, $l, $m, $n, $o, $p, $q, $r, $s, $t, $u, $v, $w, $x, $id)); mysql_query("INSERT INTO ips (ip) VALUES ('".$_SERVER['REMOTE_ADDR']."')"); } } if(mysql_num_rows(mysql_query("SELECT * FROM ips WHERE ip='".$_SERVER['REMOTE_ADDR']."' LIMIT 1"))) { header("Location: index.php"); exit; } Quote Link to comment https://forums.phpfreaks.com/topic/64704-solved-percentage/#findComment-322681 Share on other sites More sharing options...
Barand Posted August 13, 2007 Share Posted August 13, 2007 You might try adding them $totalvotes = $row['a'] + $row['b'] + .... + $row['x']; Quote Link to comment https://forums.phpfreaks.com/topic/64704-solved-percentage/#findComment-322695 Share on other sites More sharing options...
hbalagh Posted August 13, 2007 Author Share Posted August 13, 2007 thank you... now one last thing how can i have it round it off so it doesn't say 4.34782608696 % Quote Link to comment https://forums.phpfreaks.com/topic/64704-solved-percentage/#findComment-322732 Share on other sites More sharing options...
hbalagh Posted August 13, 2007 Author Share Posted August 13, 2007 nevermind got it thanks Quote Link to comment https://forums.phpfreaks.com/topic/64704-solved-percentage/#findComment-322744 Share on other sites More sharing options...
Barand Posted August 13, 2007 Share Posted August 13, 2007 $pcent = 4.34782608696; echo number_format ($pcent, 2); // 4.35 So when you output the poll results echo "<li>Swimming Pool - {$row['a']} - " . number_format($row['a']*100/$totalvotes, 2) . "%</li> "; Method 2 printf ("<li>Swimming Pool - %d - %0.2f%% </li>", $row['a'], $row['a']*100/$totalvotes); Quote Link to comment https://forums.phpfreaks.com/topic/64704-solved-percentage/#findComment-322746 Share on other sites More sharing options...
akitchin Posted August 13, 2007 Share Posted August 13, 2007 as an addendum, something i'm sure you discovered yourself: if you're asking how PHP can do a certain action, search for a function with the appropriate verb in the name (for example here, round()). chances are there's a function tailor-made for it. PHP manual is a friend. Quote Link to comment https://forums.phpfreaks.com/topic/64704-solved-percentage/#findComment-322750 Share on other sites More sharing options...
hbalagh Posted August 13, 2007 Author Share Posted August 13, 2007 i got it with $perca=round($row['a'] * 100 / $totalvotes); then displayed it like <li>Swimming Pool - {$row['a']} - $perca %</li> Quote Link to comment https://forums.phpfreaks.com/topic/64704-solved-percentage/#findComment-322754 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.