Skip to content Skip to sidebar Skip to footer

What Code Approach Would Let Users Apply Three Optional Variables In Php/mysql?

I have a large CSV file containing >11k rows of data, with 13 columns. The 3 user-selected variables are State, City, and Job Title. My question is what methodology/function typ

Solution 1:

Your server script simply translates the parameters to appropriate conditions in the WHERE clause of a query. E.g.

$wheres = array();
$params = array();
if (!empty($_POST['state'])) {
    $wheres[] = "state = :state";
    $params['state'] = $_POST['state'];
}
if (!empty($_POST['city'])) {
    $wheres[] = "city = :city";
    $params['city'] = $_POST['city'];
}
if (!empty($_POST['title'])) {
    $wheres[] = "title = :title";
    $params['title'] = $_POST['title'];
}
$query = "SELECT * FROM YourTable";
if (count($wheres)) {
    $query .= " WHERE " . implode(" AND ", $wheres);
}
$stmt = $pdo->prepare($query);
$result = $stmt->execute($params);

Post a Comment for "What Code Approach Would Let Users Apply Three Optional Variables In Php/mysql?"