0

I am fairly new to PHP, and I have started working on a small project. I want my code to be organized, so I decided to include the file container.phtml in my index.php file. So when $page = 'login' it would include the login.css and login.phtml file. I want login.css to apply to login.phtml. Instead when I run the program login.css does not load or produce any error messages in the console. Does the login.css file only apply to container.phtml? What can I do differently while maintaining the way I want this to work?

EDIT: The $page variable tells index.php what file it should include. Example: $page = 'login'; That tell it to get the login.phtml file. Then, in container.phtml it will check to see if $page = 'login' and then will include login.css

index.php

<?php

if(!isset($_GET['page'])){
    $page = 'home';
}else{
     $page = $_GET['page'];
}

/* Fetch needed files. */
require_once 'app/paths.php';
require_once THEME_LAYOUT_PATH . 'container.phtml';

$pages = array(
'home',
'login',
'register'
);

$layoutPath = 'themes/neutron/layout/' . $page . '/' . $page . '.phtml';

if(in_array($page, $pages)){
    require $layoutPath;
}else{
    require 'themes/neutron/layout/404/404.phtml';
}
?>

container.phtml

<html>
<title>Neutron</title>
<link rel = "stylesheet" type = "text/css" href = "<?php ASSETS_PATH . 'css/bootstrap.min.css' ?>">
<link rel = "stylesheet" type = "text/css" href = "<?php ASSETS_PATH . 'js/bootstrap.min.js' ?>">
<link rel = "stylesheet" type = "text/css" href = "<?php ASSETS_PATH . 'js/jquery.min.js' ?>">
<?php 
/* Include header, footer, etc */
require_once THEME_LAYOUT_PATH . 'partials/header.php';
require_once THEME_LAYOUT_PATH . 'partials/footer.php';
?>
<?php if($page == 'login'){ ?>
<link rel = "stylesheet" type = "text/css" href = "<?php ASSETS_PATH . 'css/login.css' ?>">
<?php } ?>


</html>

login.phtml

<div class="login-clean" style="background-color: rgb(255,255,255);">
    <form method="post">
        <h2 class="sr-only">Login Form</h2>
        <div class="illustration"><i class="icon ion-ios-navigate" style="color: #4777f4;"></i></div>
         <div class="form-group"><input class="border rounded form-control" type="email" name="email" placeholder="Username"></div>
         <div class="form-group"><input class="border rounded form-control" type="password" name="password" placeholder="Password"></div>
        <div class="form-group"><button class="btn btn-primary btn-block border rounded" type="submit" style="background-color: #4777f4;">Log In</button></div><a class="forgot" href="#">Forgot Password?</a></form>
</div>

3 Answers3

0
$page = basename($_SERVER['PHP_SELF']); // Current page file name.

This returns the current page file name to the variable $page, such as index.php, login.phtml, etc.

Use this in conjuction with an if statement and this way you can define which stylesheets get imported.

if ($page == "index.php")
{
    echo '<link rel="stylesheet" type="text/css" href="css/style.css" />';
}
else
{
    echo '<link rel="stylesheet" type="text/css" href="css/style.css" />';
}
Skully
  • 2,882
  • 3
  • 20
  • 31
  • I am not sure what you are trying to tell me here. The $page variable tells index.php what file it should include. Example: $page = 'login'; That tell it to get the login.phtml file. Then, in container.phtml it will check to see if $page = 'login' and then will include login.css. –  Jul 31 '20 at 00:52
0

You can use a switch statement to act on certain values

switch($page) {
   case 'login':
      include 'LOGIN-FILE';
      break;
   default:
      include 'index.php';
      break;
}
Thrallix
  • 699
  • 5
  • 20
-1

What is in ASSETS_PATH? Seems to me your error could be that ASSETS_PATH is not an url?

Does the login.css file only apply to container.phtml?

Any loaded css will apply to the whole html.

Fred
  • 74
  • 4
  • Here are the paths I have defined: define('ASSETS_PATH', THEME_PATH . 'assets/'); The problem isnt ASSETS_PATH and Im pretty sure it wasn't in the first place. So the CSS that was included in container.phtml, will apply to login.phtml based on my code? It did not work for me. –  Jul 31 '20 at 01:40
  • can you post here to content of the console errors you mentioned? – Fred Jul 31 '20 at 01:49
  • PS. You should put $layoutPath = 'themes/neutron/layout/' . $page . '/' . $page . '.phtml'; in the if (in_array()) to avoid warnings – Fred Jul 31 '20 at 01:51
  • I did not get any console errors, yet no styles took effect in login.phtml –  Jul 31 '20 at 01:53
  • seems to me like a path/url problem can you print the results of THEME_PATH – Fred Jul 31 '20 at 01:59