//CMXads.com Unlimited Polls for your website noSQL //Addon Utility Php Script demo tutorial example //Legal to use with visible display of credit and link to cmxads.com //Copyright 2021 by George CMXads.com //PHP INCLUDE THIS IN PAGE WHERE POLL IS RUNNING 'Do you like to take a poll?'); $poll_options_1 =array('No. But my boy friend does.', 'No. But my girl friend does.', 'Yes. I take every poll I can.', 'Sometimes. If I like the poll.', 'Yes. I take poles in the bum.'); ///////////////////// END INCLUDE ?> $choice_id, 'cnt'=> '1' , 'option' =>$poll_options_1[$choice_id])); $input= json_encode($input); if( file_put_contents( $to_file, $input)){ $error=''; } } // if file exists and has entries if (file_exists($to_file) && filesize($to_file) >4){ $f_out = file_get_contents($to_file);// read file $f_out = json_decode($f_out, true);// json decode $found='';// initiate flag variable if we found selected option already in array $i='0';// starting array key at 0 foreach($f_out as $opt) {//f_out array to arrays of poll options // foreach loop the inner level options array // each option has choice number(key), vote count and option text foreach($opt as $key=>$value) { //if match on posted choice and array choice if($key == 'choice' && $value == $choice_id){ // flag found set. So we know not to add a new choice option to array $found='1'; //echo $key." ". $value. "
"; //echo $f_out[$i]['cnt']. "
"; $opt['cnt'] += '1';// get the count of that choice option and add 1 } } //in loop with $i number update $f_out array count for that choice option $f_out[$i]['cnt']=$opt['cnt']; $i++; } if($found){// we updated option vote count above $input= json_encode($f_out);// encode if( file_put_contents( $to_file, $input)){// write back to file $error=''; } } if(!$found){// we must add the new chosen option to array // the new pushed array is the posted choice key, count=1 //and pull poll option text from options array $input2 = array('choice' =>$choice_id, 'cnt'=> '1' , 'option' => $poll_options_1[$choice_id]); array_push($f_out, $input2);// push new array into array $input= json_encode($f_out);// encode if(file_put_contents( $to_file, $input)){// write back to file $error=''; } } if(!$error){ //echo '

Thanks for taking a pole

'; } // echo '
';
 //$f_out = file_get_contents($to_file);// read and process results
// $f_out = json_decode($f_out, true);// json decode
//var_dump( $f_out);
// echo	'
'; //exit; } } } ?> 5){ $f_out = file_get_contents($poll_file);// read file $f_out = json_decode($f_out, true);// decode //now we want to process the data to get the votes //and calculate the percentages of the total votes to options $all_votes=0;// initialize variable foreach($f_out as $choice){ // first calculate totalvote in loop $all_votes += $choice['cnt']; } if($all_votes==0) { print "No votes have been casted.
"; }else{ // in another loop get vote count of each option foreach($f_out as $choice){ $percentage=($choice['cnt']/$all_votes)*100;// calculate percentage from totl votes $percentage=round($percentage,2);// round off to 2 digits //print results with image scaling graphic width using percentage print $choice['option'] . "
($percentage %)
"; } } print "
Total Votes Casted: $all_votes"; //viewed in pop up so window close script echo "
close window
"; } } } ?> "; echo ""; echo "
"; print "
"; echo ''; foreach ($poll_question_1 as $poll_file => $poll_question) { echo "

$poll_question

"; foreach ($poll_options_1 as $quest_id => $poll_question) { print "

"; } echo ""; print ''; // print "

"; print "

Poll Results

"; echo "

© 2022 CMXads.com Unlimited polls script.

"; } print ""; } ?>