Write permanently to a CSS file from php, no cookies or DB

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
bennyChidge
Forum Newbie
Posts: 17
Joined: Fri Sep 19, 2008 4:40 pm

Write permanently to a CSS file from php, no cookies or DB

Post by bennyChidge »

I am looking for a way to save poitions of divs permanently to a css file. To explain:

I have a Javascript that handles drag and drop of divs. Once the divs are dropped I will be using Ajax and Php to write the new positions to the CSS file.
I cant save this information in a cookie and ideally id like not to have to save it in my DB so is there a way I can write this value permanently to a CSS file so when anyone views the page it is in the order/position the artist has designated.

This is for an artist to position elements on there profile, and then for the "public" to view this profile in its new position. The "public" will not be logging, they will just be browsing the site.

I am finidng it a little difficult to get my head round.

Thanks
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Write permanently to a CSS file from php, no cookies or DB

Post by Eran »

Basically you want to build a parser that can understand CSS files in a simplistic fashion. Shouldn't be too complicated, and better yet - others already did it, so you can use open source projects such as CSSTidy to parse your file and perform your modifications.
Last edited by Eran on Sun Oct 12, 2008 10:07 am, edited 1 time in total.
bennyChidge
Forum Newbie
Posts: 17
Joined: Fri Sep 19, 2008 4:40 pm

Re: Write permanently to a CSS file from php, no cookies or DB

Post by bennyChidge »

Sorry I dont really understand what you have said:

I will have a div, once this div has been positioned I will send the X and y values to the css to be perminately written to the css.

I could have a variable in the css file which is updated but I would need to store the value for this variable somewhere (I.e it would first be sent in via xmlHttp and then potentially got from a DB or cookie) I dont want this I just want to "write" the value to the css file so it is permenant can I do this? If so could you explain the theory (as said i am havning trouble getting my head around this)
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Write permanently to a CSS file from php, no cookies or DB

Post by Eran »

There are no variables in CSS. To modify a CSS file you either have to physically change the file (hence my suggestion, google it), or dynamically compose the file at each page refresh (which you want to avoid - otherwise simply use a database to store the locations).

http://en.wikipedia.org/wiki/Parsing
bennyChidge
Forum Newbie
Posts: 17
Joined: Fri Sep 19, 2008 4:40 pm

Re: Write permanently to a CSS file from php, no cookies or DB

Post by bennyChidge »

Sorry I meant php variables in a css file

by adding this to apache httpconf

<IfModule mod_mime.c>
AddType application/x-httpd-php .css
</IfModule>

and this to the top of css file

<?php header("Content-type: text/css"); ?>

Then using in the css

background-color:<?php echo $variable; ?>;

Ill look into what you are saying with physically changing the file

thanks
sparrrow
Forum Commoner
Posts: 81
Joined: Mon Oct 20, 2008 12:22 pm

Re: Write permanently to a CSS file from php, no cookies or DB

Post by sparrrow »

Could you fwrite some php code to a file on the server, then include this file at the beginning of your php file?

So when a user makes a change, you could obtain the position coordinates through whatever means you like and write to the file something like this:

Code: Select all

fwrite('file.txt', '$x = '.$newx.'\n');
fwrite('file.txt', '$y = '.$newy.'\n');
and the file content would look like:

$x = 100;
$y = 200;


Then in your php file, just include('file.txt');

and define the style below that

Code: Select all

<style type="text/css">
.moveablediv {left:<?php echo $x; ?>px; top:<?php echo $y; ?>px;}
</style>
Sorry for the very crappy mix of real code and pseudocode, and I'm not even sure if the concept would work. It's just an idea I have.
bennyChidge
Forum Newbie
Posts: 17
Joined: Fri Sep 19, 2008 4:40 pm

Re: Write permanently to a CSS file from php, no cookies or DB

Post by bennyChidge »

I also asked this on another forum

http://www.webdeveloper.com/forum/showt ... post940639

they mentioned using fopen to write straight to the css file in question - im going to investigate on a few of these ideas and see what works best for me
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Write permanently to a CSS file from php, no cookies or DB

Post by alex.barylski »

I cant save this information in a cookie and ideally id like not to have to save it in my DB so is there a way I can write this value permanently to a CSS file so when anyone views the page it is in the order/position the artist has designated.
One problem I see with this approach is you are going to need multiple CSS files if you support multiple users. So you will need to store the values in a INI file or database and just pull them into the CSS file:

styles.css

Code: Select all

#layout {
  width: <?php echo $GLOBALS['userid']['css']['layout_width']; ?>;
}
If you only need to save the sizes for a single user then...sure write to the CSS file but an easier and fastert way would be to just use a template and PHP like above then serialize the result.s
bennyChidge
Forum Newbie
Posts: 17
Joined: Fri Sep 19, 2008 4:40 pm

Re: Write permanently to a CSS file from php, no cookies or DB

Post by bennyChidge »

I dont really want to store them in a database and pull them in as im looking at having quite a few users (300 +)

My current thoughts are:

It will be for artist after they have logged in and at present it is just x and y positioning for divs on there profiles. So I will have a main css file for all artist and a specific imported css file that just holds information for these div positions.
When the artist moves the divs around I will write these x and y values to the specific (and small) css file via xmlHTTP for each artist (which is stored in there own folder setup via registration etc). Then when viewers on the site view the particular artists profile it will reference and import the small written css file for the div positions.

If you only need to save the sizes for a single user then...sure write to the CSS file


I am presuming writing is going to take a while? even on a css file with the information for say 10 divs positions?

Code: Select all

#layout {
  width: <?php echo $GLOBALS['userid']['css']['layout_width']; ?>;
}
but an easier and fastert way would be to just use a template and PHP like above then serialize the result.s
can you explain what you mean by serialize?

Thanks
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Write permanently to a CSS file from php, no cookies or DB

Post by alex.barylski »

can you explain what you mean by serialize?
serializing typically means converting an object or array into a string which can be stored on file or in a database and restored at a later time.
When the artist moves the divs around I will write these x and y values to the specific (and small) css file via xmlHTTP for each artist (which is stored in there own folder setup via registration etc). Then when viewers on the site view the particular artists profile it will reference and import the small written css file for the div positions.
You sound like you have the problem figured out. Personally I would rather have one CSS file and generate the contents dynamically using a CSS template like the styles.css I showed above. This would require storing the data in a DB but would simplify the invocation of the CSS file.

If you have a separate CSS file for each user:

1. That is a lot of duplication is the declarations are all the same only the values are different
2. Updating a table is a lot easier than parsing CSS file, reseting values and then resaving the CSS file

There is also a lot more room for error using a CSS parser as opposed to just echo'ing stored results into a CSS template

HTH

Cheers,
Alex
bennyChidge
Forum Newbie
Posts: 17
Joined: Fri Sep 19, 2008 4:40 pm

Re: Write permanently to a CSS file from php, no cookies or DB

Post by bennyChidge »

My main concern is can the database handle all these requests?

300+ users - a call each time a page is loaded to echo these values.

Maybe I could dump these values into an xml file and then reference this from the css, then only change the xml file if the artist movs there profile again so as not to call the database each time.

Or write them straight to the css file or a smaller css file.

I am looking into a caching system that may help, but my main reason for not wanting to use a db is the worry that it would cause it to run slow. Seeing as this is my first time doing all this I am getting the impression I am seriously understimating Mysql. Especially if I index these values in the table and have a seperate table for just these values.

A bit of a thinking out loud post but it shows my train of thought.
sparrrow
Forum Commoner
Posts: 81
Joined: Mon Oct 20, 2008 12:22 pm

Re: Write permanently to a CSS file from php, no cookies or DB

Post by sparrrow »

MySQL can store millions of rows. The limit on data transfer is only as high as your hosting bandwidth. When I dump my entire products table (currently 100 rows, 15 columns), the query takes a fraction of 1 second. The only delay a client will experience is the actual transfer of the packets from server to client AFTER the query runs, and this delay is really just dependent on their ISP connection.

Now that I understand your requirements more, I will recommend storing the values in the database. MySQL can definitely handle it.

One random thought. Would you ever do a redesign on these pages in the future? Would you ever want to reset all div positions on everyone's profiles so they can start from scratch with your new redesign? This action would take a few seconds if the coordinates were managed in a database, but if you had 300+ css files you needed to manage, it would be alot more complicated.

http://rackerhacker.com/2007/01/04/mysq ... ta-limits/
bennyChidge
Forum Newbie
Posts: 17
Joined: Fri Sep 19, 2008 4:40 pm

Re: Write permanently to a CSS file from php, no cookies or DB

Post by bennyChidge »

so the term "underestimating mysql" was seriously underestiamted itself :)

- great news thanks for all the replies
Post Reply