OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How would I include a specific CSS file if a variable equals a certain value?

  • Thread starter Thread starter user14024732
  • Start date Start date
U

user14024732

Guest
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

Code:
<?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

Code:
<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

Code:
<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>
<p>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 <em>container.phtml</em> in my <em>index.php</em> file. So when $page = 'login' it would include the <em>login.css</em> and login.phtml file. I want <em>login.css</em> to apply to <em>login.phtml</em>. Instead when I run the program <em>login.css</em> does not load or produce any error messages in the console. Does the <em>login.css</em> file only apply to <em>container.phtml</em>? What can I do differently while maintaining the way I want this to work?</p>
<p><em><strong>EDIT:</strong></em> 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</p>
<p><strong>index.php</strong></p>
<pre><code><?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';
}
?>
</code></pre>
<p><strong>container.phtml</strong></p>
<pre><code><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>
</code></pre>
<p><strong>login.phtml</strong></p>
<pre><code><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>
</code></pre>
Continue reading...
 

Latest posts

Top