Home > Enterprise >  display none to element in foreach if == 0
display none to element in foreach if == 0

Time:10-08

okay so i have a foreach and in that i have a more information button. I want it so that when x == 0 then the display to that more information button is 'none'

this is the variable that is diffirent in each foreach item.

$issuesFixedCount

this is the class that needs to be display none if the variable == 0.

<a href="<?='version/?version=' . $item->name; ?>" >Meer 

my code:

<div >
    <h1 >Recente Jira releases</h1>
    <div  id="release-items">
        <?php
        foreach ($items as $item):
            $proj = new ProjectService();
            $versionService = new VersionService();

            $version = $proj->getVersion('', $item->name);
            $res = $versionService->getRelatedIssues($version);
            $uns = $versionService->getUnresolvedIssues($version);

            $issuesFixedCount = $res->issuesFixedCount;
            $issuesUnresolvedCount = $uns->issuesUnresolvedCount;
            $roundedIssue = $issuesFixedCount - $issuesUnresolvedCount;

            if ($res->issueFixedCount == 0){
                echo "
                <script>
                    const moreInfoVar = document.querySelector('.moreInfo');
                    const.style.display = 'none';
                </script>";
            }
        ?>

            <div  id="item">
                <h4>Versie: <?= $item->name;  ?></h4>
                <div >
                    <p>Issues in versie: <?= $res->issuesFixedCount; ?> </p>
                    <p>Afgeronde issues: <?= $roundedIssue;?> </p>
                    <p>Nog te verwerken issues: <?= $uns->issuesUnresolvedCount;?> </p>
                    <a href="<?='version/?version=' . $item->name; ?>" >Meer info</a><br>
                </div>
                <small><?= $item->releaseDate; ?></small>
                <a >Toon meer</a>
            </div>
            <?php
        endforeach;?>
        <?php 
        $startAtValueNext = $_GET['startAt']; 
        $startAtNext = $startAtValueNext  = 5;

        $startAtValuePrevious = $_GET['startAt']; 
        $startAtPrevious = $startAtValuePrevious -= 5;

        if ($startAtPrevious <= 0){
            $startAtPrevious = 0;
        }
        ?>
    </div>
    <div >
        <a href="<?='?startAt=' . $startAtPrevious; ?>" style="float:left; display:inline;" id="navPrevious"><- Vorige</a>
        <a href="<?='?startAt=' . $startAtNext; ?>" style="float:right;" >Volgende -></a>
    </div>
</div>

CodePudding user response:

There's no need to inject JavaScript in this way. Instead, simply modify what is sent to the browser in the first place:

  <p>Nog te verwerken issues: <?= $uns->issuesUnresolvedCount;?> </p>
  <a href="<?='version/?version=' . $item->name; ?>"  <?= ($res->issueFixedCount == 0 ? 'style="display:none;"' : ''); ?>>Meer info</a><br>
</div>

P.S. document.querySelector('.moreInfo') will only ever select the first element it finds which has that class. It doesn't target the specific element you're outputting on the current row, so that's one reason why that approach doesn't work.

  • Related