PHP - Search query combined

0 votes
27 views
added Apr 11 in API by lcjr Lieutenant (12,240 points)
if ($search) {
    $keywords = preg_split('/\s+/', strtolower(trim($search)));
    $searchClauses = [];

    foreach ($keywords as $word) {
        $searchClauses[] = "(LOWER(org_name) LIKE ? OR LOWER(country) LIKE ? OR LOWER(org_extra_info) LIKE ? OR LOWER(org_category) LIKE ? OR LOWER(location) LIKE ?)";
        // Push the same word 5 times for 5 fields
        for ($i = 0; $i < 5; $i++) {
            $params[] = "%$word%";
            $types .= "s";
        }
    }

    $query .= $is_featured !== null ? " AND" : " WHERE";
    $query .= " (" . implode(" AND ", $searchClauses) . ")";
}

 

lazacode.org - Malaysia's programming knowledge sharing platform, where everyone can share their finding as reference to others.
...