MOON
Server: Apache
System: Linux ip-208-109-13-31.ip.secureserver.net 3.10.0-1160.119.1.el7.tuxcare.els4.x86_64 #1 SMP Sat Aug 31 06:58:57 UTC 2024 x86_64
User: durgeshpandey215 (1013)
PHP: 8.1.29
Disabled: NONE
Upload Files
File: /home/durgeshpandey215/public_html/ipsol.skilladders.com/ajax_search.php
<?php
require_once 'controller/DB_Config.php';

// Allow from any origin if needed, or stick to same origin
header('Content-Type: application/json');

$con = OpenCon();

$search = isset($_GET['q']) ? trim($_GET['q']) : '';

if(empty($search)) {
    echo json_encode([]);
    CloseCon($con);
    exit;
}

// We only want courses that have a parent_course_id (meaning they are actual courses, not categories)
$sql = "SELECT id, name, page_url FROM courses WHERE delete_status = 'N' AND parent_course_id IS NOT NULL AND (name LIKE ? OR title LIKE ?) LIMIT 8";
$stmt = $con->prepare($sql);

$like_search = "%" . $search . "%";
$stmt->bind_param("ss", $like_search, $like_search);
$stmt->execute();
$result = $stmt->get_result();

$courses = [];
while($row = $result->fetch_assoc()) {
    $courses[] = [
        'id' => $row['id'],
        'name' => $row['name'],
        'url' => 'course/' . $row['page_url'] . '.php'
    ];
}

$stmt->close();
CloseCon($con);

echo json_encode($courses);
?>