如何为php添加+1值

时间:2015-06-18 10:48:30

标签: php html ajax database

所以我只用PHP和Ajax制作一个饼图。但是,我无法看到我必须进一步编码以使其工作(参见第一张图片)。因此,当有人点击按钮A B C或D时,必须看到(没有页面加载)您已投票选出其中一个并且也可以在图表中看到。其实就是这样!第二张图显示了我的数据库。

enter image description here

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

enter image description here

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

<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();
?>

1 个答案:

答案 0 :(得分:0)

您的代码中存在许多问题,但导致您遇到的实际问题的问题是您的函数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值),以及要使用的数据库连接。
  • 循环而不是重复:我没有重复round($resutl['key']/total*100),而是使用了循环,所以我只需要编写一次这个语句,并将其应用于所有值
  • 使用数组作为百分比:我没有将每一方的百分比分配给新变量,而是选择使用数组。这使得数据保持在一起,很好......一起
  • 循环创建标记:虽然我发现将标记错误的做法串联在一起,但我注意到大多数HTML实际上是相同的,所以不是硬编码所有内容,而是简单地迭代$row或{ {1}}数组(它们都具有相同的键),并填写特定值。这也意味着我可以安全地添加一方而无需更改创建标记的代码