1 | initial version |
Yes, it is possible to generate a PHP/MySQL table that records and tallies multiple variables.
Here is an example of a PHP/MySQL table that records and tallies the number of votes for multiple candidates:
CREATE TABLE election_results
(
id
int(11) NOT NULL AUTO_INCREMENT,
candidate_name
varchar(255) NOT NULL,
candidate_party
varchar(255) NOT NULL,
num_votes
int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (id
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
You can then insert data into the table using SQL queries:
INSERT INTO election_results
(candidate_name
, candidate_party
, num_votes
)
VALUES ('John Smith', 'Democratic Party', 10),
('Jane Doe', 'Republican Party', 8),
('Bob Jones', 'Green Party', 5);
You can then use PHP to retrieve and display the data from the table in a table format, along with any calculations you wish to perform on the data:
$query = "SELECT candidatename, candidateparty, numvotes FROM electionresults ORDER BY numvotes DESC"; $result = mysqliquery($connection, $query);
echo "
"; echo ""; while ($row = mysqlifetchassoc($result)) { echo ""; } echo "Candidate Name | Party | Number of Votes |
---|---|---|
" . $row['candidatename'] . " | " . $row['candidateparty'] . " | " . $row['num_votes'] . " |
You can also use SQL queries to perform calculations, such as calculating the total number of votes:
SELECT SUM(numvotes) AS totalvotes FROM election_results;
This will return a single row with the total number of votes.