Lunexsoft Lunexsoft - 5 months ago 8
PHP Question

How to define a class name based on specific page in php?

I am trying to create a body class name based on a specific page in php. What I need to do is first define a variable and then check if the variable exists, and then if it exists display that one, otherwise if it is something else, then do something else, or if nothing then do not show any.

<body<?php if (defined('PAGE_KEY') && var == "homepage") echo " class=\"homepage\"";
elseif (defined('PAGE_KEY') && var == "page3") echo " class=\"page3\"";
elseif (defined('PAGE_KEY') && var == "page12") echo " class=\"page12\""; ?>>


This code will be located in my
head.php
.

My thought was to first check if variable was defined and if so then check what the variable was defined as and then based on that variable it will display a respective class.

The goal is for example on the page12 for the body tag to look like this:

<body class="page12">


But for example for the body tag on page55 (which I don't want to show a class for) to look like this:

<body>


In doing so I am able to now define css specifically for a page within the header where the body tag happens to be located.

Problem is first I don't know how to define the variable in the page, and second I don't know exactly how to write the php code above properly.

Attempt, for example on page12 I would have this code:

<?php PAGE_KEY = "page12" ?>


This code would be for example located in
page12.php
.

Also keep in mind, that the variable will come AFTER the body tag.

I also thought of trying to see what the page URL is but I think that's just making things too complicated.






Based on @Jordi's suggestion, how about this:

<body class="<?php echo PAGE_KEY ?>">


on
head.php
.

And then on
page12.php
, this:

<?php PAGE_KEY = "page12" ?>


and for example on
page5.php
this:

<?php PAGE_KEY = "page5" ?>


so that on those respective pages the body tag shows this:

on
page5.php
:

<body class="page5">


and on
page12.php
the body tag will show this:

<body class="page12">


Is this right?




@Jose made this suggestion, is this correct?

for example, on
page12.php
, to define the variable as 'page12', this:

<?php define("PAGE_KEY", "page12"); ?>


Is that what you were suggesting to do?






Further Note:

Ok, so it seems the problem I'm having is that since the code to define the page comes after the
<body>
tag, which the is located inside head.php
and for example
page5.php
code comes after
head.php` code, then it's not working because of that.

I looked in the code and it looks like for other things, the original page programmer did something different to circumvent this issue. I could be wrong, but it looks like first they stored the variable in the mysql database, for the page; and then in head.php checked to see if that was defined and if so then run code.

Here for example is a line of their code they used for other purposes, that is found on the FAQ page:

define("PAGE_NAME", t("faq_page_name", "FAQ"));


So, what I need to do is replicate it.

It is also worth noting that because of this, each page does have a unique identifier. For example, the FAQ page has the identifier "FAQ". But I would not know how to fetch that.

Maybe this?

<body<?php echo " class=\"" . constant( "PAGE_NAME" ) . "\""; ?>>


This would make the class name the identifier; f course I need something unie so maybe I could append it, like this?

<body<?php echo " class=\"" . constant( "PAGE_NAME" ) . "\"-zz"; ?>>


So for like faq page which has the variable "FAQ" this would result in the class like this:

<body class="FAQ-zz">


The only problem is that I am not sure if my code above is forrect for fetching the variable name.

I could work with this if I can get it to work. I would just only need to edit the stylesheet, i.e. instead of
.page5{}
I would change it to
.FAQ-zz {}

Answer

You can use and define a constant like this :

<?php
define( "PAGE_KEY","homepage" );
?>
.
.
.
<body<?php echo " class=\"" . constant( "PAGE_KEY" ) . "\""; ?>>

For another page :

<?php
define( "PAGE_KEY","page5" );
?>
.
.
.
<body<?php echo " class=\"" . constant( "PAGE_KEY" ) . "\""; ?>>

You only change the constant, the rest is the same for every page.

Edit #1:

<body
  <?php
  define( "PAGE_KEY","homepage" );
  echo " class=\"" . constant( "PAGE_KEY" ) . "\"";
  ?>
>

Edit #2:

<?php
define( "PAGE_KEY","homepage" );
?>
.
.
.
<?php
include( "head.php" >
?>

Now, head.php is something like this :

echo "<body class=\"" . constant( "PAGE_KEY" ) . "\">";