In web development, counting the frequency of selection of form options is a common task. Whether it is a questionnaire, a scoring system, a selection box, etc., developers usually need to count which options the user has selected in the form and the number of times each option has been selected. The array_count_values function in PHP can easily accomplish this task. This article will introduce how to use this function to count the frequency of form options selection.
array_count_values is a built-in function in PHP that counts the frequency of occurrence of all values in an array and returns an associative array where the key is the value in the array and the value is the number of occurrences of each value.
array_count_values(array $array): array
$array : This is the array to be counted.
The function returns an associative array where the key is the values in the input array, and the values are the number of times these values appear in the array.
Suppose we have a form where the user can choose from multiple options. We collected these options and wanted to count the number of times each option was selected.
Assuming our form contains a radio box, the user can choose different colors as their preferences. The HTML form code is as follows:
<form action="process.php" method="POST">
<label>Please select your preferred color:</label><br>
<input type="radio" name="color" value="red"> red<br>
<input type="radio" name="color" value="blue"> blue<br>
<input type="radio" name="color" value="green"> green<br>
<input type="submit" value="submit">
</form>
In practical applications, form data is usually submitted through the POST method. To simulate this process, we will manually create an array containing the form selection results. Suppose the form data submitted by the user is as follows:
$submitted_data = ['red', 'blue', 'green', 'red', 'blue', 'blue', 'green', 'red'];
We can use the array_count_values function to count the frequency of each color being selected:
$selection_count = array_count_values($submitted_data);
print_r($selection_count);
Array
(
[red] => 3
[blue] => 3
[green] => 2
)
Through the output results, we can clearly see that the selection frequency of each color: red and blue are selected 3 times each, and green is selected 2 times each.
If your form data comes from a database and you want to count based on records in the database, array_count_values can also help you achieve this.
Suppose you have a table that stores the color information selected by the user, and each user's selection is stored in a field in the database. You can query all selected data and then use the array_count_values function to count the selection frequency of each option.
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "form_data";
// Create a connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check the connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT color FROM form_submissions";
$result = $conn->query($sql);
$submitted_data = [];
if ($result->num_rows > 0) {
// Output each line of data
while($row = $result->fetch_assoc()) {
$submitted_data[] = $row['color'];
}
} else {
echo "0 result";
}
$conn->close();
After obtaining the data, use array_count_values to count the selection frequency of each color:
$selection_count = array_count_values($submitted_data);
print_r($selection_count);
array_count_values is a very useful PHP function that helps developers quickly count the frequency of form options selection. Whether it is data submitted directly from the form or data extracted from the database, it can be easily achieved using array_count_values . Through the analysis of statistical results, developers can obtain valuable information and provide support for decision-making.