Jump to content

How to get this class variable in this tag?


Frank P

Recommended Posts

Depending on a set value in the URL, the html element should get a class. In this way it works:

 

include-test.php:

<?php require("include-test-included.php"); ?>
<!DOCTYPE HTML>
<html class="<?php echo makeClass(); ?>">
<head>
    <meta charset="utf-8">
    <title>Demo</title>
    <style>
    html.normal {
        background: white;
    }
    html.touch {
        background: red;
    }
    </style>
</head>
    <body>
    </body>
</html>

include-test-included.php:

<?php
function makeClass() {
    if(!isset($_GET['type']))
        { $type = 'normal'; }
    elseif (isset($_GET['type']))
        { $type = $_GET['type']; }
    return $type;
}
?>

 

But I would very much like to be able to do it this way, because I have a number of pages, and the head section is already largely PHP-generated:

 

include-test.php:

<?php require("include-test-included.php"); ?>
<?php echo $part_1 ?>
    <title>Demo</title>
    <style>
    html.normal {
        background: white;
    }
    html.touch {
        background: red;
    }
    </style>
</head>
    <body>
    </body>
</html>

 

include-test-included.php:

<?php
function makeClass() {
    if(!isset($_GET['type']))
        { $type = 'normal'; }
    elseif (isset($_GET['type']))
        { $type = $_GET['type']; }
    return $type;
}

$part_1 =
'<!DOCTYPE HTML>
<html class="<?php echo $type; ?>">
<head>
    <meta charset="utf-8">
'
?>

 

However, this way I get an undefined-variable warning. Which makes sense, because it is outside the function. But how do I get this class variable in the html tag? I have been fiddling for a couple of hours already with backslashes, single/double quotes and echoing the function, but have not found the right way yet. 

 

Which is the right way?

Thanks for jumping so quickly! :-*

 

I was mistaking, I got the undefined-variable warning in another configuration. I don't know which one again, because I have tried so many combinations. This is what I get as a result in the above (latter) configuration: a literal echoing of the line: 

<html class="<?php echo $type; ?>">

You mean like this:

<?php
function makeClass() {
if(!isset($_GET['type']))
{ $type = 'normal'; }
elseif (isset($_GET['type']))
{ $type = $_GET['type']; }
return $type;
}

$part_1 =
'<!DOCTYPE HTML>
<html class=\'echo $type\'>
<head>
    <meta charset="utf-8">
'

or:

<?php
function makeClass() {
if(!isset($_GET['type']))
{ $type = 'normal'; }
elseif (isset($_GET['type']))
{ $type = $_GET['type']; }
return $type;
}

$part_1 =
'<!DOCTYPE HTML>
<html class="echo $type">
<head>
    <meta charset="utf-8">
'

?

 

Unfortunately, that doesn't work either. In both cases I still get a literal echoing of $part_1 as a result. No class="normal" or class="touch".

Got it! I saw your example and suddenly realized that the + in javascript, with which I primarily work, is not  a + but a . in PHP! This is how it works the way I want it to:

<?php
function makeClass() {
    if(!isset($_GET['type']))
        { $type = 'normal'; }
    elseif (isset($_GET['type']))
        { $type = $_GET['type']; }
    return $type;
}

$part_1 =
'<!DOCTYPE HTML>
<html class="'.makeClass().'">
<head>
    <meta charset="utf-8">
'
?>

Again, thanks for jumping in so quickly!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.