Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP MySQL池显示最高投票数和投票数_Php_Html_Css_Mysql_Forms - Fatal编程技术网

PHP MySQL池显示最高投票数和投票数

PHP MySQL池显示最高投票数和投票数,php,html,css,mysql,forms,Php,Html,Css,Mysql,Forms,我正在运行一个PHP脚本,将一个池保存到数据库中 我的MySQL看起来像这样 ID QUESTION_1 1 1 or 2 or 3 ( according to the chosen option value in number ) $r['total']=0; foreach(array(1,2,3) as $QUESTION_1){ $res=mysqli_query($datacenter, "SELECT COUNT(QUESTION_1) AS `total`

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

ID    QUESTION_1
1     1 or 2 or 3 ( according to the chosen option value in number )
$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,',','.');
如果我在问题_1中投票,我可以投票选择1、2或3,所选值的编号在MySQL中保存为编号1、2或3

我需要什么

如果大多数人投了选项2的票,比如说348次,第二名的人投了208票,第三名的人投了87票,我需要证明:

Option 2 winner with 348 votes
Option 3 with 208 votes
option 2 with 87 votes
我们展示了获胜者和多少票

我正试着这样做

ID    QUESTION_1
1     1 or 2 or 3 ( according to the chosen option value in number )
$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新手,所以欢迎任何帮助


你知道怎么做吗?

你不需要太多的代码,答案很长,只要我试着解释每件事,但代码很短

这是非常重要的,你阅读评论,首先看看我的数据库图片

正如你在这里看到的,人们选择了选项2,三次

他们选择选项1,两次

然后他们只选择了选项3一次

因此,选项2以3票获胜,选项1以2票排名第二,选项3以一票排名第三

因此,您的显示应如下所示

$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
echo 'option '.' &nbsp;&nbsp; '. $row['QUESTION_1'].' &nbsp;&nbsp; '. $declration[$i++].' &nbsp;&nbsp; '. $row['total']. ' &nbsp;&nbsp; '. 'votes<br>';
选项1以3票获胜

备选案文2,2票

备选案文3,1票

如果我到现在为止是对的,请进一步阅读,或者附上您的数据库图片,以及您的问题,并让我知道

解决方案-执行此操作不需要太多代码,只需使用下面的代码即可

$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 '.' &nbsp;&nbsp; '. $row['QUESTION_1'].' &nbsp;&nbsp; '.
     $declration[$i++].' &nbsp;&nbsp; '. $row['total']. ' &nbsp;&nbsp; '. 
    'votes<br>';

    }
SELECT   *, COUNT(QUESTION_1) as total FROM `votes` GROUP BY QUESTION_1 ORDER BY total DESC
echo 'option '.' &nbsp;&nbsp; '. $row['QUESTION_1'].' &nbsp;&nbsp; '. $declration[$i++].' &nbsp;&nbsp; '. $row['total']. ' &nbsp;&nbsp; '. 'votes<br>';
什么查询只是说,选择每件事,然后计算重复的问题1,然后按重复值的数量排序,最后我说的是按总描述排序,如果我不排序,这很好,但是我必须对结果排序,所以我现在排序

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

正如您所能看到的,我们的结果被很好地分类了,所以现在只需考虑我们必须处理我们的显示消息

我正在使用数组显示结果

一定要显示结果,而不是简单地键入结果,我使用的是数组declration

$declration = ['winner with','with','with'];
我之所以使用数组,是因为它使我的工作更容易,正如您所看到的,第一个值是
winner with
,而第二个值是
with
,第三个值也是
with
,尝试更改第二和第三个值,例如亚军或第三名,您的显示也会更改

现在开始抓取和显示

$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 '.' &nbsp;&nbsp; '. $row['QUESTION_1'].' &nbsp;&nbsp; '.
     $declration[$i++].' &nbsp;&nbsp; '. $row['total']. ' &nbsp;&nbsp; '. 
    'votes<br>';

    }
SELECT   *, COUNT(QUESTION_1) as total FROM `votes` GROUP BY QUESTION_1 ORDER BY total DESC
echo 'option '.' &nbsp;&nbsp; '. $row['QUESTION_1'].' &nbsp;&nbsp; '. $declration[$i++].' &nbsp;&nbsp; '. $row['total']. ' &nbsp;&nbsp; '. 'votes<br>';
echo“选项”$第['QUESTION_1'].'行$声明[$i++].''$第['total'].'行投票
';
我使用
只是为了创建另一个空间,其他人将理解,如果您不理解,这部分正在做什么
$declration[$i++],更改原始
$declration
数组中的值,您将能够理解

这条消息非常重要,您的代码可以进行sql注入,请重新搜索或观看视频


任何困惑都让我知道

看起来像是家庭作业。你在上什么课?用通俗易懂的英语(而不是代码)你认为应该如何进行。分步布置。有些地方你的问题没有意义?查看
echo
代码片段。选项1怎么了?为什么选项#2列了两次?显示你的表结构和你想要达到的目标。我编辑了这个问题,现在很容易理解了。你可以做的是,当有人投票时,在问题1列中添加一个,然后你可以运行select查询,并按投票顺序排列。ASC,你在这里尝试的方式使简单的工作变得复杂,如果你不明白我想说的话,请告诉我,我会将其作为解决方案发布,但当你告诉我发布解决方案时,也请尝试说出你不明白的内容,mysqli不会自动保护你的代码,我创建的视频是为了知道你的代码为什么是vunrable的,ARIF_SUHAIL事实是我不明白,我是PHP新手。你能写一个解决方案,这样我也可以选择它作为正确的答案,每一个详细和完美的答案