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.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-05-14 23:35:41 +0000
Seen: 12 times
Last updated: May 14 '23
What is an Ansible role that includes integrated modules?
How can Yii2 be connected to a remote MySQL database through SSH?
How can records be counted from a category that is retrieved through another PHP and SQL query?
Why is the search function in PHP & MYSQL not providing relevant results?
What is the process to view and make edits to my complete MySQL code?
What is the method for showing errors in mysqli_query?
Is it impossible to generate a composer.lock file within a Docker container?