How to add +1 to php
So, I am making a pie chart with PHP and Ajax only. But I don't see how I should code further to make it work (see first image). So when someone clicks on the ABC or D button, it should be visible (without loading the page) that you voted for one of them, and also seen in the diagram. In fact, that's all! The second image shows my database.
Before I forget to say, I have no image in my database to modify.
I hope some of you will 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();
?>
source to share
There are a number of problems in the code, but the problem you are facing is that your function pie
does not increment the values ββthat you retrieved from the DB, which were executed outside of bounds, but the incremented values ββare not 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 allowed some comments to be added to point out some problems with the code.
Just remove the code that asks for and increments it outside of the function pie
and do all your work. Pass it the values ββyou want (i.e. $_GET
, and let it return the result, not echo it).
I would rewrite the function to take arguments like this (not going to use 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 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 this:
if ($_GET) {//if a get form was submitted
echo getPie($_GET, $db);//where $db holds the mysql resource
}
It's just a quick fix, though ... there is still a long way to go ...
What I changed:
A quick gallop through the actual changes I made:
- Passing arguments to a function: The function now takes 2 arguments, an array (in this case values
$_GET
) and a db connection. - Loops instead of repetition:
round($resutl['key']/total*100)
I used a loop instead of repetition , so I only need to write this statement once and apply it to all values - use an array for percentages: instead of assigning a percent to each side of a new variable, I decided to use an array. This keeps data that belongs together, well ... together
- Loops for creating markings. While I find myself overlaying markup together as bad practice, I've noticed that most of the HTML is actually identical, so instead of hardcoding everything, I just loop over
$row
either the$percentages
array (both have the same keys) and populate the specific values. This also means that I can safely add a member without changing the code that generates the markup.
source to share