Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to make data not in json value 0?
    primarykey
    data
    text
    <p>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.</p> <p>the codes are a bit too long so hopefully by just wordings would work give out an idea what I want.</p> <p>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.</p> <p>So if the value/data isn't in data.json YET how can I make the certain option (left/right/up) 0?</p> <p>Sorry for my bad english but hopefully you can understand what I'm trying to ask.....</p> <p>here are my codes....of course I skipped posting the body, html tag and stuffs...</p> <p>in my index.php</p> <pre><code>&lt;form action="store.php" method="post"&gt; &lt;?php $music_type = array("pop", "rock", "metallic"); foreach($music_type as $type) { echo $type . '&lt;input type="radio" name="type" value='. $type . '&gt;' . '&lt;br&gt;' . PHP_EOL; } ?&gt; </code></pre> <p>in my store.php</p> <pre><code> &lt;?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 "&lt;h1&gt;Vote Results&lt;/h1&gt;"; foreach($votes as $vote =&gt; $count) { echo "&lt;b&gt;$vote&lt;/b&gt; has $count votes&lt;br /&gt;"; } $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; ?&gt; &lt;canvas id="piechart1" width="400" height="400"&gt;&lt;/canvas&gt; &lt;script&gt; piechart("piechart1", ["cyan", "yellow", "green"], [ &lt;?php echo $metallic_pie;?&gt;, &lt;?php echo $pop_pie;?&gt;, &lt;?php echo $rock_pie;?&gt;]); &lt;/script&gt; </code></pre> <p>and let's say in my data.json only </p> <pre><code>{"type":"rock"} {"type":"metallic"} {"type":"metallic"} </code></pre> <p>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.</p> <p>Hopefully you understand what my question is and again really sorry for my poor explanation and english</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload