Sindbad~EG File Manager

Current Path : /home/u625735752/domains/floralwhite-woodpecker-723030.hostingersite.com/public_html/1.11/
Upload File :
Current File : /home/u625735752/domains/floralwhite-woodpecker-723030.hostingersite.com/public_html/1.11/news.php

// ... (PHP SEO/head setup remains the same as before)
?>
<head>
    <?php include 'head.php'; ?>
</head>
<body>
<?php include 'header.php'?>

<section class="news-section py-5 text-white">
    <div class="container ">
        <div class=" motion-features">
            <div class="row align-items-center mb-4">
                <div class="col-lg-8" data-aos="fade-right">
                    <h2 class="display-5 fw-bold mb-4">News</h2>
                </div>
                <div class="col-lg-4 text-lg-end text-start" data-aos="fade-left">
                    <select id="sortSelect" class="form-select w-auto border-light ms-auto mb-4" aria-label="Sort news">
                        <option value="latest" selected>Sort by Latest</option>
                        <option value="popular">Sort by Popular</option>
                    </select>
                </div>
            </div>

            <div class="row g-4" id="news-container"></div>

            <div class="text-center mt-4">
                <button id="loadMoreNews" class="btn btn-outline-light btn-sm rounded-pill px-4 mt-3 browse-btn">Load More</button>
            </div>
        </div>
    </div>
</section>

<script>
    const newsContainer = document.getElementById('news-container');
    const loadMoreBtn = document.getElementById('loadMoreNews');
    const sortSelect = document.getElementById('sortSelect');

    let offset = 0;
    const limit = 3;
    let sort = 'latest';
    let endOfNews = false;

    async function fetchNews() {
        if (endOfNews) return;

        try {
            // fetch-news.php must return the 'id' field
            const res = await fetch(`fetch-news.php?offset=${offset}&limit=${limit}&sort=${sort}`);
            const data = await res.json();

            if (data.length === 0) {
                endOfNews = true;
                loadMoreBtn.style.display = 'none';
                return;
            }

            data.forEach(news => {
                const col = document.createElement('div');
                col.className = 'col-md-4';

                const formattedDate = new Date(news.news_date).toLocaleDateString('de-CH', {
                    day: '2-digit', month: 'long', year: 'numeric'
                });
                
                // --- CRITICAL CHANGE: LINK USES ID (e.g., /news/123) ---
                col.innerHTML = `
                    <a href="news/${news.id}" class="text-decoration-none text-white h-100 w-100 d-block">
                        <div class="card text-white h-100 border-0">
                            <div class="motion-img mx-auto mt-auto">
                                <img src="images/news/${news.image}" class="img-fluid" alt="${news.heading_normal}">
                            </div>
                            <div class="text-center motion-text-container flex-grow-1 d-flex flex-column align-items-center p-3">
                                <h4 class="motion-title a1">${news.heading_normal}</h4>
                                <p class="motion-text a1">${news.intro}</p>
                            </div>
                            <div class="d-flex justify-content-between text-muted small mb-2 p-2">
                                <span class="text-white">📅 ${formattedDate}</span>
                            </div>
                        </div>
                    </a>
                `;
                newsContainer.appendChild(col);
            });

            offset += data.length;
        } catch (err) {
            console.error('Error loading news:', err);
        }
    }

    loadMoreBtn.addEventListener('click', fetchNews);
    // ... (rest of the JavaScript functions)
    sortSelect.addEventListener('change', () => {
        sort = sortSelect.value;
        offset = 0;
        endOfNews = false;
        newsContainer.innerHTML = '';
        loadMoreBtn.style.display = 'block';
        fetchNews();
    });

    // Initial load
    fetchNews();
</script>

<?php include 'footer.php'?>

</body>
</html>

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists