Reputation: 591
I'm really sorry for my bad title because I don't know how to shorten my question. I had a bit help earlier with the thing I'm trying at the moment but then now I'm stuck again and couldn't get out of my box again. Helpfully someone can give me a hand.
the codes are a bit too long so hopefully by just wordings would work give out an idea what I want.
ok so I'm having a form with let's say radio button of left right up and let's people choose then submit. I then used the # of votes / sum of votes and input it into pie chart. I then realized if no one voted for either left/right/up or let's say if one of them isn't being voted YET then I will get an error because the data isn't even in my data.json file.
So if the value/data isn't in data.json YET how can I make the certain option (left/right/up) 0?
Sorry for my bad english but hopefully you can understand what I'm trying to ask.....
here are my codes....of course I skipped posting the body, html tag and stuffs...
in my index.php
<form action="store.php" method="post">
<?php
$music_type = array("pop", "rock", "metallic");
foreach($music_type as $type)
{
echo $type . '<input type="radio" name="type" value='. $type . '>' . '<br>' . PHP_EOL;
}
?>
in my store.php
<?php
$file_handle = fopen('data.json', 'a');
if($file_handle) {
fwrite(
$file_handle,
json_encode($_POST).PHP_EOL
);
fclose($file_handle);
}
else {
echo 'Error opening data file.';
}
$file = file('data.json'); // each line gets added to the $file array
$votes = array(); // initiate $votes to an array
foreach($file as $line)
{
// json decode current line
$vote = json_decode($line, true);
// use the vote as the key
$key = $vote['type'];
// check if current vote exits. If it does increment vote by 1
if(isset($votes[ $key ]))
{
$votes[ $key ]++;
}
// vote doesn't exist yet. Add vote to votes (creates new key). Initiate vote with 1
else
{
$votes[ $key ] = 1;
}
}
echo "<h1>Vote Results</h1>";
foreach($votes as $vote => $count)
{
echo "<b>$vote</b> has $count votes<br />";
}
$sum = $votes['metallic'] + $votes['pop'] + $votes['rock'];
$circle_degree = 360;
$metallic_pie = $votes['metallic'] / $sum * $circle_degree;
$pop_pie = $votes['pop'] / $sum * $circle_degree;
$rock_pie = $votes['rock'] / $sum * $circle_degree;
?>
<canvas id="piechart1" width="400" height="400"></canvas>
<script>
piechart("piechart1", ["cyan", "yellow", "green"], [ <?php echo $metallic_pie;?>,
<?php echo $pop_pie;?>,
<?php echo $rock_pie;?>]);
</script>
and let's say in my data.json only
{"type":"rock"}
{"type":"metallic"}
{"type":"metallic"}
I know some people say this is not valid json but then I believe from my previous post someone told me because I'm using radio instead of checkbox and so on but my real question is since my json only contains and these {"type":"rock"} {"type":"metallic"} exists and NOT {"type":"pop"} How can I make pop with value of 0. Without {"type":"pop"} even exist in data.json php isn't recalling anything inside json.
Hopefully you understand what my question is and again really sorry for my poor explanation and english
Upvotes: 0
Views: 212
Reputation: 6148
Change:
$sum = $votes['metallic'] + $votes['pop'] + $votes['rock'];
$circle_degree = 360;
$metallic_pie = $votes['metallic'] / $sum * $circle_degree;
$pop_pie = $votes['pop'] / $sum * $circle_degree;
$rock_pie = $votes['rock'] / $sum * $circle_degree;
To:
$voteM = (empty($votes['metallic'])) ? 0 : (int)$votes['metallic'];
$voteP = (empty($votes['pop'])) ? 0 : (int)$votes['pop'];
$voteR = (empty($votes['rock'])) ? 0 : (int)$votes['rock'];
$sum = $voteM + $voteP + $voteR;
$circle_degree = 360;
$metallic_pie = $voteM / $sum * $circle_degree;
$pop_pie = $voteP / $sum * $circle_degree;
$rock_pie = $voteR / $sum * $circle_degree;
Upvotes: 1