Php inside html file will not execute - php

So I have some PHP stuff inside my html doc, and I'm fairly new to web stuff so I'm kinda confused. The code in question is:
<style type="text/css">
body{
background: url(<?php include 'background.php';
echo "$selectedBg"; ?>) no-repeat center center fixed;
background: url(images/1.png)
background-size: cover;
}
</style>
The rest of the file works seamlessly, I've tried putting that little bit in all sorts of different tags, and even changing the file to a php file instead of an html one. I've verified that php is installed and my path is correct. background.php exists in the same directory as seen here, this and this are the sites I referenced when creating it. The only other info I can think to provide is that I'm viewing it through the live preview in Brackets.

Changing the file to a php file fixed it. The whole file now looks like.
<html>
<?php include 'background.php';?>
<title>Yay</title>
<head>
<link rel="stylesheet" type="text/css" href="main.css">
<style type="text/css">
body {
background: url(<?php echo $wall;?>);
}
</style>
</head>
<body>
</body>
</html>
where background.php is
<?php
$bg = array('1.png', '2.png', '3.png', '4.png', '5.png', '6.png', '7.png', '8.png');
$i = rand(0, count($bg)-1);
$selected = "$bg[$i]";
$wall = "images/$selected";
?>

Related

Embedded php css file not working

I have embedded my CSS file as PHP to make it more dynamic as shown in various examples online but I can't seem to get it working
<link rel='stylesheet' href='myphpstylesheet.php'>
My PHP file which I added in the link
<?php
$bgcolor = '#FF00FF';
?>
<style>
Div #container {
Background-color: <?php echo $bgcolor ; ?>
}
</style>
My HTML file
<html>
<head>
<link rel='stylesheet' href='myphpstylesheet.php'>
</head>
<body>
<div id="container ">
I am a div with lorem ipusum
</div>
</body>
</html>
But the color does not apply please help
I did some search on embedding css as php file,
I don't really see the reason for this just use css as intended.
Anyway you can try these methods hope it fixes you problem also take note of herf and hrefas pointed out by Riggs Folly as they are not the same.
METHOD 1
You can edit your httpd.conf or .htaccess with this line
AddType application/x-httpd-php .css
the web server will now parse PHP code that is within the CSS files but i would
avoid messing with those at all costs.
METHOD 2
You can also include a PHP file, in the same manner as you include a CSS file
<link rel="stylesheet" href="style.php" media="screen">
this should be done within the head of the HTML document that is the <head></head> tags
Then your style.php file should look something like this:
<?php
header("Content-type: text/css; charset: UTF-8");
$brandColor = "#990000";
$linkColor = "#555555";
$CDNURL = "http://cdn.blahblah.net";
?>
Then your css file should look somthing like this:
#header {
background: url("<?php echo $CDNURL; ?>/images/header-bg.png") no-repeat;
}
a {
color: <?php echo $linkColor; ?>;
}
ul li a {
color: <?php echo $linkColor; ?>;
}
You should use PHP's include command to inject the external PHP:
<?php include 'myphpstylesheet.php'; ?>
Inside that PHP file, you have a missing PHP closing tag, and the CSS needs to be declared properly within <style> tags:
<?php
$bgcolor = '#FF00FF';
?>
<style>
div #container {
background-color: <?php echo $bgcolor ; ?>
}
</style>
There's many ways to have dynamic control over CSS.
If you wanted to maintain the method, and truly feed it a dynamic PHP file, you have to make sure the PHP output is ONLY css. Here's an article on that.

How to change the body background for three different div-s

I have a php file called shtype_porosine.php and also an external css file style.css. The php file I use to login to the web site and it has three different div tags. For the first one (div id="container_dyte") the body backgraound color must be white, but for the two others that are included include('include/administrator_index_nsp.php'); and include('include/login_forma.php'); the body color must be black.
I am not sure if a can use the body tag three times in the same php document, if so than the problem is solved (with body class="" i founded in the previous question "how-to-css-if-else-to-change-body-background-color"), but if not than which would be the solution?
Below is the code of the php file shtype_porosine.php
Thank you
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>website</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<?php
session_start();
// this is for an administrator
if(isset($_SESSION['auser'])){
?>
<div id="container_dyte">
<span>Something here...</span>
</div>
<?php
}else{
// these is for a user
if(isset($_SESSION['p_user'])){
include('include/administrator_index_nsp.php');
}else{
// this is the login form to apear if fails the adminstrator and the user
include('include/login_forma.php');
}
}
?>
</body>
</html>
Check if $_SESSION[ 'auser'] is set, if it is, make a body with a white background-color.
Otherwise make a body with a black background-color.
Like this:
<?php
session_start();
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>website</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
</head>
<?php
if(isset($_SESSION['auser'])){ ?>
<body style='background-color:white'>
<?php } else { ?>
<body style='background-color:black'>
<?php }
if(isset($_SESSION['auser'])){
?>
<div id="container_dyte">
<span>Something here...</span>
</div>
<?php
}else{
// these is for a user
if(isset($_SESSION['p_user'])){
include('include/administrator_index_nsp.php');
}else{
// this is the login form to apear if fails the adminstrator and the user
include('include/login_forma.php');
}
}
?>
</body>
</html>
<body class="<?= isset($_SESSION['auser']) ? 'white' : 'black';?>">
<body style="background-color:<?= isset($_SESSION['auser']) ? 'white' : 'black';?>">
You don't have to change any "body". Just set the background-color css property for each of the div:
<div id="container_dyte" style="background-color:white">
In file include/administrator_index_nsp.php
<div style="background-color:black">
In file include/login_forma.php
<div style="background-color:black">
Easy way:
Give the divs diferent ID names and use CSS.
HTML
<div id="the_id">
CSS
#the_id {
background-color:white;
}
Medium way:
Make a container div with a specyfic ID or CLASS and set up a CSS.
CSS
#container div {
background-color: black;
}
#container:first-child {
background-color: red;
}
#container:last-child {
background-color: white;
}
Hard way:
If you have a css specyfic for this page you can select the div number by style property.
body:nth-child(1) { background-color: black; };
body:nth-child(2) { background-color: red; };
body:nth-child(3) { background-color: white; };
If the css is ment for other pages do not use this answer.
In order to solve this issue, include a custom css, which will apply a required background color to the <body> tag. For example:
if (mode=='mode1')
{
echo "<div id='div1'></div>";
echo "<style type='text/css'> body { background-color:red } </style>";
}
if (mode=='mode2')
{
echo "<div id='div2'></div>";
echo "<style type='text/css'> body { background-color:yellow } </style>";
}
if (mode=='mode3')
{
echo "<div id='div3'></div>";
echo "<style type='text/css'> body { background-color:blue } </style>";
}
So using this logic, you can set the background color of the body, according to the divs showed

how can I use of a php variable into a .css file

I have a css file named test.css and I want to use into it of $var.$var is at test.php. test.css is attached in test.php. My structure is something like this:
//test.php
<html>
<head>
<?php $var = 'anything';?>
<link href="test.css" rel="stylesheet" type="text/css" />
</head>
<body></body>
</html>
and this is test.css:
// test.css
.<?php echo $var> { // css property }
Currently test.css does not work. In fact, I want to knoe how can I use of a php variable as a class name into a css file ?
Actually you can.
1st Solution
Instead of using the .css file extension, use .php
Set up variables
<?php
header("Content-type: text/css; charset: UTF-8"); //look carefully to this line
$brandColor = "#990000";
$linkColor = "#555555";
?>
Use variables
#header {
background: url("<?php echo $CDNURL; ?>/images/header-bg.png") no-repeat;
}
a {
color: <?php echo $linkColor; ?>;
}
...
ul#main-nav li a {
color: <?php echo $linkColor; ?>;
}
2nd and short solution
Create a file and name it like style.php, then in your style.php set your styles in tags like below
style.php
<style>
.blabla{
....
}
#heeeHoo{
...
}
</style>
then include style.php to your file (test.php) like
<html>
<head>
<?php include 'style.php'; ?>
</head>
<body>
</body>
</html>
That is the correct answer. Think like inline css but that is actually in external file
You can use <style> in the PHP file.
//test.php
<html>
<head>
<?php $var = 'anything';?>
</head>
<body>
<style>
<?php echo $var; ?>
</style>
</body>
</html>
The style can also be put in the <head>.

Randomly load wallpaper via css and html

I'm a newbie on css and html and even coding. I'm currently making my own start page with a local path, which loads my bookmark on an html file. The background-image control passes through the style.css file. I've googled some solutions as to creating a php script to load random pictures over css and html, and I've tried various solutions, however it doesn't seem to work for me. So here we go:
the index.html file for passing the background-image:
<link rel="stylesheet" href="style.css" type="text/css" />
and the style.css:
body {
background-image: url("Wallpaper01.jpg") ;
}
I don't know how to change it since I downloaded the code. Also the .jpg file is in the same directory as the html and css.
Can someone please help me out to have a random image with this?
Please provide the code to do it, place to insert it, or whatever I need to, that will help a lot more than just telling me what to do.
Many, many thanks if someone can help me. Thanks!
You can try something like:
<?php
$bg = array('bg-01.jpg', 'bg-02.jpg', 'bg-03.jpg', 'bg-04.jpg', 'bg-05.jpg', 'bg-06.jpg', 'bg-07.jpg' ); // array of filenames
$i = rand(0, count($bg)-1); // generate random number size of the array
$selectedBg = "$bg[$i]"; // set variable equal to which random filename was chosen
?>
CSS (in the head)
<html>
<head>
<title></title>
<style type="text/css">
<!--
body{
background: url(images/<?php echo $selectedBg; ?>) no-repeat;
}
-->
</style>
</head>
<body>
<!-- website -->
</body>
</html>
Edit (user's comments)
<?php
$bg = array('bg-01.jpg', 'bg-02.jpg', 'bg-03.jpg', 'bg-04.jpg', 'bg-05.jpg', 'bg-06.jpg', 'bg-07.jpg' ); // array of filenames
$i = rand(0, count($bg)-1); // generate random number size of the array
$selectedBg = "$bg[$i]"; // set variable equal to which random filename was chosen
?>
<html lang="en">
<head>
<meta charset=utf-8>
<link rel="stylesheet" href="style.css" type="text/css" />
<title>Start Page</title>
<style type="text/css">
<!--
body{
background: url(images/<?php echo $selectedBg; ?>) no-repeat;
}
-->
</style>
</head>
<body>
<div id="one">01 <div class="title">General</div>
<div class="links"> Google
</div>
</div>
</body>
</html>

Is there some way that PHP can control CSS

1) database
2) php
3) css
4) html form
The user enters some properties for example background: red in the html form and it saves in the db, then php loades from the db the color option in background css... and then how can I put php variable in the css properties...
also some hostings does not allow to add handlers in .htaccess, is there other options?
Yes, it is possible.
You can serve a css file with php by setting the content-type.
<?php header('content-type: text/css; charset=utf-8'); ?>
You would output the css from the php file.
You could store values in a mySQL or other table with a form, and later retrieve them with the mySQLi or other library.
This is an example of the php script:
<?
header('content-type: text/css; charset=utf-8');
$bgColor = "#FFF"; // Get it from database, I'm setting manually for this example
?>
body{
background-color:<?php echo "$bgColor" ?>;
}
Alternatively, you could output inline css on the page. Either directly in to a tag, or inline on elements.
If really needed a native css file for some reason (I can't think of one), you could technically overwrite a specific css file using php's fwrite() function, though I wouldn't recommend doing it this way.
<?php
$filename = "phpstyle.css";
$fp = fopen($filename, 'w');
$bgcolor = "#FFF";
$css = "body{";
$css .= "background-color:".$bgcolor.";";
$css .= "}";
fwrite($fp, $css);
fclose($fp);
?>
Yes you can serve a css through php script.
One method is to output style elements through php.
<?php
#css on php file
$color = '#FFFFFF';
?>
<html>
<head>
<title>Css through php</title>
<style>
.test{
text-color: <?php echo $color; ?>
}
</style>
</head>
<body>
<p class="test">Your body </p>
</body>
</html>
Another method is to generate css dynamically in a php file and linking it on an html page.
<?php
#css on php file [test_css.php]
header("Content-type: text/css");
$color = '#FFFFFF';
?>
.test{
text-color: <?php echo $color; ?>
}
<html>
<!-- test.html -->
<head>
<title>Css through php</title>
<link rel="stylesheet" type="text/css" href="test_css.php">
</head>
<body>
<p class="test">Your body </p>
</body>
</html>
In addition to Khaleel's answer, you can put CSS in the HTML document itself, in a
<style>
...
</style>
section. You can use PHP variables to fill this in.
Or you can use inline styles in the HTML
<span style="...">...</span>
Why not just:
<?php if (myCondition) { ?>
<style>.myClass {
background: <?php echo $myColor; ?> !important;
} </style>
<?php } ?>
The myCondition triggers the insertion of in body styling. The !important tag overwrites all other existing value of that property.
Simple to understand and easy to use. Works every time.
ether create a css file using php dynamically, such as:
<link href="../styles/styles.php" type="text/css" />
or use php to write the css inline on your html file:
<html>
<head>
<style type="text/css">
body {
background-color: <?php echo $redcolor; ?>;
}
</style>
</head>
<body></body>
</html>
however, you cannot create a standard css file with php.

Categories