-1

I´ve got this code snipped:

<?php
        if (class_exists('countdown')) {
            $myclass = "counter";
        }else{
            $myclass = "";
        }
        ?>

        <div class="jumbotron <?php echo $myclass; ?>
            <div class="bg_wrapper">
                <div class="bg_banner"
                    <div class="container">
                            <div class="content_wrap"> 
                                <ul class="countdown">
                                </ul>
                            </div>
                    </div>
                </div>
            </div>
        </div>

What I want to do is, PHP need to check at the DOM if the div with the class "countdown" exits and if yes, create a new one.

Can you help me, with class_exists its not working.

Thank

Marten
  • 1,376
  • 5
  • 28
  • 49
fr3d
  • 665
  • 1
  • 5
  • 17
  • 3
    css class differs from php class, this is not the same thing. – fxlacroix Jun 14 '17 at 13:12
  • 1
    if you want to do some check like this, you should make it with xpath, domdocument, or regexp. – fxlacroix Jun 14 '17 at 13:13
  • Yeah or you try it with js – A. Blub Jun 14 '17 at 13:14
  • 4
    PHP processes the file before the page is output, so it can't access the DOM directly. This would be better done with javascript to check the DOM, and ajax if needed to process anything server-side. – aynber Jun 14 '17 at 13:15
  • PHP is not part of the DOM - it all happens *before* anything is going on in the browser so it knows *nothing* about CSS classes. `class_exists()` refers to PHP classes - this might help : https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming – CD001 Jun 14 '17 at 13:15
  • Ok, thanks guys, I will try it with JS. :) – fr3d Jun 14 '17 at 13:23
  • Javascript should handle that – John Wick Jun 14 '17 at 13:26
  • 1
    PHP is currently in the process of building "the DOM" (read: the HTML for it). **You should know whether you're currently building a `'countdown'` or not without needing to check the HTML you're building for it.** – deceze Jun 14 '17 at 13:27

1 Answers1

3

The class_exists() function is a PHP function, which doesn't know anything about your HTML or CSS. PHP does something on your webserver, and your webserver sends everything (HTML) to your browser. From that moment, you need JavaScript to manipulate your DOM.

Read about class_exists() on: http://php.net/manual/en/function.class-exists.php

You can use jQuery (a JavaScript library) to check if a CSS class exists:

if ($('.countdown').length > 0) {
    // exists
}

If your class exists you can add the class 'counter' to your jumbotron:

if ($('.countdown').length > 0) {
    $('.jumbotron').addClass('counter');
}

You should place this code in a document ready handler, so it will be executed automatically when your page is loaded:

$(document).ready(function () {
     if ($('.countdown').length > 0) {
         $('.jumbotron').addClass('counter');
     }
}

Don't forget to include jQuery to your page, otherwise it won't work:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

And for more information about DOM manipulation using jQuery: http://api.jquery.com/

Marten
  • 1,376
  • 5
  • 28
  • 49
  • 1
    A link to the documentation page of the PHP function [`class_exists()`](http://php.net/manual/en/function.class-exists.php) is worth one thousand words (and an upvote ;-)). – axiac Jun 14 '17 at 13:34