How to insert css code in php? - php

I know how to insert html code in php using print <<<HERE HERE; construction. It means I can insert a link to css file. But is it possible to insert css code itself?

There are a couple of ways you can achieve this.
Lets say you have an anchor tag My Href and you want to add CSS to it based on certain instances.
You can easily echo that css into the anchor element like so:
<a <?php echo 'style="float: left;"'; ?> href="..">My Href</a>
The output would be:
<a style="float: left" href="...">My Href</a>
You can do this with almost any HTML element.
Now let's say you have a predefined CSS class you would like to echo into an anchor tag element.
Let's say you have this CSS class in your stylesheet:
.floatLeft {
float: left;
}
You can simply echo that class into the anchor element like so:
My Href
Which the output of that would be:
My Href

Yes. It is possible.
There's actually two different ways.
Method 1: Inject PHP into your actual CSS. This is done by making your file .php, using regular HTML and CSS, and then doing as such:
.someclass {
/* Some code */
<?php // Whatever you wanna inject
?>
I have performed this many times, and no one knows. It may not be the most efficent way, but it's also sometimes better than doing...
Method 2: Escape your quotes and lines.
Use:
echo("/* Your css code here */ \ (to escape the line return
/* Whenever there is a quote, make sure to do the following: \" ");
If you don't escape the quote, PHP will think you're ending the string. That's bad, because the rest of your text gets ignored.
ADDITIONAL NOTE: You don't have to use print for echoing HTML. Echo works too.

Yes, you can easily do that. You can either do it in the CSS file like 'ilarsona' said, or you can do it in the HTML file itself.
For example:
<!DOCTYPE html>
<html>
<head>
<title>The Title</title>
<style>
p {<?php *Insert dynamic styles here* ?>};
</style>
</head>
<body>
<div style=<?php *Insert styles here* ?>> </div>
</body>
</html>

Related

Disable output of hardcoded HTML tags in a PHP file

I am working with a software written in PHP that I have no control over. Some of the PHP files have hardcoded HTML tags in them that I'd like to suppress or exchange with my own code.
Fortunately there is a plugin-system that allows me to execute my own code at certain points, here's an example of the files I'm dealing with:
<!DOCTYPE html>
<html>
<?php
execute_plugin_code_1()
?>
<head>
<!-- some additional tags -->
</head>
<?php
execute_plugin_code_2()
?>
<body>
</body>
</html>
What I want to do here is generate my own <head> tag. I don't want to alter the file itself so I'd like to suppress the hardcoded tag and its children.
Is there any way to disable the output of hardcoded HTML between execute_plugin_code_1() and execute_plugin_code_2()?
To answer your question, no, you cannot "disable" the HTML tags. You would need to remove them from the file, or use an entirely different file. The structure of this file is pretty odd though. I'm not sure what the original developer was trying to allow by providing hooks that run just before, and just after the head of the HTML document. It would have made much more sense to provide a hook within the head and within the body.
Do you want to avoid jquery? If not, then
$(document).ready(function(){
$('head').remove();
)};
https://api.jquery.com/remove/
Then you'd need to also use jquery to insert your new head tag's html. I'd be very wary, though. Seems like it'd be pretty easy to break the page altogether.

How can I add style effect for elements inside of echo using classes?

I have a problem with adding style effect for div elements inside of echo, here's my code:
<?php
$db= new PDO('mysql:host=localhost;dbname=myDataBase;charset=utf8','user','password');
$response=$db->query('select * from users order by ID desc limit 10');
while($line=$reponse->fetch())
{
echo
'<div class="elements" ><h1>'.$line['pseudo'].'</h1></div>
<div class="elements">'.$line['message'].'</div>
</div>';
} ?>
and the stylesheet:
.elements{
display: inline-block;
padding: 10px;
}
when I execute it, it doesn't work.
This is pretty easy. Just inline your php as needed into the html code to display on the page. For Example:
<?php
Inlcude your php and queries here in this block
?>
<!DOCTYPE html>
<html>
<head>
<title>Test Page</title>
<link rel="stylesheet" href="css/styles.css" />
</head>
<body>
<?php echo '<div class="elements">the rest of your php code goes here</div> '; ?>
</body>
</html>
This is just bare bones basic code to show you what I am talking about. So your custom css would live in the styles.css file. Once you echo out what it is you want in php, then it will inherit those styles. You can inline php as much as you want throughout the file. Just name it file.php or whatever you want.
This will allow you to do what it is you are asking. Of course you could reverse this and echo out the html from start to finish if you really wanted too, but it would be easier to just add the relevant connection info for php and query at the top of the page before any html and then echo out the specific information you wish inside the html below the main php code.
Also I noticed you have one too many closing div tags at the bottom of your echo statement. As others have already commented, you have some other errors as well.

Replace <body> tag with another tag

I'm trying to use the following code to replace <body> tag from page with <body id="khanqah">
echo str_replace("%body%", "khanqah", "<body id='%body%'>");
It does adds <body id="khanqah"> to the page but the actual <body> tag still presents. I mean there are two body tags now, one <body> and the other <body id="khanqah">
Also the <body id="khanqah"> tag is adding at the top of page, see this: http://i.imgur.com/6zYWTv8.jpg (screenshot of page source)
Is there any way I can work around?
It's not really replacing anything in the HTML, it's just echoing the return value of str_replace("%body%", "khanqah", "<body id='%body%'>") which happens to be the string <body id="khanqah">.
You can only replace the HTML's body element with PHP if you are outputting the HTML with PHP (changing it before outputting it). PHP works server-side, so once the HTML reaches the client it cannot modify it.
You can use JavaScript, which works client-side, to do this.
To change the id of the body dynamically using jQuery (which is the easiest way), you can do
$('body').attr('id', 'khanqah');

Sharing a code fragment all over the website

So, if I have a website containing two or more webpages with some same code fragment (e.g. side menu or top bar), how can I store this fragment in one place for using by all the webpages on the website?
I tried to put that repeating code to separate php file, like:
<?php echo "<div id='menu'> ... </div>";
and then just use
<?php include "menu.php" ?>
but problems occur with double-quotes inside double-quotes (considering I have PHP and JS scripts inside that "central" one, it's even more trouble), interpretation and so on.
What should I use (preferable HTML and PHP tools) to achieve that "code sync"?
Why are you echoing HTML?
How can I put double-quotes inside double-quotes?
You can escape the double-quote by prepending a backslash:
echo "<div id=\"some-id\">";
But, you're dealing with the wrong problem!
You're going along the right lines with re-using code, but you don't need to echo HTML. Any HTML outside of PHP tags will parse as regular HTML anyway, so just do it like this:
<div id='menu'>
...
</div>
Then require_once('menu.php'); to import the file. This way, you won't have to mess around with escaping nested quotes.
What if I have dynamic content in my menu?
Good question, then use something like this:
<div id='menu'>
<?php foreach($menuitems as $menuitem): ?>
<div id='<?php echo $menuitem['id']; ?>'>
<?php echo $menuitem['text']; ?>
</div>
<?php endforeach; ?>
</div>
By keeping the PHP and the HTML in separate tags, you create code which is easy to parse, easy to read and easy to maintain.
Similarly, don't use inline JavaScript
Keep your js in separate files, and call it from within your HTML like this:
<script src="script.js"></script>
You know you can just include 'menu.html' or include 'menu.php' without echo just using html tags and adding any php functionality inline like you would normally.

add a php variable to a css statement!

I would like to include the following in my css style statment. is it possible?
<option selected value="" class="" style= background: url<?php some php variable; ?></option>
If that portion of HTML code is in a .php file, yes, it is possible : PHP does just generate the output, no matter if the textual data is actually corresponding to CSS, HTML, JS, ...
Note though that your HTML code, here, is not quite OK : you are missing quotes arround the content of the style attribute, you should add parenthesis arround the url, and should use selected="selected".
Also note that you need to echo the content of the variable -- and not just use its name.
So, basically, you should have something like this :
<option selected="selected" value="" class=""
style="background: url(<?php echo $your_variable; ?>)">
text of your option
</option>
Martin's answer is just fine, but I felt I should I point out that they way you are mixing CSS into the HTML isnt a good idea. Keeping the CSS part out of HTML file is always a better suggestion.
In case you would like the stylesheet to be generated for each request depending on certain values, (for eg, you need to pull values from the database), you could simply have a CSS file, and inside PHP tags you could pull the data and put it in the right place, like you would do for HTML.
For eg:
In HTML:
<link rel="stylesheet" type="text/css" href="style.php?user=abc" />
Style.php
<?php
header('Content-type: text/css');
$var = /*Get the background color setting for user abc*/;
?>
.wrap{
background-color:<?php echo $var; ?>;
}
That's what PHP was born for
Try echo $var;

Categories