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
[EMAIL PROTECTED]
www.tececo.com

"CÚsar aracena" <[EMAIL PROTECTED]> wrote in message
002901c23716$b352a020$84c405c8@gateway">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:[EMAIL PROTECTED]]
> > Sent: Monday, July 29, 2002 7:03 AM
> > To: [EMAIL PROTECTED]
> > Subject: [PHP-DB] Re: Percentage...
> >
> > Plz ignore this. found it after more searching...
> >
> >
> > --
> > JJ Harrison
> > [EMAIL PROTECTED]
> > www.tececo.com
> >
> > "Jj Harrison" <[EMAIL PROTECTED]> wrote in message
> > [EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
> > > 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
> > > [EMAIL PROTECTED]
> > > www.tececo.com
> > >
> > >
> > >
> >
> >
> >
> > --
> > PHP Database Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
>
>



-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to