|
We are going to see how we can create a simple survey with MySQL that stores the votings of the users in the database together with his direction ip to control that every user votes only one time.
The first step that we will do will be the conditioning of the database, in which we will create a table using the following judgment:
ONE BELIEVE TABLE `encuesta` (
`ip` VARCHAR (16) NOT NULL,
`voto` INT (1) NOT NULL,
UNIQUE (
`ip`
)
);
Except if we want that the same user could vote time and again in whose case we will do:
ONE BELIEVE TABLE `encuesta` (
`ip` VARCHAR (16) NOT NULL,
`voto` INT (1) NOT NULL
);
The functioning of the survey is very simple, the program will read the possible options of vote of a called array $opciones, that you will be able to modify to your will, then it will iterate so many times like elements have the array to show the current results of every option.
To be able to show the percentages, the first thing that we do it is a general consultation that returns us the entire number of received votes, of that time, in every option we will do the following operation:
$porcentaje = round ($votos/$total*100,2);
That will return us the percentage of votes rounded to 2 decimal ones using the function round.
Article for courteousness of Eloi of St Martin
www.programacionweb.net |