<?php
include 'sitedetails.php';

?>
<!DOCTYPE html>
<!-- search.php FILE  -->
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title> Search in <?php echo $sitename;?> Ringtones BGM</title>
    <link rel="stylesheet" type="text/css" href="css/postpage.css">
    <meta name="robots" content="noindex, nofollow">

</head>
<body>
    <?php
    include_once 'databaseconnect.php';
    include_once 'searchbar.php'; // Assuming searchbar.php is safe
    include_once 'analytics.php'; // Assuming analytics.php is safe
    include_once 'header.php'; // Assuming header.php is safe

    if (isset($_GET['search'])) {
        $search = $_GET['search'];
        $search = trim($search);
        // Prepare and execute SQL query using prepared statements to prevent SQL injection
        $keywords = array_filter(explode(' ', $search));
    
        if (count($keywords) > 0) {
            // Prepare the SQL statement with placeholders for each keyword
            $likeClauses = array_map(function($keyword) {
                return "title LIKE ?";
            }, $keywords);
            
            $sql = "SELECT * FROM posts WHERE " . implode(' AND ', $likeClauses) . " LIMIT 50";
            $stmt = $con->prepare($sql);
            
            // Prepare search terms with wildcards for the LIKE clause
            $searchTerms = array_map(function($keyword) {
                return "%" . $keyword . "%";
            }, $keywords);
            
            // Create a string with 's' repeated for each keyword
            $types = str_repeat('s', count($searchTerms));
            
            // Bind the parameters to the statement
            $stmt->bind_param($types, ...$searchTerms);
            
            // Execute the statement and fetch results
            $stmt->execute();
            $result = $stmt->get_result();
            
            // Output the number of results
            $numResults = $result->num_rows;
            echo "Number of results: " . $numResults;
       
        echo "<div class='resultquery'>";
        if ($result->num_rows > 0) {
            while ($row = $result->fetch_assoc()) {
                // Sanitize output to prevent XSS attacks
                $download = htmlspecialchars($row['download link']);
                $number = htmlspecialchars($row["No"]);
                $pageurl1 = htmlspecialchars($row['pageurl']);
                $title = htmlspecialchars($row['title']);
                $videoid = htmlspecialchars($row["videoid"]);
                $duration = htmlspecialchars($row["duration"]);
                $download_count = htmlspecialchars($row["download_count"]);
                $videohtml = "<audio controls controlsList='nofullscreen nodownload noremoteplayback noplaybackrate foobar novolume' >
                <source src='$siteurl$download' type='audio/mpeg' />
              </audio>";

                echo "<div class='Result1track'>";
                echo "<div class='audiotrack1'>$videohtml</div>";
                echo "<div class='time'>$duration Seconds. $download_count Downloads.</div>";
                echo "<div class='titletrack1'><a href='$siteurl$download' class='visitpage1'>Download $title</a></div><br>";
                echo "</div>";
            }
        } }
        else {
            echo "0 records";
        }
        echo "</div>";

        $stmt->close();
    }
    
    include_once 'footer.php'; // Assuming footer.php is safe
    ?>
</body>
</html>

<script>
        $(document).ready(function() {
            $("audio").on("play", function() {
                $("audio").not(this).each(function(index, audio) {
                    audio.pause();
                });
            });
        });
    </script>


<style>
 h4{
     color: white;
 }

body 
{
     color: white;
 }
.relatedkeys{

 color:white;
}

.mp3design{

 color: greenyellow;
}










</style>

<style>
    
    .time{
        
        color:white;
        font-size:12px;
        padding-left:15px;
    }
</style>



<!-- search.php FILE  -->

