So I am making a pie chart with only PHP and Ajax. But I can't see how I must code further to make it work (see the first picture). So when someone clicks on button A B C or D, it must be seen (without page loading) that you have voted for one of them and also be seen in the chart. Actually that is it! The second image shows my database.

Before I forget to tell I DO NOT have a picture in my database to change.

I hope some of you can help me with this. Some of my code:
<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();
?>
Best How To :
There are a number of problems in your code, but the one that causes the actual problems you're having is the fact that your function pie
does not increment the values you fetched from the DB, that's done outside of the function, but the incremented values aren't used there:
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 />";
}
I've taken the liberty of adding some comments to point out several issues with the code.
Just remove the code that queries and increments outside of the pie
function, and have your function do all of the work. Pass it the values it needs (ie $_GET
, and have it return the result, instead of echoing it).
I'd rewrite the function to take arguments like so (not going into not using mysql at this point):
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;
}
Then call this function like so:
if ($_GET) {//if a get form was submitted
echo getPie($_GET, $db);//where $db holds the mysql resource
}
That's just a quick-fix, though... there's still a long way to go...
What I changed:
A quick canter through the actual changes I've made:
- Passing arguments to the function: the function now takes 2 arguments, an array (the
$_GET
values in this case), and the db connection to use.
- Loops instead of repetition: instead of repeating
round($resutl['key']/total*100)
, I've used a loop, so I only have to write this statement once, and have it applied to all values
- use an array for the percentages: Instead of assigning the percentage for each party to a new variable, I've opted to use an array instead. This keeps data that belongs together, well... together
- Loops to create markup: Though I find stringing together markup bad practice, I've noticed most of the HTML is actually identical, so instead of hard-coding everything, I simply iterate over either the
$row
or $percentages
array (they both have the same keys), and fill in the specific values. This also means that I can safely add a party without having to change the code that creates markup