No database required for online polling survey php code This is an online voting system made of php + txt text files. It can complete online user survey functions without database support.
Online survey program code
//survey.php
/*
No database tutorial online poll php code
This is an online voting system made of php + txt text files. It can complete online user survey functions without database support.
*/
$data="data.txt";
$votes="survey.txt";
$dataf=file($data); /*Read the items in the survey project file*/
$file_votes=fopen($votes, "r");
$line_votes=fgets($file_votes, 255); /*Read the recorded survey results*/
fclose($file_votes);
$single_vote=explode("|", $line_votes); /* Split the data according to the specified string, and then transfer the string back to the array variable */
if ($result!=1) /*If the survey has been accepted*/
{
$file_votes=file($votes, "r");
if ($remote_addr == $file_votes[1]) /*Check if they are the same person*/
{
echo "You have already voted, thank you for your participation! ";
exit;
}/*If the IP is not repeated, execute the following procedure*/
$ficdest=fopen($votes, "w");
for ($i=0; $i<=count($dataf)-1; $i++)
{
if ($i == $vote)
$single_vote[$i]+=1;
}
fputs($ficdest, "$single_vote[$i]|"); /*Write data back to file*/
}
fputs($ficdest, "n$remote_addr");/* //Write the voter ip*/
fclose($ficdest);
$result=1; /*Vote successful*/
}/*Write the voting results and display the voting results*/
if ($result==1)
{
echo "";
for ($i=0; $i<=count($dataf)-1; $i++)
{
/*Get the total number of votes*/
$tot_votes+=$single_vote[$i];
}
for ($i=0; $i<=count($dataf)-1; $i++)
{
$imag=strval($i).".gif";/*Determine which bar picture to use to display statistical results*/
$stat[$i]=$single_vote[$i]/$tot_votes*100; /*Calculate percentage*/
$scla=$stat[$i]*5;/*Bar chart and magnification, here it is displayed by the width of the pixel that is 5 times the percentage of ampere*/
echo ""; ";
echo "$dataf[$i]";
echo " ";/*Output barcode image*/
Printf("%.1f", "$stat[$i]");
echo "%";
/*Output the number of votes for this column*/
echo "$single_vote[$i]";
echo "
}
echo "";
echo "Total votes: $tot_votes ";
}/*
data.txt file
Survey item one
Survey item two
Survey item three
Survey item four
Survey item five
*/
?>