如何将+1值添加到php

鹿特丹

因此,我仅使用PHP和Ajax制作饼图。但是我看不到必须如何进一步编码才能使其正常工作(请参见第一张图片)。因此,当某人单击按钮ABC或D时,必须看到(不加载页面)您已对其中之一投了赞成票,并且还必须在图表中看到。就是这样!第二张图片显示了我的数据库。

在此处输入图片说明

在我忘记告诉我数据库中没有要更改的图片之前。

在此处输入图片说明

我希望你们中的一些可以帮助我。我的一些代码:

<p><h1>Breng jou stem uit</h1></p><br />
<form action = "<?php
echo $_SERVER['PHP_SELF'];
?>" method = "GET"> 
<button type="button" name="a">Partij A</button><br />
<button type="button" name="b">Partij B</button><br />
<button type="button" name="c">Partij C</button><br />
<button type="button" name="d">Partij D</button>
 </form>
<?php

// Connects to your Database

include('../../../connection.php');

$sql = mysql_query("SELECT * FROM votes");

while ($row = mysql_fetch_array($sql)) {
echo $partijA = $row['partijA'];
$partijB = $row['partijB'];
$partijC = $row['partijC'];
$partijD = $row['partijD'];
if (isset($_GET['a'])) {
    echo $resultA = $partijA + 1;
} else {
    echo "1";
}

$resultB = $partijB + 1;
$resultC = $partijC + 1;
$resultD = $partijD + 1;
}

// Name of our cookie

$cookie = "Voted";

// A function to display our results - this refrences vote_pie.php which we     will also make

function pie()
{
    $data = mysql_query("SELECT * FROM votes") or die(mysql_error());
    $result = mysql_fetch_array($data);
    $total  = $result[partijA] + $result[partijB] + $result[partijC] +      $result[partijD];
    $one    = round(360 * $result[partijA] / $total);
    $two    = round(360 * $result[partijB] / $total);
    $per1   = round($result[partijA] / $total * 100);
    $per2   = round($result[partijB] / $total * 100);
    $per3   = round($result[partijC] / $total * 100);
    $per4   = round($result[partijD] / $total * 100);
    echo "<img src=vote_pie.php?one=" . $one . "&two=" . $two . "><br/>";
    Echo "<font color=000000>Partij A</font> = $result[partijA] votes = $per1 <br /> 
         <font color=000000>Partij B</font> = $result[partijB] votes = $per2 <br />
         <font color=000000>Partij C</font> = $result[partijC] votes =  $per3 <br /> 
         <font color=000000>Partij D</font> = $result[partijD] votes = $per4 <br />";
}

// displays the poll results

pie();
?>
埃里亚斯·范·奥特格姆

您的代码中存在许多问题,但导致您遇到实际问题的一个问题是,您的函数pie不会递增从数据库中获取的值,而是在函数外部完成,而是递增的值在那里不使用:

function pie()
{
    //this query was already performed, pass the result resource to this function, don't re-run the query
    $data = mysql_query("SELECT * FROM votes") or die(mysql_error()); //google or die must die
    $result = mysql_fetch_array($data);//mysql is deprecated
    //array keys need to be quoted
    $total  = $result[partijA] + $result[partijB] + $result[partijC] +      $result[partijD];
    $one    = round(360 * $result[partijA] / $total);
    $two    = round(360 * $result[partijB] / $total);
    $per1   = round($result[partijA] / $total * 100);
    $per2   = round($result[partijB] / $total * 100);
    $per3   = round($result[partijC] / $total * 100);
    $per4   = round($result[partijD] / $total * 100);
    //functions return, they don't echo
    echo "<img src=vote_pie.php?one=" . $one . "&two=" . $two . "><br/>";
    Echo "<font color=000000>Partij A</font> = $result[partijA] votes = $per1 <br /> 
         <font color=000000>Partij B</font> = $result[partijB] votes = $per2 <br />
         <font color=000000>Partij C</font> = $result[partijC] votes =  $per3 <br /> 
         <font color=000000>Partij D</font> = $result[partijD] votes = $per4 <br />";
}

我随意添加一些注释来指出代码中的几个问题。

只需删除在pie函数外部查询和递增的代码,然后让函数完成所有工作即可。向其传递所需的值(即使$_GET返回结果,而不是回显它)。
我将重写函数以接受这样的参数(此时不使用mysql):

function getPie(array $params, $connection)
{
    //added limit 1, seeing as you're only processing the first result
    $result = mysql_query($connection, 'SELECT * from votes LIMIT 1');
    $row = mysql_fetch_assoc($result);
    if (isset($params['a'])) {
        $row['partijA'] += 1;
    } else if (isset($params['b'])) {
        $row['partijB'] += 1;
    } //add more else's for all parties
    $percentages = [];//array
    foreach ($row as $key => $val) {
        $percentages[$key] = round(($val/$total) * 100);
    }
    $total = array_sum($row);//total of all votes
    $markup = '<img src=vote_pie.php?one=' .
      $percentages['partijA']*360 . '&two=' . $percentages['partijB']*360 . '><br>';
    foreach ($percentages as $k => $perc) {
        $markup .= '<font color=000000>Partij ' . str_replace('partij', $k) . '</font> = '.
            $row[$key] . ' votes = ' . $perc . '<br>';
    }
    return $markup;
}

然后像下面这样调用此函数:

if ($_GET) {//if a get form was submitted
    echo getPie($_GET, $db);//where $db holds the mysql resource
}

不过,这只是一个快速解决方案...还有很长的路要走...

我改变了什么:

快速查看我所做的实际更改:

  • 将参数传递给函数:函数现在需要2个参数,一个数组($_GET在这种情况下值)和要使用的db连接。
  • 循环而不是重复:round($resutl['key']/total*100)我使用了循环,而不是重复,所以我只需要编写一次该语句,并将其应用于所有值
  • 使用数组作为百分比:我没有选择为每个参与方分配百分比到新变量,而是选择使用数组。这样可以将属于一起的数据保持在一起
  • 创建标记的循环:尽管我发现将标记串在一起是一种不好的做法,但我注意到大多数HTML实际上是相同的,因此,不是对所有内容进行硬编码,而是简单地对$rowor$percentages数组进行迭代(它们都具有相同的键) ,并填写具体值。这也意味着我可以安全地添加一个聚会,而不必更改创建标记的代码

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章