Reputation: 12175
I need to create a dowloadable css file depending on variables changed on the front end.
Note: this is shortened example.
CSS:
.color: red; width: incrementvalue();
In html id have an inout field to adjust the width, example:
HTML
<input type="text" value="3">
Id then have a function that would run on change of the input that would do the following:
$('input').on ('change', function (){return $(this).val () * 2});
Yes the above function is in jquery but I dont really know php and im told this should be done in php.
Any ideas?
Upvotes: 0
Views: 2349
Reputation: 7795
You can create a regular PHP file starting with a CSS header:
<?php
header('Content-type: text/css');
After that, you can just echo out the CSS content. One way to do that is with heredoc syntax with a template system. The template puts variable values within '%%' percentage symbols as delimiters. Here's an example:
$css = <<<EOTAAA
* {
margin: 0;
padding: 0;
}
body {
background-color: %bg_color%;
background-image: url(images/bg.gif);
background-repeat: repeat-x;
font: 14px Arial, Helvetica, sans-serif;
}
div.my_div{
color: red;
width: %width%;
}
EOTAAA;
$search = array('%bg_color%', '%width%');
$width = width_function();//some function for determining width
$bg_color = bg_function();//some function for determining background color
$replace = array($bg_color, $width);
echo str_replace($search, $replace, $css);
Download Link:
In your HTML, create a link something like this with the URL of the file that will handle the request:
<a href="HTTP://www.example.com/download.php">Get CSS</a>
And then in your PHP file, change the header information to the following:
header("Content-Disposition: attachment; filename=\"custom.css\"");
header('Content-type: text/css');
header("Content-Type: application/force-download");
header("Connection: close");
Upvotes: 2
Reputation: 119
<link id='dynamic_stylesheet' rel='stylesheet' type='text/css' href='style.php?length=3' />
and later change href of $('#dynamic_stylesheet')
. In this case, you'd better just load extra styles of different selectors and change the class of input tag later.Upvotes: 0
Reputation: 1224
I can see you are binding change event. That probably means you want to change width dynamically. You can not script on client-side using PHP. PHP is server language which is useless in this case. If you want to change something dynamically, you have to use client scripting. jQuery (or Knockour.JS) is probably the easiest solution.
Upvotes: 0
Reputation: 40639
Try this,
<?php
header("Content-type: text/css; charset: UTF-8");
$my_width= incrementvalue();
?>
.phpClass{color: red; width: <?php echo $my_width;?>}
Save this file as style.php
Usage
<link rel='stylesheet' type='text/css' href='style.php' />
Read http://css-tricks.com/css-variables-with-php/
Upvotes: 0