javascript - 如果月份与特定月份匹配,则在选择某个月份时尝试显示多行数据

标签 javascript php html css

当我点击某个月份时,例如一月。然后它应该显示从一月份开始的数据库中的所有数据。每隔一个月都一样。我正在使用 PHP、JavaScript、HTML 和 CSS 来执行此操作。我最初有一种工作方式,但它只会显示该月特定的 1 条数据,当更改为类元素时,它停止工作。目前,没有任何显示,点击月份时也没有错误返回。

PHP:

if($row['month'] === 'January'){

                echo '<div class="rallyPrintJanuary">';
                echo "<h2>" . $row['rallyVenue'] . " in " . $row['month'] . " " . $row['year'] . "</h2>";
                echo "<h4>Event: ". $row['rallyEvent'] . " </h4>";
                echo "<h3>Your Marshall(s): " . $row['rallyMarsh'] . "</h3>";
                echo "<h4>When? ". $row['rallyDate'] . " </h4>";
                echo "<p>" . $row['rallyDesc'] . "</p>";
                echo "<p>How much? ". $row['rallyCost'] . " </p>";
                echo "<p>How long? ". $row['rallyNights'] . " Nights</p>";
                echo "<p>Pitch Limit? ". $row['pitchLimit'] . "</p>";
                echo "<p>Phone Number: 0". $row['phoneNo'] . " </p>";
                echo "<p>Email: <a href='mailto:". $row['email'] . "'> ". $row['email'] ."</a></p>";
                echo "<p>Please make sure you to contact ". $row['rallyMarsh'] . " for more information.</p>";
                if(isset($_SESSION['loggedin'])){
                    echo "<a href='' id='". $row['rallyId'] . "' class='trash'>Delete</a>";
                }
                echo "</div><br>";
                 }

HTML:

<div class="calander-container">
        <div class="months">
            January
        </div>
        <div class="months">
            February
        </div>
        <div class="months">
            March
        </div>
        <div class="months">
            April
        </div>
        <div class="months">
            May
        </div>
        <div class="months">
            June
        </div>
        <div class="months">
            July
        </div>
        <div class="months">
            August
        </div>
        <div class="months">
            September
        </div>
        <div class="months">
            October
        </div>
        <div class="months">
            November
        </div>
        <div class="months">
            December
        </div>
    </div>

JavaScript:

    document.addEventListener("DOMContentLoaded", function(event) {
            let monthArray = document.querySelectorAll(".months");

        monthArray.forEach(function(monthh){
            monthh.addEventListener("click", (e) => {
                    const monthName = document.getElementsByClassName("months").textContent;
                    const months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
                    months.forEach(month => {

                        const el = document.getElementsByClassName('rallyPrint' + month);

                        if (monthName === month) {

                                el[0].style.display = "block"; // Show current month

                        }else if(!month){
                            el[0].style.display = "none"; //Not the current month, hide
                        }   


                   });
            });
        });
    });

最佳答案

你有数据库吗? $row[] 函数从表中选择一行。所以你需要一个数据库。还有当你做 if($row['month'] === 'January')这就像在说 if(table Month === January) {}你真的不能说 Table 是否等于这个东西,例如January 做这个表并不真正等于任何东西,也许是表中的某些东西而不是表本身。哦,还有你在 JS 中拼写错误的月份可能想要解决这个问题。最后但并非最不重要的另一个原因是你的东西可能没有打印是因为你做了这个 echo "<h2>" . $row['rallyVenue'] . " in " . $row['month'] . " " . $row['year'] . "</h2>";我想打印这样一行你可能会喜欢这样的东西 echo '<p class="UserPost">' . $row["MainPagePosts"] . " | Posted By: " . $row["PostUser"] . "</p>";

关于javascript - 如果月份与特定月份匹配,则在选择某个月份时尝试显示多行数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59136792/

相关文章:

PHP:当用户上传带有不允许的扩展名的图像时,文件扩展名是空的

javascript - 在 PHP 或 JavaScript 中防止多次点击提交多个表单

javascript - 如何在输入 PIN 上打点显示要提供的字符数?

javascript - 升级后的 Excel 和现在的 Visual Studio 不允许我运行我的加载项项目

javascript - 如何在 JQuery UI Selectable 上实现切换功能?

javascript - react 响应式,在函数中使用而不是在 jsx 中

javascript - s% 如何引用 Helmet 组件内的 title 属性?

php - 无法更新我的 php 和 mysql 数据库数据

带有自定义 HTML 标签 View 的 Java HTML 渲染器

html - 将自定义元素和 Logo 添加到 Magento 2 移动菜单