Jump to content

How to get this class variable in this tag?


Frank P
Go to solution Solved by 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?

Edited by Frank P
Link to comment
Share on other sites

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; ?>">
Link to comment
Share on other sites

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".

Edited by Frank P
Link to comment
Share on other sites

  • Solution

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.