PHP MySQL Pool显示最高投票率和投票 [英] PHP MySQL pool show highest voted and votes

查看:80
本文介绍了PHP MySQL Pool显示最高投票率和投票的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行一个PHP脚本以将池保存到数据库中 我的MySQL看起来像这样

I am running a PHP script to save a pool into database My MySQL looks like this

ID    QUESTION_1
1     1 or 2 or 3 ( according to the chosen option value in number )

如果我在QUESTION_1中投票,则可以对选项1或2或3进行投票,并且所选值的数字在MySQL中保存为数字1或2或3

If I vote in QUESTION_1 I can vote option 1 or 2 or 3 and the number of the chosen value is saved in MySQL as number 1 or 2 or 3

我需要

如果大多数人对选项2进行投票,比如说348次,而第二名则以208票选择了选项3,而第三名则以87票选择了选项3,我需要显示:

If most people voted option 2 let's say 348 times, and in second place people chosed option 3 with 208 votes and in third place comes option 3 with 87 votes I need to show:

Option 2 winner with 348 votes
Option 3 with 208 votes
option 2 with 87 votes

我们显示获胜者以及多少票...

We show the winner and how many votes ...

我正试图这样做

$r['total']=0;
foreach(array(1,2,3) as $QUESTION_1){
  $res=mysqli_query($datacenter, "SELECT COUNT(QUESTION_1) AS `total` 
                                  FROM `votes` WHERE `QUESTION_1` = '$QUESTION_1' ");
  $tmp=mysqli_fetch_assoc($res);
  $r=max($tmp['total'],$r['total']);
 }


 echo number_format($r,0,',','.');

但是我是PHP新手,所以欢迎您提供帮助

But I am new to PHP so any help is welcome

有什么想法吗?

推荐答案

您不需要太多代码,只要我试图解释所有事情,答案就很长,但是代码很短

阅读您的评论非常重要,首先请看一下我的数据库图片

This is very important that you read the comment, first have a look at at my database picture

在这里您可以看到人们选择了选项2,三次

as you can see in here people choose option 2, three times

他们选择选项1,两次

然后他们只选择3次选项

and than they choose options 3 only one times,

所以选项2以3票得主,选项1以2票获得第二名,选项3以一票获得第三名

so option 2 is winner with 3 votes, option 1 is at second place with 2 votes and option 3 is at third place with one vote

所以您的显示应该是这样

选项1获胜者3票

具有2票的选项2

具有1票的选项3

如果我现在是正确的话,请进一步阅读,否则请附上您的数据库图片,并附上您的问题,然后让我知道,

If i am right till now, read further, other wise attach a picture of your database, with your question, and let me know,

解决方案-您不需要太多代码即可完成操作,只需使用下面的代码即可

$res = mysqli_query($datacenter,"SELECT   *, COUNT(QUESTION_1) as total FROM 
    `votes` GROUP BY QUESTION_1 ORDER BY total DESC");
    $declration = ['winner with','with','with'];
    $i=0; // you dont need this line, but if you dont need this it will show you 
// the index error but your code will work fine
    while ($row = mysqli_fetch_assoc($res)) {
           echo 'option '.'    '. $row['QUESTION_1'].'    '.
     $declration[$i++].'    '. $row['total']. '    '. 
    'votes<br>';

    }

说明-第一个查询说明

SELECT   *, COUNT(QUESTION_1) as total FROM `votes` GROUP BY QUESTION_1 ORDER BY total DESC

什么查询只是说选择所有东西,然后数出要自我重复的问题Question_1,然后按重复值的数量对它们进行排序,最后我要说的是按总DESC排序,如果我不对它们进行排序,很好,但是比起我必须对结果进行排序,所以这就是为什么我现在订购它们的原因.

what query is simply saying select every thing and than count number question_1 which are repeating it self, and order them by number of repeated value, and in the end i am saying order them by total DESC, if i dont order them, its fine, but than i have to sort the result, so that's why i am ordering them now.

如果您在本地主机中运行此查询,您将得到如下结果

您可以尽可能很好地对我们的结果进行排序,所以现在就以为我们必须处理显示消息.

as you can our result is nicely sorted, so only think now we have to work on our display message.

我正在使用数组显示结果

显示结果,而不是简单地键入它们,而是使用数组declration

do display the result, instead of typing them plainly i am using the the array, declration

$declration = ['winner with','with','with'];

我使用数组的原因是,它使我的工作更加轻松,如您所见,第一个值是winner with,而不是第二个值with和第三个是with,请尝试更改第二个和第三个值,例如亚军或第三名,那么您的显示也会发生变化.

the reason i am using array, as it make my job easier, as you can see first value is winner with, than second its with and third is with as well, try to change the second and third value, some thing like runner up or third place, and your display will change as well.

现在正在获取并显示

echo 'option '.' &nbsp;&nbsp; '. $row['QUESTION_1'].' &nbsp;&nbsp; '. $declration[$i++].' &nbsp;&nbsp; '. $row['total']. ' &nbsp;&nbsp; '. 'votes<br>';

我正在使用&nbsp;只是创建另一个空间,其余的您将理解,如果您不了解,这部分在做什么$declration[$i++],更改我们原始的$declration数组中的值,您将能够要了解,

i am using &nbsp; just to create another space, rest of you will be understand, if you dont understand, what this part is doing $declration[$i++], change the value in our original $declration array, you will be able to understand,

此消息真的很重要,您的代码对于sql注入是脆弱的,请执行reasearch或观看视频,

and this message is really important, your code is vunrable to sql injection, please do a reasearch or watch the vedio,

任何困惑都让我知道

这篇关于PHP MySQL Pool显示最高投票率和投票的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆