August 23, 200917 yr Hi, I have a small piece of PHP code that i am using... <body id="<?= basename($_SERVER['PHP_SELF'], ".php")?>"> Which works great, apart from the fact that the body id once the PHP has been executed is called 'index' (well for the homepage), which im not really after! I would much rather have it body id="home" , is there anyway to tweak this code so it would see the first page is index.php but give it an id of 'home' and then do the other pages as normal??
August 23, 200917 yr Use an array to map with the id names you want something like this... var ids = ['index.php' => 'home', 'contact.php' => 'contact']; <body id="<?= ids[basename($_SERVER['PHP_SELF'])] ?>">
August 23, 200917 yr EDIT: didn't see yours Thomas. <?php if (basename($_SERVER['PHP_SELF'], ".php") == 'index') { $id = "home"; } else { $id = basename($_SERVER['PHP_SELF'], ".php"); } ?> <body id="<?php echo $id;?>"> That should work although its not tested.
August 23, 200917 yr Author EDIT: didn't see yours Thomas. <?php if (basename($_SERVER['PHP_SELF'], ".php") == 'index') { $id = "home"; } else { $id = basename($_SERVER['PHP_SELF'], ".php"); } ?> <body id="<?php echo $id;?>"> That should work although it’s not tested. Cheers scaz, that worked a treat!! Top man!
Create an account or sign in to comment