PHP / MySQL question

Status
Not open for further replies.

Techwhore

Smack-Fu Master, in training
79
I'm scripting a poll and need to create an administrator page that'll display the results. What i'd like to do is have the percentages for each selection listed. I need to know how to script it so that i can run a select count(*) to get the total number of votes then divide that into the specific questions.

I can't seem to get it to display the oount, it comes up as resource id #2.

Here's the source I have now which produces the "resource id #2":

$count = "select count(*) from survey";
$count_result = mysql_db_query($dbname, $count, $id_link);
$count_this = mysql_result($count_result,0);

thanks in advance
 
Checked this code on my server before posting, sorry if I'm misunderstanding what you want, but here's my take:

Code:
$link = @mysql_connect(DBA_HOST_NAME, USER, DBA_PW) ;
mysql_select_db(DBA) ;
$query = "SELECT id FROM survey" ;   // don't SELECT *, PHP.net says bad/slower idea...
$result = mysql_query($query, $link) ;
$rows = mysql_num_rows($result) ;
print $rows ;

in this, id is just the index column of your database, I always use id because I don't have much imagination ;)

Hope this helped in some way....

Jher
 

Techwhore

Smack-Fu Master, in training
79
Thanks for the help guys, I ended up referencing some of my older scripts and got it, didn't think i still had em. (Haven't scripted in months).

Anyway, my project is completed but i still have a question.

For the survey, on the results page i display the selection, number of votes and percentage of votes that selection received. Is there a way i can format the output to be no more than one tenth after the decimal? Right now it shows up as something like 11.111111% One place after would be sufficient.

Thanks!
 

nottestuser

Ars Scholae Palatinae
1,049
I'm not sure how big your poll will be but why summarize every time you want to view the results? You can just have a table of summary statistics and update that every time someone votes. You can keep the individual votes in another table too if you want.

For a small poll of a couple hundred votes this is not useful but if you run a bunch of polls on a site with a lot of traffic, your vote table can easily get in to millions of records. You don't want to be summarizing that every time you show the results.
 
Status
Not open for further replies.