Home > Backend Development > PHP Tutorial > How Can I Modify a Single-Parameter Search Form to Accept Multiple Search Parameters?

How Can I Modify a Single-Parameter Search Form to Accept Multiple Search Parameters?

DDD
Release: 2024-12-15 07:55:08
Original
301 people have browsed it

How Can I Modify a Single-Parameter Search Form to Accept Multiple Search Parameters?

Search Form with Multiple Parameters

In your search form implementation, you allow users to input only a single parameter for searching. To enable users to specify multiple parameters, consider modifying your code to dynamically build the search query based on the provided parameters.

Search Form (search.php)

if (
    (isset($_POST['id']) && !empty($_POST['id'])) ||
    (isset($_POST['major']) && !empty($_POST['major'])) ||
    (isset($_POST['college']) && !empty($_POST['college'])) ||
    (isset($_POST['name']) && !empty($_POST['name']))
) {
    $params = array();
    $wheres = array();
    
    if (isset($_POST['id']) && !empty($_POST['id'])) {
        $wheres[] = 'a.uid = :uid';
        $params[':uid'] = $_POST['id'];
    }
    
    if (isset($_POST['major']) && !empty($_POST['major'])) {
        $wheres[] = 'a.major = :major';
        $params[':major'] = $_POST['major'];
    }
    
    if (isset($_POST['college']) && !empty($_POST['college'])) {
        $wheres[] = 'a.college = :college';
        $params[':college'] = $_POST['college'];
    }
    
    if (isset($_POST['name']) && !empty($_POST['name'])) {
        $wheres[] = 'b.name LIKE :name';
        $params[':name'] = '%'.$_POST['name'].'%';
    }
    
    $sql = "SELECT * 
        FROM user_details AS a
        JOIN user AS b ON a.uid = b.id";
    
    if (!empty($wheres)) {
        $sql .= " WHERE " . implode(' AND ', $wheres);
    }
    
    // ... prepare and execute the query ...
    
} else {
    // Handle the case when no parameters are set
}
Copy after login

Results Page (results.php)

// Display the results as before, using a loop and echoing HTML for each result.
Copy after login

By dynamically building the query based on the input parameters, users can now specify multiple parameters in your search form, and the results page will display the students that match all of the specified criteria.

The above is the detailed content of How Can I Modify a Single-Parameter Search Form to Accept Multiple Search Parameters?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template