Re: Re: Percentage...

From: Date: Tue, 30 Jul 2002 08:47:22 +0000
Subject: Re: Re: Percentage...
References: 1 2  Groups: php.db 
Request: Send a blank email to php-db+get-20882@lists.php.net to get a copy of this message
OK, here is the answer in (unedited except for comments)code: $query = "select count(vid) as total_qs from poll_vote where pid = $pid"; // count the number of fields in the table $result = mysql_query($query) or die("Query failed: $query<br>" . mysql_error()); $row = mysql_fetch_array($result); $query = 'select count(poll_vote.vid) as total, (count(poll_vote.vid) / '.$row['total_qs'].' * 100) as percentage, poll_option.poption from poll_vote, poll_option where poll_vote.oid = poll_option.oid and poll_vote.pid = '.$pid.' group by poll_vote.oid'; /* count the number of rows for a id or something then devide it by the number of total rows. then mulitply it by 100. use group by clause to group results by the thing you want to group it by */ $result = mysql_query($query) or die("Query failed: $query<br>" . mysql_error()); $num_results = mysql_num_rows($result); for ($i=0; $i < $num_results; $i++) { $row = mysql_fetch_array($result); echo '<tr><td>'.$row['poption'].' - '; echo $row['total'].' Votes.</td><td><img alt="line.gif (1K)" src="images/line.gif" height="15" width="'.$row['percentage'].'">'."</td></tr>\n"; } here it is in the two SQL queries + required code to get var: $query = "select count(vid) as total_qs from poll_vote where pid = $pid"; // count the number of fields in the table $result = mysql_query($query) or die("Query failed: $query<br>" . mysql_error()); $row = mysql_fetch_array($result); $query = 'select count(poll_vote.vid) as total, (count(poll_vote.vid) / '.$row['total_qs'].' * 100) as percentage, poll_option.poption from poll_vote, poll_option where poll_vote.oid = poll_option.oid and poll_vote.pid = '.$pid.' group by poll_vote.oid'; -- JJ Harrison webmaster@tececo.com www.tececo.com "César aracena" <icaam@icaam.com.ar> wrote in message news:002901c23716$b352a020$84c405c8@gateway... > Hi JJ. I know we all should do what you did, but I've seen this kind of > mails quite a lot lately in these lists. I'm sure that many of you, like > me, keep these mails for further reading and reference... it would be > nice if you also state what was the answer to your original question. I > mean, if you came up with a question that some of us might have in the > future, it would be nice to have the answer off-line (in our mail soft) > instead of going on-line for search... Shear it with others! > > Thanks, C. > > > -----Original Message----- > > From: JJ Harrison [mailto:webmaster@tececo.com] > > Sent: Monday, July 29, 2002 7:03 AM > > To: php-db@lists.php.net > > Subject: [PHP-DB] Re: Percentage... > > > > Plz ignore this. found it after more searching... > > > > > > -- > > JJ Harrison > > webmaster@tececo.com > > www.tececo.com > > > > "Jj Harrison" <webmaster@tececo.com> wrote in message > > news:20020729092720.92473.qmail@pb1.pair.com... > > > How, using the group by and where statments can I return the > percentage > > of > > > something? > > > > > > ie: > > > > > > pid | oid | uid | > > > 1 |2 |1 > > > 1 |2 |1 > > > 1 |2 |1 > > > 1 |1 |1 > > > > > > would return > > > oid | percent > > > 2 | 75 > > > 1 | 25 > > > > > > Thx, > > > > > > > > > -- > > > JJ Harrison > > > webmaster@tececo.com > > > www.tececo.com > > > > > > > > > > > > > > > > > -- > > PHP Database Mailing List (http://www.php.net/) > > To unsubscribe, visit: http://www.php.net/unsub.php > >

« previous php.db (#20882) next »