Brum
Brum

Reputation: 691

Checking if div contains p element with php

I was wondering if it is possible to see if a parent element has a child element with php? I know it is possible with jQuery, but I need to do it with php because I need to make a check that displays my content differently based on the presence of that child element.

What I now have is:

<?php if ($showLeftColumn) : ?>
            <aside class="sppb-col-md-3 custom-style-left">
                <jdoc:include type="modules" name="left-top" style="xhtml" />
                <jdoc:include type="modules" name="left-center" style="xhtml" />
                <jdoc:include type="modules" name="left-bottom" style="xhtml" />
            </aside>
        <?php endif; ?>

This works now, but it still shows when empty because the position is loaded in standard. What I want is that if the position is loaded then it gets into the first if and then does a check if the child element exists in the parent element.

Does anyone know if this is possible by maybe getting the dom structure or a other easier way?

Upvotes: 0

Views: 1925

Answers (2)

Paul van Haren
Paul van Haren

Reputation: 116

Please allow me to reformulate your question. Your approach is first to create a problem (create the empty aside), and then to clean it up (find out if the aside is empty), while you can also avoid the problem all together.

Joomla provides the method countModules(), that allows you to make your code dependent on what is to come. You can then rewrite your code as

<?php if (($showLeftColumn) &&
        ($this->countModules("left-top") + 
         $this->coutnModules("left-center") +
         $this->countModules("left-bottom"))): 
?>
        <aside class="sppb-col-md-3 custom-style-left">
            <jdoc:include type="modules" name="left-top" style="xhtml" />
            <jdoc:include type="modules" name="left-center" style="xhtml" />
            <jdoc:include type="modules" name="left-bottom" style="xhtml" />
        </aside>
    <?php endif; ?>

Upvotes: 0

user3490122
user3490122

Reputation: 29

The following should solve your problem.

<?php
        $content = '<aside class="sppb-col-md-3 custom-style-left">
                            <jdoc:include type="modules" name="left-top" style="xhtml" />
                            <jdoc:include type="modules" name="left-center" style="xhtml" />
                            <jdoc:include type="modules" name="left-bottom" style="xhtml" />
                        </aside>';
        if (strpos($content, '<p') === false) {
          //Don't show content, maybe show something else?
        } else {
          echo $content;
        } ?>

Upvotes: 1

Related Questions