Search code examples
javascriptphpdomparent

PHP equivalent to the JavaScript parentNode Property


I'd like to check the id of the parent element, to display either the big or the small version of an image.

<div id="content">
  <?php include 'test.php';?>
</div>
<div id="sidebar">
  <?php include 'test.php';?>
</div>

In test.php, an if-statement checks if the parent has the demanded id. Is there a way to get the parentnode-id without JavaScript?


Solution

  • No. PHP do nothing with the rendered page. But why don't you create a function and call that with parameters?

    include 'test.php'
    ?>
    <div id="content">
      <?php showImage(); ?>
    </div>
    <div id="sidebar">
      <?php showImage(true); ?>
    </div>
    
    <?php
    

    And in test.php

    function showImage($smallImage = false) {
        if (!$smallImage) {
            echo '<img src="path/to/bigimage" alt="" />';
        } else {
            echo '<img src="path/to/smallimage" alt="" />';
        }
    
    }
    

    Or there are an alternative way. Set a variable before you include your file, and you can set an if condition in your test.php when you want to show the image.

    <div id="content">
        <?php
        $parentNode = 'content';
        include 'test.php'
        ?>
    </div>
    <div id="sidebar">
        <?php
        $parentNode = 'sidebar';
        include 'test.php'
        ?>
    </div>