php 要使用按钮将类添加到具有特定ID的列表中

cl25kdpy  于 2022-12-25  发布在  PHP
关注(0)|答案(1)|浏览(102)

最后的列表看起来像这样,通过点击绿色按钮,我只需要添加类到上面的列表中。
Click here

<ul class='wrapper'> 
<?php foreach ($items as $item) : ?>
    <li class="note" id='<?= $item['id'] ?>'>
        <div class="details">
            <p><?= $item['title']."<span class='currentDate'>".$item['currentDate']."</span>"?></p>
            <p class ="importance"><?= $item['importance'] ?></p>
            <div class="border"></div>
            <span><?= $item['text'] ?></span>
        </div>
        <div class="bottom-content">
            <span>Do: <?= date('d.m.Y', strtotime($item['date'])) ?></span>

            <form method="post">
            <button name="doneButton" value='<?= $item['id'] ?>' type="submit">✅</button>
            </form>

            <?php
                if(isset($_POST['doneButton'])) {
                    //
                    

                    

                    //
                }
            ?>

            <form method="post" >
            <button value="<?= $item['id'] ?>" name="deleteButton" type="submit">🗑️</button>
            </form>
            <?php
                if(isset($_POST['deleteButton'])){
                    db_remove_note($_POST['deleteButton']);
                    header("Location: list-notes.php");
                }
            ?>
            
        </div>

    </li>
<?php endforeach; ?>
</ul>

我想用这个doneButton给li添加一个类,这个类有特定的ID($item ['id']),我只需要使用PHP,不需要JavaScript或者MySQL。

t1rydlwq

t1rydlwq1#

只需在li class属性中添加条件即可,我个人会在设置$_POST['doneButton'])的条件下使用如下三元运算符添加MYNEWCLASS:

<li class="note<?=(isset($_POST['doneButton'])?" MYNEWCLASS":"");?>" id='<?= $item['id'] ?>'>

编辑:通过特定ID选择LI

<?php
<ul class='wrapper'> 
<?php foreach ($items as $item) : ?>

    <?php
        $class = ""; //Declare an empty $class
        if (isset($_POST['doneButton']) && $_POST['doneButton'] == $item['id']){
            // If $_POST['doneButton'] is set, and $_POST['doneButton'] is equal to the id of the current iteration then set $class to "MYNEWCLASS"
            $class = " MYNEWCLASS"; //THE SPACE IS IMPORTANT
        }
    ?>

    <li class="note<?=$class;?>" id='<?= $item['id'] ?>'>
        <div class="details">
            <p><?= $item['title']."<span class='currentDate'>".$item['currentDate']."</span>"?></p>
            <p class ="importance"><?= $item['importance'] ?></p>
            <div class="border"></div>
            <span><?= $item['text'] ?></span>
        </div>
        <div class="bottom-content">
            <span>Do: <?= date('d.m.Y', strtotime($item['date'])) ?></span>

            <form method="post">
            <button name="doneButton" value='<?= $item['id'] ?>' type="submit">✅</button>
            </form>

            <form method="post" >
            <button value="<?= $item['id'] ?>" name="deleteButton" type="submit">🗑️</button>
            </form>
            <?php
                if(isset($_POST['deleteButton'])){
                    db_remove_note($_POST['deleteButton']);
                    header("Location: list-notes.php");
                }
            ?>
        </div>
    </li>
<?php
endforeach;
?>
</ul>

相关问题