File Manager
Editing: search.php
<?php // Database credentials $host = "127.0.0.1:3306"; $username = "u404542307_dacotywebsites"; $password = "daCotywebs1te5"; $database = "u404542307_eizon"; try { $pdo_products = new PDO("mysql:host=$host;dbname=$database", $username, $password); $pdo_products->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (PDOException $e) { die("Could not connect to the database: " . $e->getMessage()); } $query = isset($_GET['query']) ? trim($_GET['query']) : ''; if (empty($query)) { echo '<p>Please enter a search query.</p>'; exit(); } // Search for products $product_stmt = $pdo_products->prepare(" SELECT c.card_id, c.name AS product_name, c.image1, p.username, 'product' AS type FROM cards c LEFT JOIN profiles p ON c.user_id = p.user_id WHERE c.name LIKE :query "); $product_stmt->execute(['query' => "%$query%"]); $product_results = $product_stmt->fetchAll(PDO::FETCH_ASSOC); // Search for usernames in profiles $profile_stmt = $pdo_products->prepare(" SELECT NULL AS card_id, p.username, p.profile_image, 'user' AS type FROM profiles p WHERE p.username LIKE :query "); $profile_stmt->execute(['query' => "%$query%"]); $profile_results = $profile_stmt->fetchAll(PDO::FETCH_ASSOC); // If no profile results, search in users table if (empty($profile_results)) { $user_stmt = $pdo_products->prepare(" SELECT NULL AS card_id, u.username, NULL AS profile_image, 'user' AS type FROM users u WHERE u.username LIKE :query "); $user_stmt->execute(['query' => "%$query%"]); $user_results = $user_stmt->fetchAll(PDO::FETCH_ASSOC); } else { $user_results = []; } // Merge results $search_results = array_merge($product_results, $profile_results, $user_results); if (!empty($search_results)) { echo '<h4>Search Results</h4>'; foreach ($search_results as $result) { // Determine the image source based on the result type if ($result['type'] === 'product') { $imageSrc = htmlspecialchars($result['image1']); } elseif ($result['type'] === 'user') { // Use the profile image if it exists, otherwise fall back to the default image $profileImage = $result['profile_image'] ?? ''; if (!empty($profileImage)) { // Check if the image file exists on the server if (file_exists($profileImage)) { $imageSrc = htmlspecialchars($profileImage); } else { // If the image file does not exist, fall back to the default image $imageSrc = 'default-icon.png'; } } else { // If profile_image is empty, use the default image $imageSrc = 'images/profile_avatar.jpg'; } } // Define the base URL $base_url = "https://www.eizononline.com/"; // Output the result if ($result['type'] === 'product') { echo '<p><img src="' . $base_url . $imageSrc . '" alt="Product Image" style="width:30px;height:30px;margin-right:10px;">'; echo '<a href="' . $base_url . 'allproducts.php?highlight=' . $result['card_id'] . '">' . htmlspecialchars($result['product_name']) . '</a></p>'; } elseif ($result['type'] === 'user') { echo '<p><img src="' . $base_url . $imageSrc . '" alt="" style="width:30px;height:30px;border-radius:50%;margin-right:10px;">'; echo '<a href="' . $base_url . 'profile.php?user=' . htmlspecialchars($result['username']) . '">' . htmlspecialchars($result['username']) . '</a> (Account)</p>'; } } } else { echo '<p>Sorry, no results found.</p>'; } ?>
💾 Save
⬅ Back